サーバー起動処理を共通化

This commit is contained in:
rinsuki
2019-10-25 06:24:06 +09:00
parent 6e16c9389f
commit cc7cc56abe
6 changed files with 47 additions and 85 deletions

View File

@@ -2,6 +2,7 @@ import * as fs from 'fs';
import * as WebSocket from 'ws';
const fetch = require('node-fetch');
import * as req from 'request';
import * as childProcess from 'child_process';
export const async = (fn: Function) => (done: Function) => {
fn().then(() => {
@@ -102,3 +103,16 @@ export function connectStream(user: any, channel: string, listener: (message: Re
});
});
}
export function launchServer(callbackSpawnedProcess: (p: childProcess.ChildProcess) => void, moreProcess: () => Promise<void> = async () => {}) {
return (done: (err?: Error) => any) => {
const p = childProcess.spawn('node', [__dirname + '/../index.js'], {
stdio: ['inherit', 'inherit', 'ipc'],
env: { NODE_ENV: 'test', PATH: process.env.PATH }
});
callbackSpawnedProcess(p)
p.on('message', message => {
if (message === 'ok') moreProcess().then(() => done()).catch(e => done(e));
});
};
}