Add `.all` property with interleaved stdout and stderr (#171)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #1
authored by Tommaso Sotte · committed by Sindre Sorhus · parent 7d78db2 · Browse files · 5 files·+63 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
Diff stats
5 files+63 added−5 removed
- other:+63 added−5 removed
Provenance
- Commit
7f8d911- Tree
2d67169- Author
Tommaso Sotte <tommaso.sotte@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
7d78db2
5 files changed+63 added−5 removed
added:fixtures/noop-132+9 added
1+#!/usr/bin/env node2+'use strict';3+4+process.stdout.write('1');5+process.stderr.write('3');6+7+setTimeout(() => {8+ process.stdout.write('2');9+}, 1000);modified:index.js+33 added-1 removed
5 unmodified lines6 const stripFinalNewline = require('strip-final-newline');7 const npmRunPath = require('npm-run-path');8 const isStream = require('is-stream');9 const _getStream = require('get-stream');10+const mergeStream = require('merge-stream');11 const pFinally = require('p-finally');12 const onExit = require('signal-exit');13 const errname = require('./lib/errname');14 const stdio = require('./lib/stdio');76 unmodified lines91 function handleShell(fn, command, options) {92 return fn(command, {...options, shell: true});93 }94 95+function makeAllStream(spawned) {96+ if (!spawned.stdout && !spawned.stderr) {97+ return null;98+ }99+100+ const mixed = mergeStream();101+102+ if (spawned.stdout) {103+ mixed.add(spawned.stdout);104+ }105+106+ if (spawned.stderr) {107+ mixed.add(spawned.stderr);108+ }109+110+ return mixed;111+}112+113 function getStream(process, stream, {encoding, buffer, maxBuffer}) {114 if (!process[stream]) {115 return null;116 }47 unmodified lines164 error.signal = signal || null;165 error.cmd = joinedCommand;166 error.timedOut = Boolean(timedOut);167 168+ if ('all' in result) {169+ error.all = result.all;170+ }171+172 return error;173 }174 175 function getCode({error = {}}, code) {109 unmodified lines285 286 if (spawned.stderr) {287 spawned.stderr.destroy();288 }289+290+ if (spawned.all) {291+ spawned.all.destroy();292+ }293 }294 295 // TODO: Use native "finally" syntax when targeting Node.js 10296 const handlePromise = () => pFinally(Promise.all([297 processDone,298 getStream(spawned, 'stdout', {encoding, buffer, maxBuffer}),272- getStream(spawned, 'stderr', {encoding, buffer, maxBuffer})299+ getStream(spawned, 'stderr', {encoding, buffer, maxBuffer}),300+ getStream(spawned, 'all', {encoding, buffer, maxBuffer: maxBuffer * 2})301 ]).then(results => { // eslint-disable-line promise/prefer-await-to-then302 const result = results[0];303 result.stdout = results[1];304 result.stderr = results[2];305+ result.all = results[3];306 307 if (result.error || result.code !== 0 || result.signal !== null) {308 const error = makeError(result, {309 joinedCommand,15 unmodified lines325 326 return {327 stdout: handleOutput(parsed.options, result.stdout),328 stderr: handleOutput(parsed.options, result.stderr),329+ all: handleOutput(parsed.options, result.all),330 code: 0,331 exitCode: 0,332 exitCodeName: 'SUCCESS',333 failed: false,7 unmodified lines341 crossSpawn._enoent.hookChildProcess(spawned, parsed.parsed);342 343 handleInput(spawned, parsed.options.input);344 345+ spawned.all = makeAllStream(spawned);346+347 // eslint-disable-next-line promise/prefer-await-to-then348 spawned.then = (onFulfilled, onRejected) => handlePromise().then(onFulfilled, onRejected);349 spawned.catch = onRejected => handlePromise().catch(onRejected);350 60 unmodified linesmodified:package.json+1 added
38 unmodified lines39404142434445464719 unmodified lines38 unmodified lines "dependencies": { "cross-spawn": "^6.0.0", "get-stream": "^4.0.0", "is-stream": "^1.1.0", "merge-stream": "1.0.1", "npm-run-path": "^2.0.0", "p-finally": "^1.0.0", "signal-exit": "^3.0.0", "strip-final-newline": "^2.0.0"19 unmodified linesmodified:readme.md+9 added-1 removed
10 unmodified lines11 - [Improved Windows support.](https://github.com/IndigoUnited/node-cross-spawn#why)12 - Higher max buffer. 10 MB instead of 200 KB.13 - [Executes locally installed binaries by name.](#preferlocal)14 - [Cleans up spawned processes when the parent process dies.](#cleanup)15+- [Adds an `.all` property](#execafile-arguments-options) with interleaved output from `stdout` and `stderr`, similar to what the terminal sees. [*(Async only)*](#execasyncfile-arguments-options)16 17 18 ## Install19 45 unmodified lines65 exitCode: 3,66 exitCodeName: 'ESRCH',67 stdout: '',68 stderr: '',69+ all: '',70 failed: true,71 signal: null,72 cmd: 'exit 3',73 timedOut: false,33 unmodified lines107 Execute a file.108 109 Think of this as a mix of `child_process.execFile` and `child_process.spawn`.110 109-Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess), which is enhanced to also be a `Promise` for a result `Object` with `stdout` and `stderr` properties.111+Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) which is enhanced to be a `Promise`.112 113+It exposes an additional `.all` stream, with `stdout` and `stderr` interleaved.114+115+The promise result is an `Object` with `stdout`, `stderr` and `all` properties.116+117 ### execa.stdout(file, [arguments], [options])118 119 Same as `execa()`, but returns only `stdout`.120 14 unmodified lines135 Execute a file synchronously.136 137 Returns the same result object as [`child_process.spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options).138 139+It does not have the `.all` property that `execa()` has because the [underlying synchronous implementation](https://nodejs.org/api/child_process.html#child_process_child_process_execfilesync_file_args_options) only returns `stdout` and `stderr` at the end of the execution, so they cannot be interleaved.140+141 This method throws an `Error` if the command fails.142 143 ### execa.shellSync(file, [options])144 198 unmodified linesmodified:test.js+11 added-3 removed
44 unmodified lines45 const stderr = await execa.stderr('noop-err', ['foo']);46 t.is(stderr, 'foo');47 });48 49+test.serial('result.all shows both `stdout` and `stderr` intermixed', async t => {50+ const result = await execa('noop-132');51+ t.is(result.all, '132');52+});53+49-test('stdout/stderr available on errors', async t => {54+test('stdout/stderr/all available on errors', async t => {55 const err = await t.throwsAsync(execa('exit', ['2']), {message: getExitRegExp('2')});56 t.is(typeof err.stdout, 'string');57 t.is(typeof err.stderr, 'string');58+ t.is(typeof err.all, 'string');59 });60 61 test('include stdout and stderr in errors for improved debugging', async t => {62 await t.throwsAsync(execa('fixtures/error-message.js'), {message: STDERR_STDOUT_REGEXP, code: 1});176 unmodified lines239 test('do not buffer stdout when `buffer` set to `false`', async t => {240 const promise = execa('max-buffer', ['stdout', '10'], {buffer: false});241 const [result, stdout] = await Promise.all([242 promise,237- getStream(promise.stdout)243+ getStream(promise.stdout),244+ getStream(promise.all)245 ]);246 247 t.is(result.stdout, undefined);248 t.is(stdout, '.........\n');2 unmodified lines251 test('do not buffer stderr when `buffer` set to `false`', async t => {252 const promise = execa('max-buffer', ['stderr', '10'], {buffer: false});253 const [result, stderr] = await Promise.all([254 promise,248- getStream(promise.stderr)255+ getStream(promise.stderr),256+ getStream(promise.all)257 ]);258 259 t.is(result.stderr, undefined);260 t.is(stderr, '.........\n');328 unmodified lines