Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
ruyadorno committed Dec 19, 2024
commit 1356262cc367f67982ad8dd118ded1827bb08c1f
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