shell: when using shell use child_process.exec

arg splitting messes up arguments and debug output
This commit is contained in:
Girish Ramakrishnan
2024-02-28 18:34:03 +01:00
parent 03207f62ba
commit 07527fe2b1

View File

@@ -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) {