-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
44 lines (42 loc) · 1.5 KB
/
index.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
const { fork } = require('child_process');
const style = require('ansi-styles');
const asyncFork = (file) => {
return new Promise((resolve, reject) => {
const fileFork = fork(file, [], {
silent: true,
env: {
...process.env,
NPM_CONFIG_COLOR: 'always'
},
stdio: 'inherit'
});
fileFork.on('exit', (code) => {
if (code != 0)
reject(code);
else
resolve();
});
});
}
const wait = (delay) => new Promise(resolve => setTimeout(resolve, delay));
module.exports = async (filePath, delay = 0) => {
console.log(`${style.green.open}${style.bold.open}[auto-restarter]${style.bold.close} starting \`${filePath}\`${style.green.close}`);
let continueLoop = true;
while (continueLoop) {
await asyncFork(filePath).then(() => {
console.log();
if (delay > 0)
console.log(`${style.red.open}${style.bold.open}[auto-restarter]${style.bold.close} app stopped - relaunching in ${delay}ms...${style.red.close}`);
else
console.log(`${style.red.open}${style.bold.open}[auto-restarter]${style.bold.close} app stopped - relaunching...${style.red.close}`);
}).catch(code => {
console.log();
if (delay > 0)
console.log(`${style.red.open}${style.bold.open}[auto-restarter]${style.bold.close} app crashed with code ${code} - relaunching in ${delay}ms...${style.red.close}`);
else
console.log(`${style.red.open}${style.bold.open}[auto-restarter]${style.bold.close} app crashed with code ${code} - relaunching...${style.red.close}`);
});
if (continueLoop)
await wait(delay);
}
}