-
Notifications
You must be signed in to change notification settings - Fork 6
/
runCommand.js
46 lines (36 loc) · 1.22 KB
/
runCommand.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
'use strict';
const { processenv } = require('processenv'),
shell = require('shelljs');
const errors = require('./errors'),
ui = require('./cli/ui');
const runCommand = async function (command, options = {}) {
if (!command) {
throw new Error('Command is missing.');
}
const cwd = options.cwd || process.cwd(),
env = options.env || processenv(),
maxBuffer = options.maxBuffer || 1024 * 200,
silent = options.silent || false;
return new Promise((resolve, reject) => {
try {
const childProcess = shell.exec(command, { cwd, env, maxBuffer, silent: true }, (exitCode, stdout, stderr) => {
if (exitCode !== 0) {
const ex = new errors.ExecutableFailed(stderr);
ex.command = command;
ex.exitCode = exitCode;
ex.stdout = stdout;
ex.stderr = stderr;
return reject(ex);
}
resolve({ exitCode, stdout, stderr });
});
if (!silent) {
childProcess.stdout.on('data', data => ui.passThrough(` ${data}`));
childProcess.stderr.on('data', data => ui.passThrough(` ${data}`, { target: 'stderr' }));
}
} catch (ex) {
reject(ex);
}
});
};
module.exports = runCommand;