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.js

86 lines
2.5 KiB
JavaScript
Raw Normal View History

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;
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;
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)
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
2017-12-03 16:23:06 -05:00
stop(): void {
return this.childFork.kill('SIGINT');
}
2017-11-27 01:15:09 -05:00
}