forked from nodejs/node-core-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.js
More file actions
70 lines (61 loc) · 1.62 KB
/
run.js
File metadata and controls
70 lines (61 loc) · 1.62 KB
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
62
63
64
65
66
67
68
69
70
'use strict';
const { spawn, spawnSync } = require('child_process');
const IGNORE = '__ignore__';
function runAsyncBase(cmd, args, options = {}) {
return new Promise((resolve, reject) => {
const child = spawn(cmd, args, Object.assign({
cwd: process.cwd(),
stdio: 'inherit'
}, options.spawnArgs));
child.on('close', (code) => {
if (code !== 0) {
const { ignoreFailure = true } = options;
if (ignoreFailure) {
return reject(new Error(IGNORE));
}
const err = new Error(`${cmd} failed: ${code}`);
err.code = code;
err.messageOnly = true;
return reject(err);
}
return resolve();
});
});
}
exports.forceRunAsync = function(cmd, args, options) {
return runAsyncBase(cmd, args, options).catch((error) => {
if (error.message !== IGNORE) {
if (!error.messageOnly) {
console.error(error);
}
throw error;
}
});
};
exports.runPromise = function runAsync(promise) {
return promise.catch((error) => {
if (error.message !== IGNORE) {
console.error(error);
}
exports.exit();
});
};
exports.runAsync = function(cmd, args, options) {
return exports.runPromise(runAsyncBase(cmd, args, options));
};
exports.runSync = function(cmd, args, options) {
const child = spawnSync(cmd, args, Object.assign({
cwd: process.cwd()
}, options));
if (child.error) {
throw child.error;
} else if (child.stderr.length) {
throw new Error(child.stderr.toString());
} else {
return child.stdout.toString();
}
};
exports.exit = function() {
process.exit(1);
};
exports.IGNORE = IGNORE;