-
Notifications
You must be signed in to change notification settings - Fork 1
/
wait-before.cjs
47 lines (37 loc) · 1.07 KB
/
wait-before.cjs
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
const [, , beforeCommand, afterCommand, triggerText] = process.argv;
const child_process = require('child_process');
const launchBackend = child_process.spawn(beforeCommand, {
stdio: ['inherit', 'pipe', 'inherit'],
env: process.env,
shell: true,
detached: true,
});
launchBackend.stdout.pipe(process.stdout);
launchBackend.on('close', () => {
if (launchBackend.exitCode !== null) {
process.exit(launchBackend.exitCode);
}
});
process.on('SIGINT', () => {
process.kill(-launchBackend.pid);
});
launchBackend.stdout.on('data', (data) => {
if (data.toString().includes(triggerText)) {
console.log(`${beforeCommand} done, executing ${afterCommand}`);
const exec = child_process.spawn(afterCommand, {
stdio: ['inherit', 'inherit', 'inherit'],
env: process.env,
shell: true,
detached: true,
});
process.on('SIGINT', () => {
process.kill(-exec.pid);
});
exec.on('close', () => {
process.kill(-launchBackend.pid);
if (exec.exitCode !== null) {
process.exit(exec.exitCode);
}
});
}
});