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';
|
2019-07-16 01:40:01 -05:00
|
|
|
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
|
|
|
|
2019-07-16 01:40:01 -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;
|
2018-06-18 15:58:09 -05:00
|
|
|
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
|
|
|
|
2019-07-16 01:40:01 -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 = {
|
2019-07-16 01:40:01 -05:00
|
|
|
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) {
|
2019-07-16 01:40:01 -05:00
|
|
|
// @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;
|
2019-07-16 01:40:01 -05:00
|
|
|
this.childFork = fork(verdaccioRegisterWrap, ['-c', configPath, '-l', port as string], childOptions);
|
2018-06-24 15:39:09 -05:00
|
|
|
|
|
|
|
this.childFork.on('message', (msg) => {
|
2019-07-16 01:40:01 -05:00
|
|
|
// 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))
|
2018-08-21 01:05:34 -05:00
|
|
|
.then(() => resolve([this, body.pid]), reject)
|
2018-06-24 15:39:09 -05:00
|
|
|
}, reject);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-21 01:05:34 -05:00
|
|
|
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
|
|
|
|
2019-07-16 01:40:01 -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
|
|
|
|
2018-01-27 20:40:07 -05:00
|
|
|
}
|