Add `all` option (#353)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tiago Nápoli · committed by Sindre Sorhus · parent bfef2c2 · Browse files · 8 files·+104 added−36 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: ehmicky <ehmicky@gmail.com>
Diff stats
8 files+104 added−36 removed
- tests:+58 added−13 removed
- other:+46 added−23 removed
Provenance
- Commit
56b749d- Tree
a4978eb- Author
Tiago Nápoli <napoli.tiago96@gmail.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
bfef2c2
8 files changed+104 added−36 removed
modified:lib/stream.js+3 added-12 removed
17 unmodified lines18192021222322232425262724 unmodified lines5253545556565758596061626364656667686061626334 unmodified lines17 unmodified lines }};
// `all` interleaves `stdout` and `stderr`const makeAllStream = spawned => { if (!spawned.stdout && !spawned.stderr) {const makeAllStream = (spawned, {all}) => { if (!all || (!spawned.stdout && !spawned.stderr)) { return; }
const mixed = mergeStream();24 unmodified lines }};
const getStreamPromise = (stream, {encoding, buffer, maxBuffer}) => { if (!stream) { if (!stream || !buffer) { return; }
if (!buffer) { // TODO: Use `ret = util.promisify(stream.finished)(stream);` when targeting Node.js 10 return new Promise((resolve, reject) => { stream .once('end', resolve) .once('error', reject); }); }
if (encoding) { return getStream(stream, {encoding, maxBuffer}); }
34 unmodified linesmodified:test/fixtures/max-buffer+2 added-2 removed
1 #!/usr/bin/env node2 'use strict';3-const output = process.argv[2];4-const bytes = Number(process.argv[3]);3+const output = process.argv[2] || 'stdout';4+const bytes = Number(process.argv[3] || 1e7);5 6 process[output].write('.'.repeat(bytes - 1) + '\n');modified:test/error.js+2 added-2 removed
8 unmodified lines9 10 const getExitRegExp = exitMessage => new RegExp(`failed with exit code ${exitMessage}`);11 12 test('stdout/stderr/all available on errors', async t => {13- const {stdout, stderr, all} = await t.throwsAsync(execa('exit', ['2']), {message: getExitRegExp('2')});13+ const {stdout, stderr, all} = await t.throwsAsync(execa('exit', ['2'], {all: true}), {message: getExitRegExp('2')});14 t.is(typeof stdout, 'string');15 t.is(typeof stderr, 'string');16 t.is(typeof all, 'string');17 });2 unmodified lines20 '\'wrong\' is not recognized as an internal or external command,\r\noperable program or batch file.' :21 '';22 23 test('stdout/stderr/all on process errors', async t => {24- const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command'));24+ const {stdout, stderr, all} = await t.throwsAsync(execa('wrong command', {all: true}));25 t.is(stdout, '');26 t.is(stderr, WRONG_COMMAND);27 t.is(all, WRONG_COMMAND);28 });137 unmodified linesmodified:test/stream.js+54 added-9 removed
25 unmodified lines26 t.is(fs.readFileSync(file, 'utf8'), 'foo bar\n');27 });28 29 test.serial('result.all shows both `stdout` and `stderr` intermixed', async t => {30- const {all} = await execa('noop-132');30+ const {all} = await execa('noop-132', {all: true});31 t.is(all, '132');32 });33 34+test('result.all is undefined unless opts.all is true', async t => {35+ const {all} = await execa('noop');36+ t.is(all, undefined);37+});38+39 test('stdout/stderr/all are undefined if ignored', async t => {35- const {stdout, stderr, all} = await execa('noop', {stdio: 'ignore'});40+ const {stdout, stderr, all} = await execa('noop', {stdio: 'ignore', all: true});41 t.is(stdout, undefined);42 t.is(stderr, undefined);43 t.is(all, undefined);44 });45 46 test('stdout/stderr/all are undefined if ignored in sync mode', t => {42- const {stdout, stderr, all} = execa.sync('noop', {stdio: 'ignore'});47+ const {stdout, stderr, all} = execa.sync('noop', {stdio: 'ignore', all: true});48 t.is(stdout, undefined);49 t.is(stderr, undefined);50 t.is(all, undefined);51 });50 unmodified lines102 });103 104 test('maxBuffer affects stdout', async t => {105 await t.notThrowsAsync(execa('max-buffer', ['stdout', '10'], {maxBuffer: 10}));101- const {stdout, all} = await t.throwsAsync(execa('max-buffer', ['stdout', '11'], {maxBuffer: 10}), /max-buffer stdout/);106+ const {stdout, all} = await t.throwsAsync(execa('max-buffer', ['stdout', '11'], {maxBuffer: 10, all: true}), /max-buffer stdout/);107 t.is(stdout, '.'.repeat(10));108 t.is(all, '.'.repeat(10));109 });110 111 test('maxBuffer affects stderr', async t => {112 await t.notThrowsAsync(execa('max-buffer', ['stderr', '10'], {maxBuffer: 10}));108- const {stderr, all} = await t.throwsAsync(execa('max-buffer', ['stderr', '11'], {maxBuffer: 10}), /max-buffer stderr/);113+ const {stderr, all} = await t.throwsAsync(execa('max-buffer', ['stderr', '11'], {maxBuffer: 10, all: true}), /max-buffer stderr/);114 t.is(stderr, '.'.repeat(10));115 t.is(all, '.'.repeat(10));116 });117 118 test('do not buffer stdout when `buffer` set to `false`', async t => {119 const promise = execa('max-buffer', ['stdout', '10'], {buffer: false});120 const [result, stdout] = await Promise.all([121 promise,117- getStream(promise.stdout),118- getStream(promise.all)122+ getStream(promise.stdout)123 ]);124 125 t.is(result.stdout, undefined);126 t.is(stdout, '.........\n');2 unmodified lines129 test('do not buffer stderr when `buffer` set to `false`', async t => {130 const promise = execa('max-buffer', ['stderr', '10'], {buffer: false});131 const [result, stderr] = await Promise.all([132 promise,129- getStream(promise.stderr),130- getStream(promise.all)133+ getStream(promise.stderr)134 ]);135 136 t.is(result.stderr, undefined);137 t.is(stderr, '.........\n');3 unmodified lines141 const {stdout} = execa('max-buffer', ['stdout', '21'], {maxBuffer: 10});142 const result = await getStream(stdout);143 t.is(result, '....................\n');144 });145+146+test('buffer: false > promise resolves', async t => {147+ await t.notThrowsAsync(execa('noop', {buffer: false}));148+});149+150+test('buffer: false > promise resolves when output is big but is not pipable', async t => {151+ await t.notThrowsAsync(execa('max-buffer', {buffer: false, stdout: 'ignore'}));152+});153+154+test('buffer: false > promise resolves when output is big and is read', async t => {155+ const cp = execa('max-buffer', {buffer: false});156+ cp.stdout.resume();157+ cp.stderr.resume();158+ await t.notThrowsAsync(cp);159+});160+161+test('buffer: false > promise resolves when output is big and "all" is used and is read', async t => {162+ const cp = execa('max-buffer', {buffer: false, all: true});163+ cp.all.resume();164+ await t.notThrowsAsync(cp);165+});166+167+test('buffer: false > promise rejects when process returns non-zero', async t => {168+ const cp = execa('fail', {buffer: false});169+ const {exitCode} = await t.throwsAsync(cp);170+ t.is(exitCode, 2);171+});172+173+const BUFFER_TIMEOUT = 1e3;174+175+test.serial('buffer: false > promise does not resolve when output is big and is not read', async t => {176+ const {timedOut} = await t.throwsAsync(execa('max-buffer', {buffer: false, timeout: BUFFER_TIMEOUT}));177+ t.true(timedOut);178+});179+180+test.serial('buffer: false > promise does not resolve when output is big and "all" is used but not read', async t => {181+ const cp = execa('max-buffer', {buffer: false, all: true, timeout: BUFFER_TIMEOUT});182+ cp.stdout.resume();183+ cp.stderr.resume();184+ const {timedOut} = await t.throwsAsync(cp);185+ t.true(timedOut);186+});modified:index.d.ts+21 added-4 removed
37 unmodified lines38 */39 readonly localDir?: string;40 41 /**42- Buffer the output from the spawned process. When buffering is disabled you must consume the output of the `stdout` and `stderr` streams because the promise will not be resolved/rejected until they have completed.42+ Buffer the output from the spawned process. When set to `false`, you must read the output of `stdout` and `stderr` (or `all` if the `all` option is `true`). Otherwise the returned promise will not be resolved/rejected.43 44 If the spawned process fails, `error.stdout`, `error.stderr`, and `error.all` will contain the buffered data.45 46 @default true28 unmodified lines75 */76 readonly reject?: boolean;77 78 /**79+ Add an `.all` property on the promise and the resolved value. The property contains the output of the process with `stdout` and `stderr` interleaved.80+81+ @default false82+ */83+ readonly all?: boolean;84+85+ /**86 Strip the final [newline character](https://en.wikipedia.org/wiki/Newline) from the output.87 88 @default true89 */181 unmodified lines271 interface ExecaReturnValue<StdoutErrorType = string>272 extends ExecaSyncReturnValue<StdoutErrorType> {273 /**274 The output of the process with `stdout` and `stderr` interleaved.275+276+ This is `undefined` if either:277+ - the `all` option is `false` (default value)278+ - `execa.sync()` was used279 */269- all: StdoutErrorType;280+ all?: StdoutErrorType;281 282 /**283 Whether the process was canceled.284 */12 unmodified lines297 interface ExecaError<StdoutErrorType = string>298 extends ExecaSyncError<StdoutErrorType> {299 /**300 The output of the process with `stdout` and `stderr` interleaved.301+302+ This is `undefined` if either:303+ - the `all` option is `false` (default value)304+ - `execa.sync()` was used305 */291- all: StdoutErrorType;306+ all?: StdoutErrorType;307 308 /**309 Whether the process was canceled.310 */28 unmodified lines339 340 /**341 Stream combining/interleaving [`stdout`](https://nodejs.org/api/child_process.html#child_process_subprocess_stdout) and [`stderr`](https://nodejs.org/api/child_process.html#child_process_subprocess_stderr).342 328- This is `undefined` when both `stdout` and `stderr` options are set to [`'pipe'`, `'ipc'`, `Stream` or `integer`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).343+ This is `undefined` if either:344+ - the `all` option is `false` (the default value)345+ - both `stdout` and `stderr` options are set to [`'inherit'`, `'ipc'`, `Stream` or `integer`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio)346 */347 all?: ReadableStream;348 }349 139 unmodified linesmodified:index.js+2 added-1 removed
38 unmodified lines39 localDir: options.cwd || process.cwd(),40 encoding: 'utf8',41 reject: true,42 cleanup: true,43+ all: false,44 ...options,45 windowsHide: true46 };47 102 unmodified lines150 crossSpawn._enoent.hookChildProcess(spawned, parsed.parsed);151 152 handleInput(spawned, parsed.options.input);153 153- spawned.all = makeAllStream(spawned);154+ spawned.all = makeAllStream(spawned, parsed.options);155 156 return mergePromise(spawned, handlePromiseOnce);157 };158 96 unmodified linesmodified:index.test-d.ts+3 added-2 removed
18 unmodified lines19 expectType<number>(unicornsResult.exitCode);20 expectType<string>(unicornsResult.exitCodeName);21 expectType<string>(unicornsResult.stdout);22 expectType<string>(unicornsResult.stderr);23- expectType<string>(unicornsResult.all);23+ expectType<string | undefined>(unicornsResult.all);24 expectType<boolean>(unicornsResult.failed);25 expectType<boolean>(unicornsResult.timedOut);26 expectType<boolean>(unicornsResult.isCanceled);27 expectType<boolean>(unicornsResult.killed);5 unmodified lines33 expectType<number>(execaError.exitCode);34 expectType<string>(execaError.exitCodeName);35 expectType<string>(execaError.stdout);36 expectType<string>(execaError.stderr);37- expectType<string>(execaError.all);37+ expectType<string | undefined>(execaError.all);38 expectType<boolean>(execaError.failed);39 expectType<boolean>(execaError.timedOut);40 expectType<boolean>(execaError.isCanceled);41 expectType<boolean>(execaError.killed);56 unmodified lines98 execa('unicorns', {stderr: 'inherit'});99 execa('unicorns', {stderr: process.stderr});100 execa('unicorns', {stderr: 1});101 execa('unicorns', {stderr: undefined});102+execa('unicorns', {all: true});103 execa('unicorns', {reject: false});104 execa('unicorns', {stripFinalNewline: false});105 execa('unicorns', {extendEnv: false});106 execa('unicorns', {cwd: '.'});78 unmodified linesmodified:readme.md+17 added-4 removed
159 unmodified lines160 Type: `ReadableStream | undefined`161 162 Stream combining/interleaving [`stdout`](https://nodejs.org/api/child_process.html#child_process_subprocess_stdout) and [`stderr`](https://nodejs.org/api/child_process.html#child_process_subprocess_stderr).163 164-This is `undefined` when both [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options are set to [`'pipe'`, `'ipc'`, `Stream` or `integer`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).164+This is `undefined` if either:165+ - the [`all` option](#all-2) is `false` (the default value)166+ - both [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options are set to [`'inherit'`, `'ipc'`, `Stream` or `integer`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio)167 168 ### execa.sync(file, [arguments], [options])169 170 Execute a file synchronously.60 unmodified lines231 The output of the process on stderr.232 233 #### all234 233-Type: `string | Buffer`235+Type: `string | Buffer | undefined`236 235-The output of the process on both stdout and stderr. `undefined` if `execa.sync()` was used.237+The output of the process with `stdout` and `stderr` interleaved.238 239+This is `undefined` if either:240+ - the [`all` option](#all-2) is `false` (the default value)241+ - `execa.sync()` was used242+243 #### failed244 245 Type: `boolean`246 55 unmodified lines302 303 Type: `boolean`<br>304 Default: `true`305 300-Buffer the output from the spawned process. When buffering is disabled you must consume the output of the `stdout` and `stderr` streams because the promise will not be resolved/rejected until they have completed.306+Buffer the output from the spawned process. When set to `false`, you must read the output of [`stdout`](#stdout-1) and [`stderr`](#stderr-1) (or [`all`](#all) if the [`all`](#all-2) option is `true`). Otherwise the returned promise will not be resolved/rejected.307 308 If the spawned process fails, [`error.stdout`](#stdout), [`error.stderr`](#stderr), and [`error.all`](#all) will contain the buffered data.309 310 #### input23 unmodified lines334 Default: `pipe`335 336 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).337 338+#### all339+340+Type: `boolean`<br>341+Default: `false`342+343+Add an `.all` property on the [promise](#all) and the [resolved value](#all-1). The property contains the output of the process with `stdout` and `stderr` interleaved.344+345 #### reject346 347 Type: `boolean`<br>348 Default: `true`196 unmodified lines