Skip to content

Commit

Permalink
chore: add captureStderr option to runAsyncBase
Browse files Browse the repository at this point in the history
Added a `captureStderr` option to `lib/run.js` `runAsyncBase` method
that allows for adding stderr information to thrown errors.
  • Loading branch information
ruyadorno committed Nov 25, 2024
1 parent 03936ce commit 01db083
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -36,13 +41,22 @@ 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) {
if (ignoreFailure) {
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);
Expand Down

0 comments on commit 01db083

Please sign in to comment.