-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.ts
62 lines (52 loc) · 1.69 KB
/
loader.ts
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
52
53
54
55
56
57
58
59
60
61
62
import fs from 'fs';
import { spawn } from 'child_process';
import * as color from './lib/color';
console.log(color.loader + 'Preparing core...');
function loader() {
return spawn('node', ['./dist/main']);
}
function sleep(ms: number) {
return new Promise( resolve => setTimeout(resolve, ms) );
}
function run() {
const subprocess = loader();
const confWatcher = fs.watch('config.yaml', function () {
console.log(color.loader + 'Config changed.');
restart();
});
const coreWatcher = fs.watch('main.ts', function () {
console.log(color.loader + 'Core updated.');
deferRestart();
});
subprocess.stdout.on('data', data => {
process.stdout.write(data.toString());
});
subprocess.stderr.on('data', data => {
process.stderr.write(color.fRed + data.toString());
});
subprocess.on('exit', () => {
console.log(color.loader + 'Subprocess exited.');
run();
});
function restart() {
console.log(color.loader + 'Reloading...');
confWatcher.close();
coreWatcher.close();
subprocess.stdin.write('KINTERNALLOADERQUIT\n');
// do not use CTRLC/SIGTERM/SIGKILL to kill process
// otherwise subprocess will never exit on reload
// until there is a core error or loader restart
};
async function deferRestart() {
console.log(color.loader + 'Deferred reloading...');
confWatcher.close();
coreWatcher.close();
await sleep(3000);
restart();
};
process.stdin.on('data', (data: Buffer) => { // passthrough stdin to core
let key = data.toString().trim();
subprocess.stdin.write(key);
});
}
run();