Skip to content
Myr

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

6 files+35 added−93 removed

modified:docs/scripts.md+1 added-1 removed
634 unmodified lines
635```636637```js638// Execa639await $`echo example`.pipeStdout('file.txt');639await $({stdout: {file: 'file.txt'}})`echo example`;640```641642### Piping stdin from a file643
33 unmodified lines
modified:lib/pipe.js+1 added-12 removed
modified:test/pipe.js+2 added-34 removed
modified:index.d.ts+8 added-14 removed
655 unmodified lines
656657 /**658 The output of the process on `stdout`.659660 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>;663664 /**665 The output of the process on `stderr`.666667 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>;670671 /**672 The output of the process on `stdin`, `stdout`, `stderr` and other file descriptors.673674 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>;677678 /**
130 unmodified lines
809 onRejected?: (reason: ExecaError<OptionsType>) => ResultType | PromiseLike<ResultType>810 ): Promise<ExecaReturnValue<OptionsType> | ResultType>;811812 /**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`.814818 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.816820 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>;820821 /**822 Like `pipeStdout()` but piping the child process's `stderr` instead.823828 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>;827828 /**829 Combines both `pipeStdout()` and `pipeStderr()`.830836 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};835836export type ExecaChildProcess<OptionsType extends Options = Options> = ChildProcess &837ExecaChildPromise<OptionsType> &
463 unmodified lines
modified:index.test-d.ts-12 removed
81 unmodified lines
82 const execaBufferPromise = execa('unicorns', {encoding: 'buffer', all: true});83 const writeStream = createWriteStream('output.txt');8485 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));9091 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));9697 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 lines
modified:readme.md+23 added-20 removed
144 unmodified lines
145```js146import {execa} from 'execa';147148// Similar to `echo unicorns > stdout.txt` in Bash149await execa('echo', ['unicorns']).pipeStdout('stdout.txt');149await execa('echo', ['unicorns'], {stdout: {file: 'stdout.txt'}});150151// Similar to `echo unicorns 2> stdout.txt` in Bash152await execa('echo', ['unicorns']).pipeStderr('stderr.txt');152await execa('echo', ['unicorns'], {stderr: {file: 'stderr.txt'}});153154// Similar to `echo unicorns &> stdout.txt` in Bash155await execa('echo', ['unicorns'], {all: true}).pipeAll('all.txt');155await execa('echo', ['unicorns'], {stdout: {file: 'all.txt'}, stderr: {file: 'all.txt'}});156```157158#### Redirect input from a file159
10 unmodified lines
170171```js172import {execa} from 'execa';173174const {stdout} = await execa('echo', ['unicorns']).pipeStdout(process.stdout);174const {stdout} = await execa('echo', ['unicorns'], {stdout: ['pipe', 'inherit']});175// Prints `unicorns`176console.log(stdout);177// Also returns 'unicorns'178```
139 unmodified lines
318This 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)321322#### pipeStdout(target)322#### pipeStdout(execaChildProcess)323324[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)325329If 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`.327331The [`stdout` option](#stdout-1) must be kept as `pipe`, its default value.328Returns `execaChildProcess`, which allows chaining `pipeStdout()` then `await`ing the [final result](#childprocessresult).329333#### pipeStderr(target)330[`childProcess.stdout`](#stdout) must not be `undefined`.331335Like [`pipeStdout()`](#pipestdouttarget) but piping the child process's `stderr` instead.332#### pipeStderr(execaChildProcess)333337The [`stderr` option](#stderr-1) must be kept as `pipe`, its default value.334`execaChildProcess`: [`execa()` return value](#pipe-multiple-processes)335339#### pipeAll(target)336Like [`pipeStdout()`](#pipestdoutexecachildprocess) but piping the child process's `stderr` instead.337341Combines both [`pipeStdout()`](#pipestdouttarget) and [`pipeStderr()`](#pipestderrtarget).338[`childProcess.stderr`](#stderr) must not be `undefined`.339343Either 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)341342`execaChildProcess`: [`execa()` return value](#pipe-multiple-processes)343344Combines both [`pipeStdout()`](#pipestdoutexecachildprocess) and [`pipeStderr()`](#pipestderrexecachildprocess).345346The [`all` option](#all-2) must be set to `true`.347348### childProcessResult349350Type: `object`351
36 unmodified lines
388Type: `string | Uint8Array | string[] | Uint8Array[] | unknown[] | undefined`389390The output of the process on `stdout`.391389This 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).392This 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).393394#### stderr395396Type: `string | Uint8Array | string[] | Uint8Array[] | unknown[] | undefined`397398The output of the process on `stderr`.399397This 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).400This 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).401402#### all403404Type: `string | Uint8Array | string[] | Uint8Array[] | unknown[] | undefined`
1 unmodified line
406The output of the process with `stdout` and `stderr` [interleaved](#ensuring-all-output-is-interleaved).407408This 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)411412This 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).413414#### stdio
545 unmodified lines