Skip to content
Myr

pipe.md

This file

Path

docs/pipe.md

177 lines · 4.8 KiB · Markdown

Last changed by

Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c

History of this file

Take it

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 1–37why, one step back
1<picture>
2 <source media="(prefers-color-scheme: dark)" srcset="../media/logo_dark.svg">3 <img alt="execa logo" src="../media/logo.svg" width="400">4</picture>5<br>67# 🔀 Piping multiple subprocesses89## Array syntax1011A subprocess' [output](output.md) can be [piped](https://en.wikipedia.org/wiki/Pipeline_(Unix)) to another subprocess' [input](input.md). The syntax is the same as [`execa(file, arguments?, options?)`](execution.md#array-syntax).1213```js14import {execa} from 'execa';1516// Similar to `npm run build | head -n 2` in shells17const {stdout} = await execa('npm', ['run', 'build'])18 .pipe('head', ['-n', '2']);19```2021## Template string syntax2223```js24const {stdout} = await execa`npm run build`25 .pipe`head -n 2`;26```2728## Advanced syntax2930```js31const subprocess = execa`head -n 2`;32const {stdout} = await execa`npm run build`33 .pipe(subprocess);34```3536## Options37

Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813

line 38why, one step back
38[Options](api.md#options-1) can be passed to either the source or the destination subprocess. Some [pipe-specific options](api.md#pipeoptions) can also be set by the destination subprocess.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 39–57why, one step back
39
40```js41const {stdout} = await execa('npm', ['run', 'build'], subprocessOptions)42 .pipe('head', ['-n', '2'], subprocessOrPipeOptions);43```4445```js46const {stdout} = await execa(subprocessOptions)`npm run build`47 .pipe(subprocessOrPipeOptions)`head -n 2`;48```4950```js51const subprocess = execa(subprocessOptions)`head -n 2`;52const {stdout} = await execa(subprocessOptions)`npm run build`53 .pipe(subprocess, pipeOptions);54```5556## Result57

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 58why, one step back
58When both subprocesses succeed, the [`result`](api.md#result) of the destination subprocess is returned. The [`result`](api.md#result) of the source subprocess is available in a [`result.pipedFrom`](api.md#resultpipedfrom) array.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 59–68why, one step back
59
60```js61const destinationResult = await execa`npm run build`62 .pipe`head -n 2`;63console.log(destinationResult.stdout); // First 2 lines of `npm run build`6465const sourceResult = destinationResult.pipedFrom[0];66console.log(sourceResult.stdout); // Full output of `npm run build`67```68

Expose the destination subprocess methods on `.pipe()` (#1252)Sindre Sorhusbefore adoptionc31c94c

lines 69–78why, one step back
69## Iterate, stream and IPC
7071Just like a regular subprocess, the value returned by `subprocess.pipe()` can be [iterated](lines.md#progressive-splitting) over its output lines, [converted to a readable stream](streams.md#converting-a-subprocess-to-a-stream), or used to exchange [IPC messages](ipc.md) with the destination subprocess.7273```js74for await (const line of execa`npm run build`.pipe`sort`) {75 console.log(line);76}77```78

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 79–80why, one step back
79## Errors
80

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 81why, one step back
81When either subprocess fails, `subprocess.pipe()` is rejected with that subprocess' error. If the destination subprocess fails, [`error.pipedFrom`](api.md#resultpipedfrom) includes the source subprocess' result, which is useful for debugging.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 82–131why, one step back
82
83```js84try {85 await execa`npm run build`86 .pipe`head -n 2`;87} catch (error) {88 if (error.pipedFrom.length === 0) {89 // `npm run build` failure90 console.error(error);91 } else {92 // `head -n 2` failure93 console.error(error);94 // `npm run build` output95 console.error(error.pipedFrom[0].stdout);96 }9798 throw error;99}100```101102## Series of subprocesses103104```js105await execa`npm run build`106 .pipe`sort`107 .pipe`head -n 2`;108```109110## 1 source, multiple destinations111112```js113const subprocess = execa`npm run build`;114const [sortedResult, truncatedResult] = await Promise.all([115 subprocess.pipe`sort`,116 subprocess.pipe`head -n 2`,117]);118```119120## Multiple sources, 1 destination121122```js123const destination = execa`./log-remotely.js`;124await Promise.all([125 execa`npm run build`.pipe(destination),126 execa`npm run test`.pipe(destination),127]);128```129130## Source file descriptor131

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 132why, one step back
132By default, the source's [`stdout`](api.md#subprocessstdout) is used, but this can be changed using the [`from`](api.md#pipeoptionsfrom) piping option.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 133–140why, one step back
133
134```js135await execa`npm run build`136 .pipe({from: 'stderr'})`head -n 2`;137```138139## Destination file descriptor140

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 141why, one step back
141By default, the destination's [`stdin`](api.md#subprocessstdin) is used, but this can be changed using the [`to`](api.md#pipeoptionsto) piping option.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 142–149why, one step back
142
143```js144await execa`npm run build`145 .pipe({to: 'fd3'})`./log-remotely.js`;146```147148## Unpipe149

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 150why, one step back
150Piping can be stopped using the [`unpipeSignal`](api.md#pipeoptionsunpipesignal) piping option.

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 151why, one step back
151

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 152why, one step back
152The [`subprocess.pipe()`](api.md#subprocesspipefile-arguments-options) method will be rejected with a cancelation error. However, each subprocess will keep running.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 153–177why, one step back
153
154```js155const abortController = new AbortController();156157process.on('SIGUSR1', () => {158 abortController.abort();159});160161// If the process receives SIGUSR1, `npm run build` stopped being logged remotely.162// However, it keeps running successfully.163try {164 await execa`npm run build`165 .pipe({unpipeSignal: abortController.signal})`./log-remotely.js`;166} catch (error) {167 if (!abortController.signal.aborted) {168 throw error;169 }170}171```172173<hr>174175[**Next**: ⏳️ Streams](streams.md)\176[**Previous**: 🧙 Transforms](transform.md)\177[**Top**: Table of contents](../readme.md#documentation)