diff --git a/src/shell.js b/src/shell.js index e9306aa6c..9aae7e476 100644 --- a/src/shell.js +++ b/src/shell.js @@ -27,7 +27,7 @@ async function execArgs(tag, file, args, options) { assert(Array.isArray(args)); assert.strictEqual(typeof options, 'object'); - debug(`${tag} exec: ${file} ${JSON.stringify(args)}`); + debug(`${tag} execArgs: ${file} ${JSON.stringify(args)}`); const execOptions = Object.assign({ encoding: 'utf8', shell: false }, options); @@ -56,9 +56,31 @@ async function exec(tag, cmd, options) { assert.strictEqual(typeof cmd, 'string'); assert.strictEqual(typeof options, 'object'); - cmd = options.shell ? cmd : cmd.replace(/\s+/g, ' '); // collapse spaces when not using shell. note: no more complexity like parsing quotes here! - const [file, ...args] = cmd.split(' '); - return await execArgs(tag, file, args, options); + if (!options.shell) { + cmd = cmd.replace(/\s+/g, ' '); // collapse spaces when not using shell. note: no more complexity like parsing quotes here! + const [file, ...args] = cmd.split(' '); + return await execArgs(tag, file, args, options); + } + + debug(`${tag} exec: ${cmd}`); + + return new Promise((resolve, reject) => { + const cp = child_process.exec(cmd, options, function (error, stdout, stderr) { + if (!error) return resolve(stdout); + + const e = new BoxError(BoxError.SHELL_ERROR, `${tag} errored with code ${error.code} message ${error.message}`); + e.stdout = stdout; // when promisified, this is the way to get stdout + e.stderr = stderr; // when promisified, this is the way to get stderr + debug(`${tag}: ${cmd} errored`, error); + reject(e); + }); + + // https://github.com/nodejs/node/issues/25231 + if (options.input) { + cp.stdin.write(options.input); + cp.stdin.end(); + } + }); } function sudo(tag, args, options, callback) {