2017-11-27 01:15:09 -05:00
|
|
|
// @flow
|
|
|
|
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 type {IVerdaccioConfig, IServerBridge, IServerProcess} from '../types';
|
2017-11-27 01:15:09 -05:00
|
|
|
|
|
|
|
export default class VerdaccioProcess implements IServerProcess {
|
|
|
|
|
2017-12-03 16:23:06 -05:00
|
|
|
bridge: IServerBridge;
|
|
|
|
config: IVerdaccioConfig;
|
|
|
|
childFork: any;
|
2018-06-18 15:58:09 -05:00
|
|
|
isDebug: boolean;
|
2017-12-03 16:23:06 -05:00
|
|
|
silence: boolean;
|
2018-06-24 15:39:09 -05:00
|
|
|
cleanStore: boolean;
|
2017-11-27 01:15:09 -05:00
|
|
|
|
2018-06-24 15:39:09 -05:00
|
|
|
constructor(config: IVerdaccioConfig,
|
|
|
|
bridge: IServerBridge,
|
|
|
|
silence: boolean = true,
|
|
|
|
isDebug: boolean = false,
|
|
|
|
cleanStore: boolean = 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
|
|
|
|
2018-06-24 15:39:09 -05:00
|
|
|
init(verdaccioPath: string = '../../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
|
|
|
|
2018-06-24 15:39:09 -05:00
|
|
|
_start(verdaccioPath: string, resolve: Function, reject: Function) {
|
|
|
|
const verdaccioRegisterWrap: string = path.join(__dirname, verdaccioPath);
|
|
|
|
let childOptions = {
|
|
|
|
silent: this.silence
|
|
|
|
};
|
2017-11-27 01:15:09 -05:00
|
|
|
|
2018-06-24 15:39:09 -05:00
|
|
|
if (this.isDebug) {
|
|
|
|
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;
|
|
|
|
// $FlowFixMe
|
|
|
|
this.childFork = fork(verdaccioRegisterWrap, ['-c', configPath, '-l', port], childOptions);
|
|
|
|
|
|
|
|
this.childFork.on('message', (msg) => {
|
|
|
|
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)
|
|
|
|
}, reject);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.childFork.on('error', (err) => {
|
|
|
|
console.log('error process', err);
|
|
|
|
reject([err, this]);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.childFork.on('disconnect', (err) => {
|
|
|
|
reject([err, this]);
|
|
|
|
});
|
2017-11-27 01:15:09 -05:00
|
|
|
|
2018-06-24 15:39:09 -05:00
|
|
|
this.childFork.on('exit', (err) => {
|
|
|
|
reject([err, this]);
|
2017-12-03 16:23:06 -05:00
|
|
|
});
|
|
|
|
}
|
2017-11-27 01:15:09 -05:00
|
|
|
|
2017-12-03 16:23:06 -05:00
|
|
|
stop(): void {
|
|
|
|
return this.childFork.kill('SIGINT');
|
|
|
|
}
|
2017-11-27 01:15:09 -05:00
|
|
|
|
2018-01-27 20:40:07 -05:00
|
|
|
}
|