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-17 06:34:59 -05:00
|
|
|
import type {IVerdaccioConfig, IServerBridge, IServerProcess} from '../types';
|
2018-06-23 02:46:09 -05:00
|
|
|
import {CREDENTIALS} from '../functional/config.func';
|
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;
|
2017-11-27 01:15:09 -05:00
|
|
|
|
2018-06-18 15:58:09 -05:00
|
|
|
constructor(config: IVerdaccioConfig, bridge: IServerBridge, silence: boolean = true, isDebug: boolean = false) {
|
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;
|
2017-12-03 16:23:06 -05:00
|
|
|
}
|
2017-11-27 01:15:09 -05:00
|
|
|
|
2017-12-03 16:23:06 -05:00
|
|
|
init(): Promise<any> {
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-02-09 02:42:34 -05:00
|
|
|
const verdaccioRegisterWrap: string = path.join(__dirname, '../../bin/verdaccio');
|
2017-11-27 01:15:09 -05:00
|
|
|
|
2018-02-09 02:42:34 -05:00
|
|
|
rimRaf(this.config.storagePath, (err) => {
|
2017-12-03 16:23:06 -05:00
|
|
|
if (_.isNil(err) === false) {
|
|
|
|
reject(err);
|
|
|
|
}
|
2017-11-27 01:15:09 -05:00
|
|
|
|
2018-06-18 15:58:09 -05:00
|
|
|
let childOptions = {
|
|
|
|
silent: this.silence
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.isDebug) {
|
2018-06-23 02:46:09 -05:00
|
|
|
const debugPort = parseInt(this.config.port, 10) + 5;
|
|
|
|
|
2018-06-18 15:58:09 -05:00
|
|
|
childOptions = Object.assign({}, childOptions, {
|
2018-06-23 02:46:09 -05:00
|
|
|
execArgv: [`--inspect=${debugPort}`]
|
2018-06-18 15:58:09 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.childFork = fork(verdaccioRegisterWrap, ['-c', this.config.configPath], childOptions);
|
2017-11-27 01:15:09 -05:00
|
|
|
|
2017-12-03 16:23:06 -05:00
|
|
|
this.childFork.on('message', (msg) => {
|
|
|
|
if ('verdaccio_started' in msg) {
|
|
|
|
this.bridge.debug().status(200).then((body) => {
|
2018-06-23 02:46:09 -05:00
|
|
|
this.bridge.auth(CREDENTIALS.user, CREDENTIALS.password)
|
2017-12-03 16:23:06 -05:00
|
|
|
.status(201)
|
2018-06-23 02:46:09 -05:00
|
|
|
.body_ok(new RegExp(CREDENTIALS.user))
|
2017-12-03 16:23:06 -05:00
|
|
|
.then(() => {
|
|
|
|
resolve([this, body.pid]);
|
|
|
|
}, reject)
|
|
|
|
}, reject);
|
|
|
|
}
|
|
|
|
});
|
2017-11-27 01:15:09 -05:00
|
|
|
|
2018-01-27 20:40:07 -05:00
|
|
|
this.childFork.on('error', (err) => {
|
2018-06-23 02:46:09 -05:00
|
|
|
console.log('error process', err);
|
2018-01-27 20:40:07 -05:00
|
|
|
reject([err, this]);
|
2017-12-03 16:23:06 -05:00
|
|
|
});
|
2017-11-27 01:15:09 -05:00
|
|
|
|
2018-01-27 20:40:07 -05:00
|
|
|
this.childFork.on('disconnect', (err) => {
|
|
|
|
reject([err, this]);
|
2017-12-03 16:23:06 -05:00
|
|
|
});
|
2017-11-27 01:15:09 -05:00
|
|
|
|
2018-01-27 20:40:07 -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
|
|
|
});
|
2017-11-27 01:15:09 -05:00
|
|
|
|
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
|
|
|
}
|