Skip to content
Myr

Improve process piping API (#757)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 192d818 · Browse files · 7 files+167 added−109 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+167 added−109 removed

  • tests:+75 added−28 removed
  • other:+92 added−81 removed

7 files+167 added−109 removed

modified:docs/scripts.md+3 added-3 removed
4 unmodified lines
5```js6import {$} from 'execa';78const {stdout: name} = await $`cat package.json`9 .pipeStdout($({stdin: 'pipe'})`grep name`);9 .pipe($({stdin: 'pipe'})`grep name`);10console.log(name);1112const branch = await $`git branch --show-current`;13await $`dep deploy --branch=${branch}`;
583 unmodified lines
597```598599```js600// Execa601await $`echo example`.pipeStdout($({stdin: 'pipe'})`cat`);601await $`echo example`.pipe($({stdin: 'pipe'})`cat`);602```603604### Piping stdout and stderr to another command605
12 unmodified lines
618```619620```js621// Execa622await $({all: true})`echo example`.pipeAll($({stdin: 'pipe'})`cat`);622await $({all: true})`echo example`.pipe($({stdin: 'pipe'})`cat`, 'all');623```624625### Piping stdout to a file626
50 unmodified lines
modified:lib/pipe.js+62 added-14 removed
modified:test/pipe.js+75 added-28 removed
modified:index.d.ts+9 added-21 removed
810 unmodified lines
811812 /**813 [Pipe](https://nodejs.org/api/stream.html#readablepipedestination-options) the child process' `stdout` to another Execa child process' `stdin`.814815 Returns `execaChildProcess`, which allows chaining `pipeStdout()` then `await`ing the final result.815 A `streamName` can be passed to pipe `"stderr"`, `"all"` (both `stdout` and `stderr`) or any another file descriptor instead of `stdout`.816817 `childProcess.stdout` must not be `undefined`.818 */819 pipeStdout?<Target extends ExecaChildProcess>(target: Target): Target;817 `childProcess.stdout` (and/or `childProcess.stderr` depending on `streamName`) must not be `undefined`. When `streamName` is `"all"`, the `all` option must be set to `true`.818821 /**822 Like `pipeStdout()` but piping the child process's `stderr` instead.823824 `childProcess.stderr` must not be `undefined`.819 Returns `execaChildProcess`, which allows chaining `.pipe()` then `await`ing the final result.820 */826 pipeStderr?<Target extends ExecaChildProcess>(target: Target): Target;827828 /**829 Combines both `pipeStdout()` and `pipeStderr()`.830831 The `all` option must be set to `true`.832 */833 pipeAll?<Target extends ExecaChildProcess>(target: Target): Target;821 pipe<Target extends ExecaChildProcess>(target: Target, streamName?: 'stdout' | 'stderr' | 'all' | number): Target;822};823824export type ExecaChildProcess<OptionsType extends Options = Options> = ChildProcess &825ExecaChildPromise<OptionsType> &
26 unmodified lines
852```853import {execa} from 'execa';854855// Similar to `echo unicorns > stdout.txt` in Bash868await execa('echo', ['unicorns']).pipeStdout('stdout.txt');856await execa('echo', ['unicorns'], {stdout: {file: 'stdout.txt'}});857858// Similar to `echo unicorns 2> stdout.txt` in Bash871await execa('echo', ['unicorns']).pipeStderr('stderr.txt');859await execa('echo', ['unicorns'], {stderr: {file: 'stderr.txt'}});860861// Similar to `echo unicorns &> stdout.txt` in Bash874await execa('echo', ['unicorns'], {all: true}).pipeAll('all.txt');862await execa('echo', ['unicorns'], {stdout: {file: 'all.txt'}, stderr: {file: 'all.txt'}});863```864865@example <caption>Redirect input from a file</caption>866```
8 unmodified lines
875@example <caption>Save and pipe output from a child process</caption>876```877import {execa} from 'execa';878891const {stdout} = await execa('echo', ['unicorns']).pipeStdout(process.stdout);879const {stdout} = await execa('echo', ['unicorns'], {stdout: ['pipe', 'inherit']});880// Prints `unicorns`881console.log(stdout);882// Also returns 'unicorns'883```
2 unmodified lines
886```887import {execa} from 'execa';888889// Similar to `echo unicorns | cat` in Bash902const {stdout} = await execa('echo', ['unicorns']).pipeStdout(execa('cat'));890const {stdout} = await execa('echo', ['unicorns']).pipe(execa('cat'));891console.log(stdout);892//=> 'unicorns'893```894
402 unmodified lines
modified:index.js+3 added-4 removed
9 unmodified lines
10import {handleInputAsync, pipeOutputAsync} from './lib/stdio/async.js';11import {handleInputSync, pipeOutputSync} from './lib/stdio/sync.js';12import {normalizeStdioNode} from './lib/stdio/normalize.js';13import {spawnedKill, validateTimeout, normalizeForceKillAfterDelay} from './lib/kill.js';14import {addPipeMethods} from './lib/pipe.js';14import {pipeToProcess} from './lib/pipe.js';15import {getSpawnedResult, makeAllStream} from './lib/stream.js';16import {mergePromise} from './lib/promise.js';17import {joinCommand, parseCommand, parseTemplates, getEscapedCommand} from './lib/command.js';18import {logCommand, verboseDefault} from './lib/verbose.js';
134 unmodified lines
153 const controller = new AbortController();154155 pipeOutputAsync(spawned, stdioStreamsGroups);156157 spawned.kill = spawnedKill.bind(null, spawned.kill.bind(spawned), options, controller);157 spawned.kill = spawnedKill.bind(undefined, spawned.kill.bind(spawned), options, controller);158 spawned.all = makeAllStream(spawned, options);159 spawned.pipe = pipeToProcess.bind(undefined, {spawned, stdioStreamsGroups, options});160160 addPipeMethods(spawned);161161 const promise = handlePromise({spawned, options, stdioStreamsGroups, command, escapedCommand, controller});162 mergePromise(spawned, promise);163 return spawned;164}
192 unmodified lines
modified:index.test-d.ts+9 added-20 removed
80 unmodified lines
8182 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!(execaPromise));87 expectAssignable<ExecaChildProcess>(execaPromise.pipeStdout!(execaBufferPromise));88 expectAssignable<ExecaChildProcess>(execaBufferPromise.pipeStdout!(execaPromise));89 expectAssignable<ExecaChildProcess>(execaBufferPromise.pipeStdout!(execaBufferPromise));85 expectType<typeof execaPromise>(execaBufferPromise.pipe(execaPromise));86 expectError(execaBufferPromise.pipe(writeStream));87 expectError(execaBufferPromise.pipe('output.txt'));88 await execaBufferPromise.pipe(execaPromise, 'stdout');89 await execaBufferPromise.pipe(execaPromise, 'stderr');90 await execaBufferPromise.pipe(execaPromise, 'all');91 await execaBufferPromise.pipe(execaPromise, 3);92 expectError(execaBufferPromise.pipe(execaPromise, 'other'));9391 expectAssignable<Function | undefined>(execaPromise.pipeStderr);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);98 expectAssignable<ExecaChildProcess>(execaPromise.pipeAll!(execaPromise));99 expectAssignable<ExecaChildProcess>(execaPromise.pipeAll!(execaBufferPromise));100 expectAssignable<ExecaChildProcess>(execaBufferPromise.pipeAll!(execaPromise));101 expectAssignable<ExecaChildProcess>(execaBufferPromise.pipeAll!(execaBufferPromise));10294 expectType<Readable>(execaPromise.all);95 const noAllPromise = execa('unicorns');96 expectType<undefined>(noAllPromise.all);97 const noAllResult = await noAllPromise;
443 unmodified lines
541 expectAssignable<ExecaSyncReturnValue>(unicornsResult);542 expectType<string>(unicornsResult.command);543 expectType<string>(unicornsResult.escapedCommand);544 expectType<number | undefined>(unicornsResult.exitCode);554 expectError(unicornsResult.pipeStdout);555 expectError(unicornsResult.pipeStderr);556 expectError(unicornsResult.pipeAll);545 expectError(unicornsResult.pipe);546 expectType<boolean>(unicornsResult.failed);547 expectType<boolean>(unicornsResult.timedOut);548 expectType<boolean>(unicornsResult.isCanceled);549 expectType<boolean>(unicornsResult.isTerminated);
832 unmodified lines
modified:readme.md+6 added-19 removed
181 unmodified lines
182```js183import {execa} from 'execa';184185// Similar to `echo unicorns | cat` in Bash186const {stdout} = await execa('echo', ['unicorns']).pipeStdout(execa('cat'));186const {stdout} = await execa('echo', ['unicorns']).pipe(execa('cat'));187console.log(stdout);188//=> 'unicorns'189```190
127 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(execaChildProcess)322#### pipe(execaChildProcess, streamName?)323324`execaChildProcess`: [`execa()` return value](#pipe-multiple-processes)325`streamName`: `"stdout"` (default), `"stderr"`, `"all"` or file descriptor index326327[Pipe](https://nodejs.org/api/stream.html#readablepipedestination-options) the child process' `stdout` to another Execa child process' `stdin`.328328Returns `execaChildProcess`, which allows chaining `pipeStdout()` then `await`ing the [final result](#childprocessresult).329A `streamName` can be passed to pipe `"stderr"`, `"all"` (both `stdout` and `stderr`) or any another file descriptor instead of `stdout`.330330[`childProcess.stdout`](#stdout) must not be `undefined`.331[`childProcess.stdout`](#stdout) (and/or [`childProcess.stderr`](#stderr) depending on `streamName`) must not be `undefined`. When `streamName` is `"all"`, the [`all` option](#all-2) must be set to `true`.332332#### pipeStderr(execaChildProcess)333Returns `execaChildProcess`, which allows chaining `.pipe()` then `await`ing the [final result](#childprocessresult).334334`execaChildProcess`: [`execa()` return value](#pipe-multiple-processes)335336Like [`pipeStdout()`](#pipestdoutexecachildprocess) but piping the child process's `stderr` instead.337338[`childProcess.stderr`](#stderr) must not be `undefined`.339340#### 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`.347335### childProcessResult336337Type: `object`338
608 unmodified lines