From 1356262cc367f67982ad8dd118ded1827bb08c1f Mon Sep 17 00:00:00 2001 From: Ruy Adorno Date: Sun, 24 Nov 2024 22:18:41 -0500 Subject: [PATCH] chore: add captureStderr option to runAsyncBase Added a `captureStderr` option to `lib/run.js` `runAsyncBase` method that allows for adding stderr information to thrown errors. --- lib/run.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/run.js b/lib/run.js index c9f87c70..7557dc83 100644 --- a/lib/run.js +++ b/lib/run.js @@ -12,14 +12,19 @@ function runAsyncBase(cmd, args, { ignoreFailure = true, spawnArgs, input, - captureStdout = false + captureStdout = false, + captureStderr = false } = {}) { if (cmd instanceof URL) { cmd = fileURLToPath(cmd); } let stdio = 'inherit'; if (captureStdout || input != null) { - stdio = [input == null ? 'inherit' : 'pipe', captureStdout ? 'pipe' : 'inherit', 'inherit']; + stdio = [ + input == null ? 'inherit' : 'pipe', + captureStdout ? 'pipe' : 'inherit', + captureStderr ? 'pipe' : 'inherit' + ]; } return new Promise((resolve, reject) => { const opt = Object.assign({ @@ -36,6 +41,12 @@ function runAsyncBase(cmd, args, { child.stdout.setEncoding('utf8'); child.stdout.on('data', (chunk) => { stdout += chunk; }); } + let stderr; + if (captureStderr) { + stderr = ''; + child.stderr.setEncoding('utf8'); + child.stderr.on('data', (chunk) => { stderr += chunk; }); + } child.on('error', reject); child.on('close', (code) => { if (code !== 0) { @@ -43,6 +54,9 @@ function runAsyncBase(cmd, args, { return reject(new Error(IGNORE)); } const err = new Error(`${cmd} ${args} failed: ${code}`); + if (stderr) { + err.stderr = stderr; + } err.code = code; err.messageOnly = true; return reject(err);