-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonitor.js
77 lines (69 loc) · 2.03 KB
/
monitor.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
const argv = require('minimist')(process.argv.slice(2));
const compiler = require('./compiler.js');
const fs = require('fs');
const { spawn } = require('child_process');
const path = require('path');
require('colors');
let timeout = null;
let child = null;
let directory = null;
function stopServer() {
if (child == null)
return;
console.log("Stopping server".red);
child.kill('SIGINT');
child = null;
}
function startServer() {
timeout = null;
stopServer();
compiler.compile(directory);
let port = argv.p ?? argv.port ?? 8080;
let serverType = argv._.includes('remote') ? 'server-remote.js' : 'server.js';
child = spawn(process.execPath, [path.join(__dirname, serverType), port, directory, '--color'], {
detached: true,
stdio: 'pipe'
});
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
}
function keepAliveChild() {
if (child == null)
return;
child.stdin.write('1', (err) => { });
}
async function main() {
directory = argv.dir ?? argv.d;
if (directory != null && !path.isAbsolute(directory)) {
directory = path.join(process.cwd(), directory);
}
else if (directory == null) {
directory = process.cwd();
}
if (argv._.includes('typings')) {
require('./types-generator.js')(directory);
return;
}
if (argv._.includes('publish')) {
await require('./publisher.js')(directory);
return;
}
console.log(("Monitoring folder: " + directory + "\n").yellow);
fs.watch(directory, (eventType, filename) => {
let extension = path.extname(filename);
if (extension != '.js' && extension != '.ts' && !filename.includes('.env'))
return;
if (timeout != null) {
clearTimeout(timeout);
}
timeout = setTimeout(startServer, 500);
});
process.on('SIGINT', () => {
stopServer();
process.exit();
});
process.on('exit', stopServer);
startServer();
setInterval(keepAliveChild, 5000);
}
main();