0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-02-10 23:39:31 -05:00
verdaccio/packages/mock/src/mock.ts

99 lines
2.8 KiB
TypeScript
Raw Normal View History

2020-03-03 23:59:19 +01:00
import path from 'path';
import fs from 'fs';
import * as fsExtra from 'fs-extra';
import os from 'os';
import {DOMAIN_SERVERS} from './constants';
import VerdaccioProcess from './server_process';
import {VerdaccioConfig} from './verdaccio-server';
import Server from './server';
import {IServerBridge} from './types';
/**
* Fork a Verdaccio process with a custom configuration.
*
* Usage:
*
* - Fork the process within the beforeAll body.
* - Define a storage (use a specific name)
* - Define a unique port (be careful with conflicts)
* - Set a configuration
* - await / mockServer
* - call done();
*
* beforeAll(function(done) {
const store = path.join(__dirname, '../partials/store/test-profile-storage');
const mockServerPort = 55544;
rimraf(store, async () => {
const parsedConfig = parseConfigFile(parseConfigurationProfile());
const configForTest = _.assign({}, _.cloneDeep(parsedConfig), {
storage: store,
auth: {
htpasswd: {
file: './test-profile-storage/.htpasswd'
}
},
self_path: store
});
app = await endPointAPI(configForTest);
mockRegistry = await mockServer(mockServerPort).init();
done();
});
On finish the test we must close the server
afterAll(function(done) {
mockRegistry[0].stop();
done();
});
*
*
* @param port
* @returns {VerdaccioProcess}
*/
export function mockServer(port: number, options: MockRegistryOptions = {}) {
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), '/verdaccio-test'));
// console.log("-->tempRoot", tempRoot);
// default locations
const configPath = path.join(__dirname, './config/yaml', '/mock-server-test.yaml');
const mockStorePath = path.join(__dirname, '/fixtures/mock-store');
// default options
const localOptions: MockRegistryOptions = {
port,
configPath,
storePath: mockStorePath,
rootFolder: tempRoot,
silence: true,
debug: false
};
// mix external options
const finalOptions: MockRegistryOptions = Object.assign({}, localOptions, options);
// console.log('--->finalOptions=>', finalOptions);
// final locations
const tempConfigFile = path.join(tempRoot, 'verdaccio.yaml');
const storePath = path.join(tempRoot, '/mock-store');
fs.copyFileSync(finalOptions.configPath!, tempConfigFile);
fsExtra.copySync(finalOptions.storePath!, storePath);
const verdaccioConfig = new VerdaccioConfig(storePath, tempConfigFile, `http://${DOMAIN_SERVERS}:${port}/`, port);
const server: IServerBridge = new Server(verdaccioConfig.domainPath);
return new VerdaccioProcess(verdaccioConfig, server, finalOptions.silence, finalOptions.debug);
}
export interface MockRegistryOptions {
rootFolder?: string;
configPath?: string;
port?: number;
storePath?: string;
silence?: boolean
debug?: boolean;
}