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

79 lines
2 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';
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;
2017-11-27 01:15: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;
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
let childOptions = {
silent: this.silence
};
if (this.isDebug) {
childOptions = Object.assign({}, childOptions, {
execArgv: [`--inspect=${this.config.port + 5}`]
});
}
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) => {
this.bridge.auth('test', 'test')
.status(201)
.body_ok(/'test'/)
.then(() => {
resolve([this, body.pid]);
}, reject)
}, reject);
}
});
2017-11-27 01:15:09 -05:00
this.childFork.on('error', (err) => {
reject([err, this]);
2017-12-03 16:23:06 -05:00
});
2017-11-27 01:15:09 -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
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
}