1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-17 07:45:52 +01:00
verdaccio/test/lib/server_process.js

97 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-11-27 07:15:09 +01:00
// @flow
import _ from 'lodash';
import rimRaf from 'rimraf';
import path from 'path';
import {fork} from 'child_process';
2018-06-24 10:11:52 +02:00
import {CREDENTIALS} from '../functional/config.functional';
2018-06-23 11:51:58 +02:00
import {HTTP_STATUS} from '../../src/lib/constants';
import type {IVerdaccioConfig, IServerBridge, IServerProcess} from '../types';
2017-11-27 07:15:09 +01:00
export default class VerdaccioProcess implements IServerProcess {
2017-12-03 22:23:06 +01:00
bridge: IServerBridge;
config: IVerdaccioConfig;
childFork: any;
isDebug: boolean;
2017-12-03 22:23:06 +01:00
silence: boolean;
2018-06-24 22:39:09 +02:00
cleanStore: boolean;
2017-11-27 07:15:09 +01:00
2018-06-24 22:39:09 +02:00
constructor(config: IVerdaccioConfig,
bridge: IServerBridge,
silence: boolean = true,
isDebug: boolean = false,
cleanStore: boolean = true) {
2017-12-03 22:23:06 +01:00
this.config = config;
this.bridge = bridge;
this.silence = silence;
this.isDebug = isDebug;
2018-06-24 22:39:09 +02:00
this.cleanStore = cleanStore;
2017-12-03 22:23:06 +01:00
}
2017-11-27 07:15:09 +01:00
2018-06-24 22:39:09 +02:00
init(verdaccioPath: string = '../../bin/verdaccio'): Promise<any> {
2017-12-03 22:23:06 +01:00
return new Promise((resolve, reject) => {
2018-06-24 22:39:09 +02:00
if(this.cleanStore) {
rimRaf(this.config.storagePath, (err) => {
if (_.isNil(err) === false) {
reject(err);
2017-12-03 22:23:06 +01:00
}
2017-11-27 07:15:09 +01:00
2018-06-24 22:39:09 +02:00
this._start(verdaccioPath, resolve, reject);
2017-12-03 22:23:06 +01:00
});
2018-06-24 22:39:09 +02:00
} else {
this._start(verdaccioPath, resolve, reject);
}
});
}
2017-11-27 07:15:09 +01:00
2018-06-24 22:39:09 +02:00
_start(verdaccioPath: string, resolve: Function, reject: Function) {
const verdaccioRegisterWrap: string = path.join(__dirname, verdaccioPath);
let childOptions = {
silent: this.silence
};
2017-11-27 07:15:09 +01:00
2018-06-24 22:39:09 +02:00
if (this.isDebug) {
const debugPort = parseInt(this.config.port, 10) + 5;
2017-11-27 07:15:09 +01:00
2018-06-24 22:39:09 +02:00
childOptions = Object.assign({}, childOptions, {
execArgv: [`--inspect=${debugPort}`]
2017-12-03 22:23:06 +01:00
});
2018-06-24 22:39:09 +02: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 07:15:09 +01:00
2018-06-24 22:39:09 +02:00
this.childFork.on('exit', (err) => {
reject([err, this]);
2017-12-03 22:23:06 +01:00
});
}
2017-11-27 07:15:09 +01:00
2017-12-03 22:23:06 +01:00
stop(): void {
return this.childFork.kill('SIGINT');
}
2017-11-27 07:15:09 +01:00
}