forked from claudiajs/example-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchild-process-promise.js
39 lines (39 loc) · 947 Bytes
/
child-process-promise.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
/*global module, require, console, Promise */
const childProcess = require('child_process'),
execPromise = function (command) {
'use strict';
return new Promise(function (resolve, reject) {
childProcess.exec(command, function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
});
},
spawnPromise = function (command, options) {
'use strict';
return new Promise(function (resolve, reject) {
const process = childProcess.spawn(command, options),
result = [];
process.stdout.on('data', function (buffer) {
console.log(buffer.toString());
result.push(buffer.toString());
});
process.stderr.on('data', function (buffer) {
console.error(buffer.toString());
});
process.on('close', function (code) {
if (code !== 0) {
reject(code);
} else {
resolve(result.join(''));
}
});
});
};
module.exports = {
exec: execPromise,
spawn: spawnPromise
};