0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-23 22:27:34 -05:00
verdaccio/test/lib/server_process.ts

86 lines
2.6 KiB
TypeScript
Raw Normal View History

2017-11-27 01:15:09 -05:00
import _ from 'lodash';
import rimRaf from 'rimraf';
import path from 'path';
import {fork} from 'child_process';
2018-06-24 03:11:52 -05:00
import {CREDENTIALS} from '../functional/config.functional';
2018-06-23 04:51:58 -05:00
import {HTTP_STATUS} from '../../src/lib/constants';
import {IVerdaccioConfig, IServerBridge, IServerProcess} from '../types';
2017-11-27 01:15:09 -05:00
export default class VerdaccioProcess implements IServerProcess {
2019-09-26 11:22:14 -05:00
private bridge: IServerBridge;
private config: IVerdaccioConfig;
private childFork: any;
private isDebug: boolean;
private silence: boolean;
private cleanStore: boolean;
2017-11-27 01:15:09 -05:00
public constructor(config: IVerdaccioConfig,
bridge: IServerBridge,
2019-09-26 11:22:14 -05:00
silence = true,
isDebug = false,
cleanStore = true) {
2017-12-03 16:23:06 -05:00
this.config = config;
this.bridge = bridge;
this.silence = silence;
this.isDebug = isDebug;
2018-06-24 15:39:09 -05:00
this.cleanStore = cleanStore;
2017-12-03 16:23:06 -05:00
}
2017-11-27 01:15:09 -05:00
2019-09-26 11:22:14 -05:00
public init(verdaccioPath = '../../bin/verdaccio'): Promise<any> {
2017-12-03 16:23:06 -05:00
return new Promise((resolve, reject) => {
2018-06-24 15:39:09 -05:00
if(this.cleanStore) {
rimRaf(this.config.storagePath, (err) => {
if (_.isNil(err) === false) {
reject(err);
2017-12-03 16:23:06 -05:00
}
2017-11-27 01:15:09 -05:00
2018-06-24 15:39:09 -05:00
this._start(verdaccioPath, resolve, reject);
2017-12-03 16:23:06 -05:00
});
2018-06-24 15:39:09 -05:00
} else {
this._start(verdaccioPath, resolve, reject);
}
});
}
2017-11-27 01:15:09 -05:00
private _start(verdaccioPath: string, resolve: Function, reject: Function) {
2018-06-24 15:39:09 -05:00
const verdaccioRegisterWrap: string = path.join(__dirname, verdaccioPath);
let childOptions = {
silent: true
2018-06-24 15:39:09 -05:00
};
2017-11-27 01:15:09 -05:00
2018-06-24 15:39:09 -05:00
if (this.isDebug) {
// @ts-ignore
2018-06-24 15:39:09 -05:00
const debugPort = parseInt(this.config.port, 10) + 5;
2017-11-27 01:15:09 -05:00
2018-06-24 15:39:09 -05:00
childOptions = Object.assign({}, childOptions, {
execArgv: [`--inspect=${debugPort}`]
2017-12-03 16:23:06 -05:00
});
2018-06-24 15:39:09 -05:00
}
const {configPath, port} = this.config;
this.childFork = fork(verdaccioRegisterWrap, ['-c', configPath, '-l', port as string], childOptions);
2018-06-24 15:39:09 -05:00
this.childFork.on('message', (msg) => {
// verdaccio_started is a message that comes from verdaccio in debug mode that notify has been started
2018-06-24 15:39:09 -05:00
if ('verdaccio_started' in msg) {
this.bridge.debug().status(HTTP_STATUS.OK).then((body) => {
this.bridge.auth(CREDENTIALS.user, CREDENTIALS.password)
.status(HTTP_STATUS.CREATED)
.body_ok(new RegExp(CREDENTIALS.user))
.then(() => resolve([this, body.pid]), reject)
2018-06-24 15:39:09 -05:00
}, reject);
}
});
this.childFork.on('error', (err) => reject([err, this]));
this.childFork.on('disconnect', (err) => reject([err, this]));
this.childFork.on('exit', (err) => reject([err, this]));
2017-12-03 16:23:06 -05:00
}
2017-11-27 01:15:09 -05:00
public stop(): void {
2017-12-03 16:23:06 -05:00
return this.childFork.kill('SIGINT');
}
2017-11-27 01:15:09 -05:00
}