-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
57 lines (49 loc) · 1.31 KB
/
lib.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
const exec = require('child_process').exec;
const run = (command, execOptions = {}, {
verbose = process.env.CEED_VERBOSE,
_commit,
_maybe
} = {}) => {
return new Promise((resolve, reject) => {
const execTime = process.hrtime();
exec(command, execOptions, (err, stdout, stderr) => {
const code = err ? err.code : 0;
const cwd = execOptions.cwd || process.cwd();
const [seconds, ns] = process.hrtime(execTime);
const result = {
code,
error: err,
failed: code !== 0,
succeed: code === 0,
output: stdout,
value: stdout.trim(),
values: stdout.trim().split(/\n/gmi),
stderr
};
if (verbose) {
console.log(`[${command}] exited with ${code} in ${seconds + (ns / 10e9)}s`);
}
if (code === 0 || _maybe) {
resolve(result);
} else if (_commit) {
console.log(`Commit command failed with code ${code} in "${cwd}"`);
console.log(`Error message:`);
console.log(err.message);
throw new Error(err);
} else {
reject(result);
}
});
});
};
const commit = (command, execOptions, options) => {
return run(command, execOptions, Object.assign({}, options, {_commit: true}));
};
const maybe = (command, execOptions, options) => {
return run(command, execOptions, Object.assign({}, options, {_maybe: true}));
};
module.exports = {
run,
commit,
maybe
};