-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecProc.js
56 lines (44 loc) · 1.36 KB
/
execProc.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
import { spawn } from 'child_process';
function wrapOutput(cmd, output) {
return output.toString()
.split(/\r\n|\n/)
.filter((line, index) => line && line.length > 0)
.map(line => `${cmd} > ${line}`)
.join('\n');
}
export function execProcess(cmd, args) {
console.log(`RUN: ${cmd} ${args ? args.join(' ') : ''}`);
const childProcess = spawn(cmd, args, {
env: {
...process.env
}
});
childProcess.stdout.on('data', data => {
console.log(wrapOutput(cmd, data));
});
childProcess.stderr.on('data', data => {
console.error(wrapOutput(cmd, data));
});
childProcess.on('error', e => {
console.error(`${cmd} error: [${e.name}] ${e.message}`, e);
});
childProcess.on('exit', code => {
console.log(`${cmd} exit with code ${code}`);
});
return childProcess;
}
export function execProcessAsync(cmd, args) {
return new Promise((resolve, reject) => {
const process = execProcess(cmd, args);
process.on('exit', (code) => {
process.removeAllListeners();
if (code === 0) {
resolve(0);
return;
}
const error = new Error(`Process exit with status code ${code}`);
error.code = code;
reject(error);
});
});
}