-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (50 loc) · 1.26 KB
/
index.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
57
58
59
60
61
// @flow
'use strict';
const crossSpawn = require('cross-spawn');
class ChildProcessError extends Error {
/*::
code: number;
stdout: string;
stderr: string;
*/
constructor(code, stdout, stderr) {
super(stderr);
Error.captureStackTrace(this, this.constructor);
this.code = code;
this.stdout = stdout;
this.stderr = stderr;
}
}
function spawn(
cmd /*: string */,
args /*: Array<string> */,
opts /*: ?child_process$spawnOpts */
) {
return new Promise((resolve, reject) => {
let stdoutBuf = Buffer.from('');
let stderrBuf = Buffer.from('');
let child = crossSpawn(cmd, args, opts);
if (child.stdout) {
child.stdout.on('data', data => {
stdoutBuf = Buffer.concat([stdoutBuf, data]);
});
}
if (child.stderr) {
child.stderr.on('data', data => {
stderrBuf = Buffer.concat([stderrBuf, data]);
});
}
child.on('error', reject);
child.on('close', code => {
let stdout = stdoutBuf.toString();
let stderr = stderrBuf.toString();
if (code === 0) {
resolve({code, stdout, stderr});
} else {
reject(new ChildProcessError(code, stdout, stderr));
}
});
});
}
spawn.ChildProcessError = ChildProcessError;
module.exports = spawn;