-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.js
46 lines (40 loc) · 1.52 KB
/
exec.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
'use strict';
const spawn = require("child_process").spawn;
/**
* Wrap executing a command in a promise
* @param {string} command command to execute
* @param {Array<string>} args Arguments to the command.
* @param {string} cwd The working directory to run the command in.
* @param {bool} shouldReturnOutput set to true if stdout should be returned.
* @return {Promise} A promise for the completion of the command.
*/
module.exports = function exec(command, args, cwd, shouldReturnOutput) {
return new Promise((resolve, reject) => {
const proc = spawn(command, args, {
cwd: cwd,
stdio: ['ignore', (shouldReturnOutput ? 'pipe' : 'ignore'), 'pipe']
});
let stdout;
if (shouldReturnOutput) {
stdout = "";
proc.stdout.on('data', (chunk) => {
stdout += chunk.toString("utf8");
});
}
let stderr = "";
proc.stderr.on("data", (chunk) => {
stderr += chunk.toString("utf8");
});
proc.on("error", (error) => reject(error));
proc.on("close", (code) => {
if (code === 0) {
resolve(shouldReturnOutput ? stdout : undefined);
} else {
if (stderr) {
console.log("["+proc.pid+"]", "stderr", "*" + stderr.trim() + "*");
}
reject(new Error("'" + command + " " + args.join(" ") + "' in " + cwd + " exited with code " + code));
}
});
});
};