-
Notifications
You must be signed in to change notification settings - Fork 51
/
utils.js
61 lines (50 loc) · 1.72 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
53
54
55
56
57
58
59
60
61
'use strict';
const {spawn} = require('child_process');
// Try to resolve path to shell.
// We assume that Windows provides COMSPEC env variable
// and other platforms provide SHELL env variable
const SHELL_PATH = process.env.SHELL || process.env.COMSPEC;
const EXECUTE_OPTION = process.env.COMSPEC !== undefined && process.env.SHELL === undefined ? '/c' : '-c';
// XXX: Wrapping tos to a promise is a bit wrong abstraction. Maybe RX suits
// better?
function run(cmd, opts) {
if (!SHELL_PATH) {
// If we cannot resolve shell, better to just crash
throw new Error('$SHELL environment variable is not set.');
}
opts = {
pipe: true,
cwd: undefined,
callback(child) { // eslint-disable-line no-unused-vars
// Since we return promise, we need to provide
// this callback if one wants to access the child
// process reference
// Called immediately after successful child process
// spawn
}, ...opts};
return new Promise((resolve, reject) => {
let child;
try {
child = spawn(SHELL_PATH, [EXECUTE_OPTION, cmd], {
cwd: opts.cwd,
stdio: opts.pipe ? 'inherit' : null
});
} catch (error) {
return reject(error);
}
opts.callback(child);
function errorHandler(err) {
child.removeListener('close', closeHandler);
reject(err);
}
function closeHandler(exitCode) {
child.removeListener('error', errorHandler);
resolve(exitCode);
}
child.once('error', errorHandler);
child.once('close', closeHandler);
});
}
module.exports = {
run
};