Skip to content
Myr

pipe.md

This file

Path

docs/pipe.md

167 lines · 4.3 KiB · Markdown

Last changed by

Improve documentation of Execa methods (#1115)ehmickybefore adoptionca2e813

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–70why, 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```6869## Errors70

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

line 71why, one step back
71When 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 72–121why, one step back
72
73```js74try {75 await execa`npm run build`76 .pipe`head -n 2`;77} catch (error) {78 if (error.pipedFrom.length === 0) {79 // `npm run build` failure80 console.error(error);81 } else {82 // `head -n 2` failure83 console.error(error);84 // `npm run build` output85 console.error(error.pipedFrom[0].stdout);86 }8788 throw error;89}90```9192## Series of subprocesses9394```js95await execa`npm run build`96 .pipe`sort`97 .pipe`head -n 2`;98```99100## 1 source, multiple destinations101102```js103const subprocess = execa`npm run build`;104const [sortedResult, truncatedResult] = await Promise.all([105 subprocess.pipe`sort`,106 subprocess.pipe`head -n 2`,107]);108```109110## Multiple sources, 1 destination111112```js113const destination = execa`./log-remotely.js`;114await Promise.all([115 execa`npm run build`.pipe(destination),116 execa`npm run test`.pipe(destination),117]);118```119120## Source file descriptor121

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

line 122why, one step back
122By 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 123–130why, one step back
123
124```js125await execa`npm run build`126 .pipe({from: 'stderr'})`head -n 2`;127```128129## Destination file descriptor130

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

line 131why, one step back
131By 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 132–139why, one step back
132
133```js134await execa`npm run build`135 .pipe({to: 'fd3'})`./log-remotely.js`;136```137138## Unpipe139

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

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

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 141why, one step back
141

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

line 142why, one step back
142The [`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 143–167why, one step back
143
144```js145const abortController = new AbortController();146147process.on('SIGUSR1', () => {148 abortController.abort();149});150151// If the process receives SIGUSR1, `npm run build` stopped being logged remotely.152// However, it keeps running successfully.153try {154 await execa`npm run build`155 .pipe({unpipeSignal: abortController.signal})`./log-remotely.js`;156} catch (error) {157 if (!abortController.signal.aborted) {158 throw error;159 }160}161```162163<hr>164165[**Next**: ⏳️ Streams](streams.md)\166[**Previous**: 🧙 Transforms](transform.md)\167[**Top**: Table of contents](../readme.md#documentation)