-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexec-process.js
37 lines (35 loc) · 1.19 KB
/
exec-process.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
const util = require('util');
var exec = util.promisify(require('child_process').exec);
async function callExec(command) {
try {
const { stdout, stderr } = await exec(command);
/* console.log('stdout:', stdout);
console.log('stderr:', stderr); */
if (typeof (stderr) != "string") {
throw new Error(stderr);
} else {
return stdout;
}
} catch (e) {
console.error(e); // should contain code (exit code) and signal (that caused the termination).
}
}
// https://stackoverflow.com/a/29655902/253576
var result = function (command) {
return new Promise((resolve, reject) => {
var child = exec(command, (err, stdout, stderr) => {
/* console.log('stdout:', stdout);
console.log('stderr:', stderr);
console.log('err:', err); */
if (err != null) {
reject(new Error(err), null);
} else if (typeof (stderr) != "string") {
reject(new Error(stderr), null);
} else {
resolve(stdout);
}
});
});
}
exports.callExec = callExec;
exports.result = result;