@kwsites/exec-p
Version:
Execute a command and retrieve data as a promise.
27 lines (26 loc) • 1.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const child_process_1 = require("child_process");
/**
* Spawns a child process to run the specified command. When there is a non-zero exit code, and there has been
* output to `stderr` the resulting promise will throw and must be caught by the caller.
*
* @returns {Promise<string>}
*/
function execP(command, params = [], options = {}) {
return new Promise((ok, fail) => {
const out = [];
const err = [];
const spawned = child_process_1.spawn(command, params, options);
spawned.stdout.on('data', data => out.push(data));
spawned.stderr.on('data', data => err.push(data));
spawned.on('error', e => err.push(new Buffer(String(e.stack), 'ascii')));
spawned.on('exit', (exitCode, exitSignal) => {
if (exitCode && err.length) {
return fail(new Error(Buffer.concat(err).toString('utf-8')));
}
ok(Buffer.concat(out).toString('utf-8'));
});
});
}
exports.execP = execP;