-
Notifications
You must be signed in to change notification settings - Fork 6
/
spawn-history-server.mjs
52 lines (42 loc) · 1.49 KB
/
spawn-history-server.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import child_process from "child_process";
import path from "path";
import fs from "fs";
import { dirname } from 'path';
import { fileURLToPath } from 'url';
export function HistoryServer(historyFile, portNumber) {
const self = {
start,
stop
};
const serverLog = fs.createWriteStream("history-server.log");
const historyFileAbsolutePath = path.resolve(historyFile);
const __dirname = dirname(fileURLToPath(import.meta.url));
const serverScriptPath = path.resolve(__dirname, "../history-api/server.js");
let proc;
function start() {
return new Promise((accept, reject) => {
proc = child_process.spawn("node", [
serverScriptPath,
historyFileAbsolutePath,
String(portNumber)
]);
proc.stdout.pipe(serverLog);
proc.stderr.pipe(serverLog);
const listener = (data) => {
if (data.toString().indexOf("Listening on " + portNumber) !== -1) {
proc.stdout.off("data", listener);
accept();
}
};
proc.stdout.on("data", listener);
proc.on('exit', (code) => {
serverLog.write(`child process exited with code ${code}\n`);
reject(new Error(`Server process exited with code ${code}`));
});
});
}
function stop() {
proc.kill();
}
return self;
}