-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli.js
executable file
·109 lines (88 loc) · 2.83 KB
/
cli.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env node
/**
* @import { ChildProcessByStdio } from 'child_process'
* @import { Readable } from 'stream'
*/
const log = require('./log');
const config = require('./config');
const { getFlag } = require('./utils');
if (config.isDev) {
const ROOT = process.cwd();
/** @type {ChildProcessByStdio<null, Readable, null>} */
let core;
/** @return {Promise<void> | void} */
function stopCore () {
if (!core || core.exitCode !== null) return;
return new Promise(resolve => {
core.kill();
let timeout;
core.once('exit', () => {
clearTimeout(timeout);
resolve();
});
timeout = setTimeout(() => core.kill(9), 500);
});
}
let restarting = false;
const { spawn } = require('child_process');
const start = async reason => {
if (restarting) return;
restarting = true;
await stopCore();
log.text('');
log.info('Starting API...');
core = spawn(
'node',
[__dirname + '/index.js', ...process.argv.slice(2), '--restart-reason', reason],
{ cwd: ROOT, env: process.env, stdio: ['ignore', 'pipe', 'inherit'] }
);
core.stdout.pipe(process.stdout);
core.stdout.once('readable', () => restarting = false);
core.once('exit', code => {
if (code) {
// Converting i64 (JS Number) to i32 code
log.error('API server process crushed with code ' + (code | 0));
} else if (!restarting) {
log.info('API server process exited');
}
restarting = false;
});
};
log.text('API will be restarted after saving any file');
log.text('You can submit `rs` to restart server manually');
start('@initial');
process.stdin.on('data', line => {
if (line.toString().trim() == 'rs') start('@manual');
});
const watch = require('watch');
watch.watchTree(ROOT, {
ignoreDotFiles: true,
filter (file) {
file = file.replace(/\\/g, '/').slice(file.lastIndexOf('/') + 1);
if (config.ignore.indexOf(file) != -1) {
return false;
} else if (file.includes('/node_modules/')) {
return false;
} else {
return true;
}
},
interval: 0.075
}, (path, curr, prev) => {
if (typeof path == 'object' && !prev && !curr) return;
start(path);
});
process.on('SIGINT', async () => {
restarting = true;
stopCore();
process.exit();
});
} else if (getFlag('--ts-build')) {
if (!config.typescript) {
log.warn('Typescript is not enabled');
process.exit();
}
require('./typescript/build');
} else {
require('.');
}