Only allow child processes as argument to `.pipeStdout()` (#752)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent fd00294 · Browse files · 6 files·+35 added−93 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+35 added−93 removed
- tests:+2 added−34 removed
- other:+33 added−59 removed
Provenance
- Commit
d16c305- Tree
23113b9- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
fd00294
6 files changed+35 added−93 removed
modified:docs/scripts.md+1 added-1 removed
634 unmodified lines635 ```636 637 ```js638 // Execa639-await $`echo example`.pipeStdout('file.txt');639+await $({stdout: {file: 'file.txt'}})`echo example`;640 ```641 642 ### Piping stdin from a file643 33 unmodified linesmodified:lib/pipe.js+1 added-12 removed
11234568910111213141516177198910111219 unmodified linesimport {createWriteStream} from 'node:fs';import {ChildProcess} from 'node:child_process';import {isWritableStream} from 'is-stream';
const isExecaChildProcess = target => target instanceof ChildProcess && typeof target.then === 'function';
const pipeToTarget = (spawned, streamName, target) => { if (typeof target === 'string') { spawned[streamName].pipe(createWriteStream(target)); return spawned; }
if (isWritableStream(target)) { spawned[streamName].pipe(target); return spawned; }
if (!isExecaChildProcess(target)) { throw new TypeError('The second argument must be a string, a stream or an Execa child process.'); throw new TypeError('The second argument must be an Execa child process.'); }
if (!isWritableStream(target.stdin)) { throw new TypeError('The target child process\'s stdin must be available.');19 unmodified linesmodified:test/pipe.js+2 added-34 removed
112343645677 unmodified lines151617182223242526272829303132333435363738394041424344454647192050212223245455562526272820 unmodified linesimport {PassThrough, Readable} from 'node:stream';import {PassThrough} from 'node:stream';import {spawn} from 'node:child_process';import {readFile, rm} from 'node:fs/promises';import tempfile from 'tempfile';import test from 'ava';import getStream from 'get-stream';import {execa} from '../index.js';import {setFixtureDir} from './helpers/fixtures-dir.js';
setFixtureDir();7 unmodified linestest('pipeStderr() can pipe to Execa child processes', pipeToProcess, 2, 'pipeStderr');test('pipeAll() can pipe stdout to Execa child processes', pipeToProcess, 1, 'pipeAll');test('pipeAll() can pipe stderr to Execa child processes', pipeToProcess, 2, 'pipeAll');
const pipeToStream = async (t, index, funcName) => { const stream = new PassThrough(); const result = await execa('noop-fd.js', [`${index}`, 'test'], {all: true})[funcName](stream); t.is(result.stdio[index], 'test'); t.is(await getStream(stream), 'test');};
test('pipeStdout() can pipe to streams', pipeToStream, 1, 'pipeStdout');test('pipeStderr() can pipe to streams', pipeToStream, 2, 'pipeStderr');test('pipeAll() can pipe stdout to streams', pipeToStream, 1, 'pipeAll');test('pipeAll() can pipe stderr to streams', pipeToStream, 2, 'pipeAll');
const pipeToFile = async (t, index, funcName) => { const file = tempfile(); const result = await execa('noop-fd.js', [`${index}`, 'test'], {all: true})[funcName](file); t.is(result.stdio[index], 'test'); t.is(await readFile(file, 'utf8'), 'test'); await rm(file);};
// `test.serial()` is due to a race condition: `execa(...).pipe*(file)` might resolve before the file stream has resolvedtest.serial('pipeStdout() can pipe to files', pipeToFile, 1, 'pipeStdout');test.serial('pipeStderr() can pipe to files', pipeToFile, 2, 'pipeStderr');test.serial('pipeAll() can pipe stdout to files', pipeToFile, 1, 'pipeAll');test.serial('pipeAll() can pipe stderr to files', pipeToFile, 2, 'pipeAll');
const invalidTarget = (t, funcName, getTarget) => { t.throws(() => execa('empty.js', {all: true})[funcName](getTarget()), { message: /a stream or an Execa child process/, message: /an Execa child process/, });};
test('pipeStdout() can only pipe to writable streams', invalidTarget, 'pipeStdout', () => new Readable());test('pipeStderr() can only pipe to writable streams', invalidTarget, 'pipeStderr', () => new Readable());test('pipeAll() can only pipe to writable streams', invalidTarget, 'pipeAll', () => new Readable());test('pipeStdout() cannot pipe to non-processes', invalidTarget, 'pipeStdout', () => ({stdin: new PassThrough()}));test('pipeStderr() cannot pipe to non-processes', invalidTarget, 'pipeStderr', () => ({stdin: new PassThrough()}));test('pipeAll() cannot pipe to non-processes', invalidTarget, 'pipeStderr', () => ({stdin: new PassThrough()}));test('pipeStdout() cannot pipe to non-Execa processes', invalidTarget, 'pipeStdout', () => spawn('node', ['--version']));20 unmodified linesmodified:index.d.ts+8 added-14 removed
655 unmodified lines656 657 /**658 The output of the process on `stdout`.659 660- This is `undefined` if the `stdout` option is set to [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). This is an array if the `lines` option is `true, or if the `stdout` option is a transform in object mode.660+ This is `undefined` if the `stdout` option is set to only [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). This is an array if the `lines` option is `true, or if the `stdout` option is a transform in object mode.661 */662 stdout: StdioOutput<'1', OptionsType>;663 664 /**665 The output of the process on `stderr`.666 667- This is `undefined` if the `stderr` option is set to [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). This is an array if the `lines` option is `true, or if the `stderr` option is a transform in object mode.667+ This is `undefined` if the `stderr` option is set to only [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). This is an array if the `lines` option is `true, or if the `stderr` option is a transform in object mode.668 */669 stderr: StdioOutput<'2', OptionsType>;670 671 /**672 The output of the process on `stdin`, `stdout`, `stderr` and other file descriptors.673 674- Items are `undefined` when their corresponding `stdio` option is set to [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). Items are arrays when their corresponding `stdio` option is a transform in object mode.674+ Items are `undefined` when their corresponding `stdio` option is set to only [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). Items are arrays when their corresponding `stdio` option is a transform in object mode.675 */676 stdio: StdioArrayOutput<OptionsType>;677 678 /**130 unmodified lines809 onRejected?: (reason: ExecaError<OptionsType>) => ResultType | PromiseLike<ResultType>810 ): Promise<ExecaReturnValue<OptionsType> | ResultType>;811 812 /**813- [Pipe](https://nodejs.org/api/stream.html#readablepipedestination-options) the child process's `stdout` to `target`, which can be:814- - Another `execa()` return value815- - A writable stream816- - A file path string813+ [Pipe](https://nodejs.org/api/stream.html#readablepipedestination-options) the child process' `stdout` to another Execa child process' `stdin`.814 818- If the `target` is another `execa()` return value, it is returned. Otherwise, the original `execa()` return value is returned. This allows chaining `pipeStdout()` then `await`ing the final result.815+ Returns `execaChildProcess`, which allows chaining `pipeStdout()` then `await`ing the final result.816 820- The `stdout` option] must be kept as `pipe`, its default value.817+ `childProcess.stdout` must not be `undefined`.818 */819 pipeStdout?<Target extends ExecaChildProcess>(target: Target): Target;823- pipeStdout?(target: Writable | string): ExecaChildProcess<OptionsType>;820 821 /**822 Like `pipeStdout()` but piping the child process's `stderr` instead.823 828- The `stderr` option must be kept as `pipe`, its default value.824+ `childProcess.stderr` must not be `undefined`.825 */826 pipeStderr?<Target extends ExecaChildProcess>(target: Target): Target;831- pipeStderr?(target: Writable | string): ExecaChildProcess<OptionsType>;827 828 /**829 Combines both `pipeStdout()` and `pipeStderr()`.830 836- Either the `stdout` option or the `stderr` option must be kept as `pipe`, their default value. Also, the `all` option must be set to `true`.831+ The `all` option must be set to `true`.832 */833 pipeAll?<Target extends ExecaChildProcess>(target: Target): Target;839- pipeAll?(target: Writable | string): ExecaChildProcess<OptionsType>;834 };835 836 export type ExecaChildProcess<OptionsType extends Options = Options> = ChildProcess &837 ExecaChildPromise<OptionsType> &463 unmodified linesmodified:index.test-d.ts-12 removed
81 unmodified lines82 const execaBufferPromise = execa('unicorns', {encoding: 'buffer', all: true});83 const writeStream = createWriteStream('output.txt');84 85 expectAssignable<Function | undefined>(execaPromise.pipeStdout);86- expectAssignable<ExecaChildProcess>(execaPromise.pipeStdout!('file.txt'));87- expectAssignable<ExecaChildProcess>(execaBufferPromise.pipeStdout!('file.txt'));88- expectAssignable<ExecaChildProcess>(execaPromise.pipeStdout!(writeStream));89- expectAssignable<ExecaChildProcess>(execaBufferPromise.pipeStdout!(writeStream));86 expectAssignable<ExecaChildProcess>(execaPromise.pipeStdout!(execaPromise));87 expectAssignable<ExecaChildProcess>(execaPromise.pipeStdout!(execaBufferPromise));88 expectAssignable<ExecaChildProcess>(execaBufferPromise.pipeStdout!(execaPromise));89 expectAssignable<ExecaChildProcess>(execaBufferPromise.pipeStdout!(execaBufferPromise));90 91 expectAssignable<Function | undefined>(execaPromise.pipeStderr);96- expectAssignable<ExecaChildProcess>(execaPromise.pipeStderr!('file.txt'));97- expectAssignable<ExecaChildProcess>(execaBufferPromise.pipeStderr!('file.txt'));98- expectAssignable<ExecaChildProcess>(execaPromise.pipeStderr!(writeStream));99- expectAssignable<ExecaChildProcess>(execaBufferPromise.pipeStderr!(writeStream));92 expectAssignable<ExecaChildProcess>(execaPromise.pipeStderr!(execaPromise));93 expectAssignable<ExecaChildProcess>(execaPromise.pipeStderr!(execaBufferPromise));94 expectAssignable<ExecaChildProcess>(execaBufferPromise.pipeStderr!(execaPromise));95 expectAssignable<ExecaChildProcess>(execaBufferPromise.pipeStderr!(execaBufferPromise));96 97 expectAssignable<Function | undefined>(execaPromise.pipeAll);106- expectAssignable<ExecaChildProcess>(execaPromise.pipeAll!('file.txt'));107- expectAssignable<ExecaChildProcess>(execaBufferPromise.pipeAll!('file.txt'));108- expectAssignable<ExecaChildProcess>(execaPromise.pipeAll!(writeStream));109- expectAssignable<ExecaChildProcess>(execaBufferPromise.pipeAll!(writeStream));98 expectAssignable<ExecaChildProcess>(execaPromise.pipeAll!(execaPromise));99 expectAssignable<ExecaChildProcess>(execaPromise.pipeAll!(execaBufferPromise));100 expectAssignable<ExecaChildProcess>(execaBufferPromise.pipeAll!(execaPromise));101 expectAssignable<ExecaChildProcess>(execaBufferPromise.pipeAll!(execaBufferPromise));1291 unmodified linesmodified:readme.md+23 added-20 removed
144 unmodified lines145 ```js146 import {execa} from 'execa';147 148 // Similar to `echo unicorns > stdout.txt` in Bash149-await execa('echo', ['unicorns']).pipeStdout('stdout.txt');149+await execa('echo', ['unicorns'], {stdout: {file: 'stdout.txt'}});150 151 // Similar to `echo unicorns 2> stdout.txt` in Bash152-await execa('echo', ['unicorns']).pipeStderr('stderr.txt');152+await execa('echo', ['unicorns'], {stderr: {file: 'stderr.txt'}});153 154 // Similar to `echo unicorns &> stdout.txt` in Bash155-await execa('echo', ['unicorns'], {all: true}).pipeAll('all.txt');155+await execa('echo', ['unicorns'], {stdout: {file: 'all.txt'}, stderr: {file: 'all.txt'}});156 ```157 158 #### Redirect input from a file159 10 unmodified lines170 171 ```js172 import {execa} from 'execa';173 174-const {stdout} = await execa('echo', ['unicorns']).pipeStdout(process.stdout);174+const {stdout} = await execa('echo', ['unicorns'], {stdout: ['pipe', 'inherit']});175 // Prints `unicorns`176 console.log(stdout);177 // Also returns 'unicorns'178 ```139 unmodified lines318 This is `undefined` if either:319 - the [`all` option](#all-2) is `false` (the default value)320 - both [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options are set to [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio)321 322-#### pipeStdout(target)322+#### pipeStdout(execaChildProcess)323 324-[Pipe](https://nodejs.org/api/stream.html#readablepipedestination-options) the child process's `stdout` to `target`, which can be:325-- Another [`execa()` return value](#pipe-multiple-processes)324+`execaChildProcess`: [`execa()` return value](#pipe-multiple-processes)326-- A [writable stream](#save-and-pipe-output-from-a-child-process)327-- A [file path string](#redirect-output-to-a-file)325 329-If the `target` is another [`execa()` return value](#execacommandcommand-options), it is returned. Otherwise, the original `execa()` return value is returned. This allows chaining `pipeStdout()` then `await`ing the [final result](#childprocessresult).326+[Pipe](https://nodejs.org/api/stream.html#readablepipedestination-options) the child process' `stdout` to another Execa child process' `stdin`.327 331-The [`stdout` option](#stdout-1) must be kept as `pipe`, its default value.328+Returns `execaChildProcess`, which allows chaining `pipeStdout()` then `await`ing the [final result](#childprocessresult).329 333-#### pipeStderr(target)330+[`childProcess.stdout`](#stdout) must not be `undefined`.331 335-Like [`pipeStdout()`](#pipestdouttarget) but piping the child process's `stderr` instead.332+#### pipeStderr(execaChildProcess)333 337-The [`stderr` option](#stderr-1) must be kept as `pipe`, its default value.334+`execaChildProcess`: [`execa()` return value](#pipe-multiple-processes)335 339-#### pipeAll(target)336+Like [`pipeStdout()`](#pipestdoutexecachildprocess) but piping the child process's `stderr` instead.337 341-Combines both [`pipeStdout()`](#pipestdouttarget) and [`pipeStderr()`](#pipestderrtarget).338+[`childProcess.stderr`](#stderr) must not be `undefined`.339 343-Either the [`stdout` option](#stdout-1) or the [`stderr` option](#stderr-1) must be kept as `pipe`, their default value. Also, the [`all` option](#all-2) must be set to `true`.340+#### pipeAll(execaChildProcess)341 342+`execaChildProcess`: [`execa()` return value](#pipe-multiple-processes)343+344+Combines both [`pipeStdout()`](#pipestdoutexecachildprocess) and [`pipeStderr()`](#pipestderrexecachildprocess).345+346+The [`all` option](#all-2) must be set to `true`.347+348 ### childProcessResult349 350 Type: `object`351 36 unmodified lines388 Type: `string | Uint8Array | string[] | Uint8Array[] | unknown[] | undefined`389 390 The output of the process on `stdout`.391 389-This is `undefined` if the [`stdout`](#stdout-1) option is set to [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). This is an array if the [`lines` option](#lines) is `true, or if the `stdout` option is a [transform in object mode](docs/transform.md#object-mode).392+This is `undefined` if the [`stdout`](#stdout-1) option is set to only [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). This is an array if the [`lines` option](#lines) is `true, or if the `stdout` option is a [transform in object mode](docs/transform.md#object-mode).393 394 #### stderr395 396 Type: `string | Uint8Array | string[] | Uint8Array[] | unknown[] | undefined`397 398 The output of the process on `stderr`.399 397-This is `undefined` if the [`stderr`](#stderr-1) option is set to [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). This is an array if the [`lines` option](#lines) is `true, or if the `stderr` option is a [transform in object mode](docs/transform.md#object-mode).400+This is `undefined` if the [`stderr`](#stderr-1) option is set to only [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). This is an array if the [`lines` option](#lines) is `true, or if the `stderr` option is a [transform in object mode](docs/transform.md#object-mode).401 402 #### all403 404 Type: `string | Uint8Array | string[] | Uint8Array[] | unknown[] | undefined`1 unmodified line406 The output of the process with `stdout` and `stderr` [interleaved](#ensuring-all-output-is-interleaved).407 408 This is `undefined` if either:409 - the [`all` option](#all-2) is `false` (the default value)407-- both [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options are set to [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio)410+- both [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options are set to only [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio)411 412 This is an array if the [`lines` option](#lines) is `true, or if either the `stdout` or `stderr` option is a [transform in object mode](docs/transform.md#object-mode).413 414 #### stdio545 unmodified lines