shell: remove usage of .spawn

This commit is contained in:
Girish Ramakrishnan
2024-02-21 13:09:59 +01:00
parent 62ca0487dc
commit 2237d2bbb7
5 changed files with 86 additions and 78 deletions

View File

@@ -8,12 +8,11 @@ const assert = require('assert'),
util = require('util');
exports = module.exports = {
spawn,
exec,
sudo,
promises: {
exec: util.promisify(exec),
execFile: util.promisify(execFile),
spawn: util.promisify(spawn),
sudo: util.promisify(sudo)
}
@@ -21,6 +20,7 @@ exports = module.exports = {
const SUDO = '/usr/bin/sudo';
// default encoding utf8, shell, handles input. full command
function exec(tag, cmd, options, callback) {
assert.strictEqual(typeof tag, 'string');
assert.strictEqual(typeof cmd, 'string');
@@ -29,6 +29,7 @@ function exec(tag, cmd, options, callback) {
debug(`${tag} exec: ${cmd}`);
// https://github.com/nodejs/node/issues/25231
const cp = child_process.exec(cmd, options, function (error, stdout, stderr) {
let e = null;
if (error) {
@@ -48,6 +49,36 @@ function exec(tag, cmd, options, callback) {
}
}
// no shell, utf8 encoding, separate args
function execFile(tag, file, args, options, callback) {
assert.strictEqual(typeof tag, 'string');
assert.strictEqual(typeof file, 'string');
assert(Array.isArray(args));
assert.strictEqual(typeof options, 'object');
assert.strictEqual(typeof callback, 'function');
debug(`${tag} exec: ${file}`);
// https://github.com/nodejs/node/issues/25231
const cp = child_process.execFile(file, args, options, function (error, stdout, stderr) {
let e = null;
if (error) {
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}: ${file} with args ${args.join(' ')} errored`, error);
}
callback(e, stdout);
});
if (options.input) {
cp.stdin.write(options.input);
cp.stdin.end();
}
}
// use this when you are afraid of how arguments will split up
function spawn(tag, file, args, options, callback) {
assert.strictEqual(typeof tag, 'string');