Skip to content
Myr

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

5 files+63 added−5 removed

added:fixtures/noop-132+9 added
1#!/usr/bin/env node2'use strict';34process.stdout.write('1');5process.stderr.write('3');67setTimeout(() => {8 process.stdout.write('2');9}, 1000);
modified:index.js+33 added-1 removed
5 unmodified lines
6const stripFinalNewline = require('strip-final-newline');7const npmRunPath = require('npm-run-path');8const isStream = require('is-stream');9const _getStream = require('get-stream');10const mergeStream = require('merge-stream');11const pFinally = require('p-finally');12const onExit = require('signal-exit');13const errname = require('./lib/errname');14const stdio = require('./lib/stdio');
76 unmodified lines
91function handleShell(fn, command, options) {92 return fn(command, {...options, shell: true});93}9495function makeAllStream(spawned) {96 if (!spawned.stdout && !spawned.stderr) {97 return null;98 }99100 const mixed = mergeStream();101102 if (spawned.stdout) {103 mixed.add(spawned.stdout);104 }105106 if (spawned.stderr) {107 mixed.add(spawned.stderr);108 }109110 return mixed;111}112113function getStream(process, stream, {encoding, buffer, maxBuffer}) {114 if (!process[stream]) {115 return null;116 }
47 unmodified lines
164 error.signal = signal || null;165 error.cmd = joinedCommand;166 error.timedOut = Boolean(timedOut);167168 if ('all' in result) {169 error.all = result.all;170 }171172 return error;173}174175function getCode({error = {}}, code) {
109 unmodified lines
285286 if (spawned.stderr) {287 spawned.stderr.destroy();288 }289290 if (spawned.all) {291 spawned.all.destroy();292 }293 }294295 // 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];306307 if (result.error || result.code !== 0 || result.signal !== null) {308 const error = makeError(result, {309 joinedCommand,
15 unmodified lines
325326 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 lines
341 crossSpawn._enoent.hookChildProcess(spawned, parsed.parsed);342343 handleInput(spawned, parsed.options.input);344345 spawned.all = makeAllStream(spawned);346347 // 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 lines
modified:package.json+1 added
modified:readme.md+9 added-1 removed
10 unmodified lines
11- [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)161718## Install19
45 unmodified lines
65 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 lines
107Execute a file.108109Think of this as a mix of `child_process.execFile` and `child_process.spawn`.110109Returns 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.111Returns a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) which is enhanced to be a `Promise`.112113It exposes an additional `.all` stream, with `stdout` and `stderr` interleaved.114115The promise result is an `Object` with `stdout`, `stderr` and `all` properties.116117### execa.stdout(file, [arguments], [options])118119Same as `execa()`, but returns only `stdout`.120
14 unmodified lines
135Execute a file synchronously.136137Returns the same result object as [`child_process.spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options).138139It 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.140141This method throws an `Error` if the command fails.142143### execa.shellSync(file, [options])144
198 unmodified lines
modified:test.js+11 added-3 removed
44 unmodified lines
45 const stderr = await execa.stderr('noop-err', ['foo']);46 t.is(stderr, 'foo');47});4849test.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});5349test('stdout/stderr available on errors', async t => {54test('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});6061test('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 lines
239test('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 ]);246247 t.is(result.stdout, undefined);248 t.is(stdout, '.........\n');
2 unmodified lines
251test('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 ]);258259 t.is(result.stderr, undefined);260 t.is(stderr, '.........\n');
328 unmodified lines