UNPKG

1.19 kBJavaScriptView Raw
1'use strict';
2
3const processenv = require('processenv').default,
4 shell = require('shelljs');
5
6const errors = require('./errors'),
7 ui = require('./cli/ui');
8
9const exec = async function (command, options = {}) {
10 if (!command) {
11 throw new Error('Command is missing.');
12 }
13
14 const cwd = options.cwd || process.cwd(),
15 env = options.env || processenv(),
16 maxBuffer = options.maxBuffer || 1024 * 200,
17 silent = options.silent || false;
18
19 return new Promise((resolve, reject) => {
20 try {
21 const childProcess = shell.exec(command, { cwd, env, maxBuffer, silent: true }, (exitCode, stdout, stderr) => {
22 if (exitCode !== 0) {
23 const ex = new errors.ExecutableFailed(stderr);
24
25 ex.exitCode = exitCode;
26 ex.stdout = stdout;
27 ex.stderr = stderr;
28
29 return reject(ex);
30 }
31
32 resolve({ exitCode, stdout, stderr });
33 });
34
35 if (!silent) {
36 childProcess.stdout.on('data', data => ui.passThrough(data));
37 childProcess.stderr.on('data', data => ui.passThrough(data, { target: 'stderr' }));
38 }
39 } catch (ex) {
40 reject(ex);
41 }
42 });
43};
44
45module.exports = exec;