-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
46 lines (38 loc) · 1.04 KB
/
server.js
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
#!/usr/bin/env node
"use strict";
const fork = require('child_process').fork;
const path = require('path');
const BACKEND_DIR = path.join(__dirname, 'backend');
const FRONTEND_DIR = path.join(__dirname, 'frontend');
const PROXY_DIR = path.join(__dirname, 'proxy');
const BACKEND_SCRIPT = path.join(BACKEND_DIR, 'lib');
const FRONTEND_SCRIPT = path.join(FRONTEND_DIR, 'lib/server');
const PROXY_SCRIPT = path.join(PROXY_DIR, 'lib');
const backend = fork(BACKEND_SCRIPT, { cwd: BACKEND_DIR, env: process.env });
const frontend = fork(FRONTEND_SCRIPT, { cwd: FRONTEND_DIR, env: process.env });
let proxy;
function ready(proc) {
return new Promise(resolve => {
proc.on('message', message => {
if (message === 'ready') {
resolve();
}
});
});
}
Promise.all([
ready(backend),
ready(frontend),
]).then(() => {
proxy = fork(PROXY_SCRIPT, { cwd: PROXY_DIR });
});
function exit() {
backend.kill();
frontend.kill();
if (proxy) {
proxy.kill();
}
process.exit();
}
process.on('SIGINT', exit);
process.on('SIGTERM', exit);