Skip to content

Commit 1356262

Browse files
committed
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.
1 parent 03936ce commit 1356262

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

lib/run.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@ function runAsyncBase(cmd, args, {
1212
ignoreFailure = true,
1313
spawnArgs,
1414
input,
15-
captureStdout = false
15+
captureStdout = false,
16+
captureStderr = false
1617
} = {}) {
1718
if (cmd instanceof URL) {
1819
cmd = fileURLToPath(cmd);
1920
}
2021
let stdio = 'inherit';
2122
if (captureStdout || input != null) {
22-
stdio = [input == null ? 'inherit' : 'pipe', captureStdout ? 'pipe' : 'inherit', 'inherit'];
23+
stdio = [
24+
input == null ? 'inherit' : 'pipe',
25+
captureStdout ? 'pipe' : 'inherit',
26+
captureStderr ? 'pipe' : 'inherit'
27+
];
2328
}
2429
return new Promise((resolve, reject) => {
2530
const opt = Object.assign({
@@ -36,13 +41,22 @@ function runAsyncBase(cmd, args, {
3641
child.stdout.setEncoding('utf8');
3742
child.stdout.on('data', (chunk) => { stdout += chunk; });
3843
}
44+
let stderr;
45+
if (captureStderr) {
46+
stderr = '';
47+
child.stderr.setEncoding('utf8');
48+
child.stderr.on('data', (chunk) => { stderr += chunk; });
49+
}
3950
child.on('error', reject);
4051
child.on('close', (code) => {
4152
if (code !== 0) {
4253
if (ignoreFailure) {
4354
return reject(new Error(IGNORE));
4455
}
4556
const err = new Error(`${cmd} ${args} failed: ${code}`);
57+
if (stderr) {
58+
err.stderr = stderr;
59+
}
4660
err.code = code;
4761
err.messageOnly = true;
4862
return reject(err);

0 commit comments

Comments
 (0)