Skip to content
Myr

transform.md

This file

Path

docs/transform.md

157 lines · 5.4 KiB · Markdown

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 2cd41ef point at it.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 1–5why, 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>

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 6why, one step back
6

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 7–8why, one step back
7# 🧙 Transforms
8

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 9–10why, one step back
9## Summary
10

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

line 11why, one step back
11Transforms map or filter the input or output of a subprocess. They are defined by passing a [generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*) or a [transform options object](api.md#transform-options) to the [`stdin`](api.md#optionsstdin), [`stdout`](api.md#optionsstdout), [`stderr`](api.md#optionsstderr) or [`stdio`](api.md#optionsstdio) option. It can be [`async`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function*).

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 12–20why, one step back
12
13```js14import {execa} from 'execa';1516const transform = function * (line) {17 const prefix = line.includes('error') ? 'ERROR' : 'INFO';18 yield `${prefix}: ${line}`;19};20

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 21why, one step back
21const {stdout} = await execa({stdout: transform})`npm run build`;

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 22–24why, one step back
22console.log(stdout); // HELLO
23```24

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 25why, one step back
25## Difference with iteration

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 26why, one step back
26

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 27why, one step back
27Transforms operate one `line` at a time, just like [`subprocess.iterable()`](lines.md#progressive-splitting). However, unlike iteration, transforms:

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

line 28why, one step back
28- Modify the subprocess' [output](api.md#resultstdout) and [streams](api.md#subprocessstdout).

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 29–30why, one step back
29- Can apply to the subprocess' input.
30- Are defined using a [generator function](#summary), [`Duplex`](#duplextransform-streams) stream, Node.js [`Transform`](#duplextransform-streams) stream or web [`TransformStream`](#duplextransform-streams).

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 31–42why, one step back
31
32## Filtering3334`yield` can be called 0, 1 or multiple times. Not calling `yield` enables filtering a specific line.3536```js37const transform = function * (line) {38 if (!line.includes('secret')) {39 yield line;40 }41};42

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 43why, one step back
43const {stdout} = await execa({stdout: transform})`echo ${'This is a secret'}`;

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 44–48why, one step back
44console.log(stdout); // ''
45```4647## Object mode48

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

line 49why, one step back
49By default, [`stdout`](api.md#optionsstdout) and [`stderr`](api.md#optionsstderr)'s transforms must return a string or an `Uint8Array`. However, if the [`objectMode`](api.md#transformoptionsobjectmode) transform option is `true`, any type can be returned instead, except `null` or `undefined`. The subprocess' [`result.stdout`](api.md#resultstdout)/[`result.stderr`](api.md#resultstderr) will be an array of values.

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 50–55why, one step back
50
51```js52const transform = function * (line) {53 yield JSON.parse(line);54};55

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 56why, one step back
56const {stdout} = await execa({stdout: {transform, objectMode: true}})`node jsonlines-output.js`;

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

line 57why, one step back
57for (const data of stdout) {

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 58why, one step back
58 console.log(stdout); // {...object}

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 59–61why, one step back
59}
60```61

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

line 62why, one step back
62[`stdin`](api.md#optionsstdin) can also use `objectMode: true`.

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 63–69why, one step back
63
64```js65const transform = function * (line) {66 yield JSON.stringify(line);67};6869const input = [{event: 'example'}, {event: 'otherExample'}];

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 70why, one step back
70await execa({stdin: [input, {transform, objectMode: true}]})`node jsonlines-input.js`;

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 71–74why, one step back
71```
7273## Sharing state74

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

line 75why, one step back
75State can be shared between calls of the [`transform`](api.md#transformoptionstransform) and [`final`](api.md#transformoptionsfinal) functions.

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 76–77why, one step back
76
77```js

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 78why, one step back
78let count = 0;

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 79–87why, one step back
79
80// Prefix line number81const transform = function * (line) {82 yield `[${count++}] ${line}`;83};84```8586## Finalizing87

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

line 88why, one step back
88To create additional lines after the last one, a [`final`](api.md#transformoptionsfinal) generator function can be used.

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 89–101why, one step back
89
90```js91let count = 0;9293const transform = function * (line) {94 count += 1;95 yield line;96};9798const final = function * () {99 yield `Number of lines: ${count}`;100};101

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 102why, one step back
102const {stdout} = await execa({stdout: {transform, final}})`npm run build`;

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 103–105why, one step back
103console.log(stdout); // Ends with: 'Number of lines: 54'
104```105

Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6

line 106why, one step back
106## Duplex/Transform streams

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 107why, one step back
107

Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6

line 108why, one step back
108A [`Duplex`](https://nodejs.org/api/stream.html#class-streamduplex) stream, Node.js [`Transform`](https://nodejs.org/api/stream.html#class-streamtransform) stream or web [`TransformStream`](https://developer.mozilla.org/en-US/docs/Web/API/TransformStream) can be used instead of a generator function.

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

line 109why, one step back
109

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

line 110why, one step back
110Like generator functions, web `TransformStream` can be passed either directly or as a [`{transform}` plain object](api.md#transform-options). But `Duplex` and `Transform` must always be passed as a `{transform}` plain object.

Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6

line 111why, one step back
111

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

line 112why, one step back
112The [`objectMode`](#object-mode) transform option can be used, but not the [`binary`](api.md#transformoptionsbinary) nor [`preserveNewlines`](api.md#transformoptionspreservenewlines) options.

Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6

line 113why, one step back
113

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

lines 114–117why, one step back
114```js
115import {createGzip} from 'node:zlib';116import {execa} from 'execa';117

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 118–121why, one step back
118const {stdout} = await execa({
119 stdout: {transform: createGzip()},120 encoding: 'buffer',121})`npm run build`;

Add support for `std*: duplex` option (#937)ehmickybefore adoption6ca44fb

lines 122–124why, one step back
122console.log(stdout); // `stdout` is compressed with gzip
123```124

Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6

line 125why, one step back
125```js

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 126–129why, one step back
126const {stdout} = await execa({
127 stdout: new CompressionStream('gzip'),128 encoding: 'buffer',129})`npm run build`;

Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6

lines 130–132why, one step back
130console.log(stdout); // `stdout` is compressed with gzip
131```132

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 133–134why, one step back
133## Combining
134

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

line 135why, one step back
135The [`stdin`](api.md#optionsstdin), [`stdout`](api.md#optionsstdout), [`stderr`](api.md#optionsstderr) and [`stdio`](api.md#optionsstdio) options can accept [an array of values](output.md#multiple-targets). While this is not specific to transforms, this can be useful with them too. For example, the following transform impacts the value printed by `'inherit'`.

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 136–137why, one step back
136
137```js

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 138why, one step back
138await execa({stdout: [transform, 'inherit']})`npm run build`;

Reorder sections in `docs/scripts.md` (#876)ehmickybefore adoption2cd41ef

lines 139–143why, one step back
139```
140141This also allows using multiple transforms.142143```js

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 144why, one step back
144await execa({stdout: [transform, otherTransform]})`npm run build`;

Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6

lines 145–146why, one step back
145```
146

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 147why, one step back
147Or saving to archives.

Add support for `std*: TransformStream` option (#938)ehmickybefore adoption45824d6

lines 148–149why, one step back
148
149```js

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 150why, one step back
150await execa({stdout: [new CompressionStream('gzip'), {file: './output.gz'}]})`npm run build`;

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

lines 151–152why, one step back
151```
152

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 153why, one step back
153<hr>

Make subprocess async iterable (#923)ehmickybefore adoption2e8e8c9

line 154why, one step back
154

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 155–157why, one step back
155[**Next**: 🔀 Piping multiple subprocesses](pipe.md)\
156[**Previous**: 🤖 Binary data](binary.md)\157[**Top**: Table of contents](../readme.md#documentation)