-
Notifications
You must be signed in to change notification settings - Fork 463
/
Copy pathutils.js
52 lines (42 loc) · 1.18 KB
/
utils.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
const child_process = require("child_process");
const signals = {
SIGINT: 2,
SIGQUIT: 3,
SIGKILL: 9,
SIGTERM: 15,
};
/**
* @param {string} command
* @param {Array<string>} args
* @param {child_process.SpawnOptions} [options]
*/
async function exec(command, args, options) {
const stdoutChunks = [];
const stderrChunks = [];
const subprocess = child_process.spawn(command, args, {
stdio: ["ignore", "pipe", "pipe"],
...options,
});
subprocess.stdout.on("data", chunk => {
stdoutChunks.push(chunk);
});
subprocess.stderr.on("data", chunk => {
stderrChunks.push(chunk);
});
return await new Promise((resolve, reject) => {
subprocess.once("error", err => {
reject(err);
});
subprocess.once("close", (exitCode, signal) => {
const stdout = Buffer.concat(stdoutChunks).toString("utf8");
const stderr = Buffer.concat(stderrChunks).toString("utf8");
let code = exitCode ?? 1;
if (signals[signal]) {
// + 128 is standard POSIX practice, see also https://nodejs.org/api/process.html#exit-codes
code = signals[signal] + 128;
}
resolve({ code, stdout, stderr });
});
});
}
exports.exec = exec;