transform.md
This file
Path
docs/transform.md
157 lines · 5.4 KiB · Markdown
Last changed by
[docs] Transforms: Summary: Fix example command and output. (#1206)M.K.before adoptionc468672
History of this fileTake it
The walk stopped after 200 commits: lines older than f744f7d point at it.
Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 1–5why, one step back1<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>Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
line 6why, one step back6Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 7–8why, one step back7# 🧙 Transforms8Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 9–10why, one step back9## Summary10Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 11why, one step back11Transforms 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*).Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 12–20why, one step back1213```js14import {execa} from 'execa';1516const transform = function * (line) {17 const prefix = line.includes('error') ? 'ERROR' : 'INFO';18 yield `${prefix}: ${line}`;19};20[docs] Transforms: Summary: Fix example command and output. (#1206)M.K.before adoptionc468672
lines 21–22why, one step back21const {stdout} = await execa({stdout: transform})`echo HELLO`;22console.log(stdout); // INFO: HELLOAdd support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 23–24why, one step back23```24Add user guides (#989)ehmickybefore adoptionbe91ecf
line 25why, one step back25## Difference with iterationAdd support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
line 26why, one step back26Add user guides (#989)ehmickybefore adoptionbe91ecf
line 27why, one step back27Transforms 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 back28- Modify the subprocess' [output](api.md#resultstdout) and [streams](api.md#subprocessstdout).Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 29–30why, one step back29- 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).Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 31–42why, one step back3132## 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};42Add user guides (#989)ehmickybefore adoptionbe91ecf
line 43why, one step back43const {stdout} = await execa({stdout: transform})`echo ${'This is a secret'}`;Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 44–48why, one step back44console.log(stdout); // ''45```4647## Object mode48Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 49why, one step back49By 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.Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 50–55why, one step back5051```js52const transform = function * (line) {53 yield JSON.parse(line);54};55Add user guides (#989)ehmickybefore adoptionbe91ecf
line 56why, one step back56const {stdout} = await execa({stdout: {transform, objectMode: true}})`node jsonlines-output.js`;Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
line 57why, one step back57for (const data of stdout) {Add user guides (#989)ehmickybefore adoptionbe91ecf
line 58why, one step back58 console.log(stdout); // {...object}Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 59–61why, one step back59}60```61Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 62why, one step back62[`stdin`](api.md#optionsstdin) can also use `objectMode: true`.Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 63–69why, one step back6364```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 back70await execa({stdin: [input, {transform, objectMode: true}]})`node jsonlines-input.js`;Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 71–74why, one step back71```7273## Sharing state74Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 75why, one step back75State can be shared between calls of the [`transform`](api.md#transformoptionstransform) and [`final`](api.md#transformoptionsfinal) functions.Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 76–77why, one step back7677```jsAdd user guides (#989)ehmickybefore adoptionbe91ecf
line 78why, one step back78let count = 0;Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 79–87why, one step back7980// Prefix line number81const transform = function * (line) {82 yield `[${count++}] ${line}`;83};84```8586## Finalizing87Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 88why, one step back88To create additional lines after the last one, a [`final`](api.md#transformoptionsfinal) generator function can be used.Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 89–101why, one step back8990```js91let count = 0;9293const transform = function * (line) {94 count += 1;95 yield line;96};9798const final = function * () {99 yield `Number of lines: ${count}`;100};101Add user guides (#989)ehmickybefore adoptionbe91ecf
line 102why, one step back102const {stdout} = await execa({stdout: {transform, final}})`npm run build`;Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 103–109why, one step back103console.log(stdout); // Ends with: 'Number of lines: 54'104```105106## Duplex/Transform streams107108A [`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.109Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 110why, one step back110Like 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 sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
line 111why, one step back111Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 112why, one step back112The [`objectMode`](#object-mode) transform option can be used, but not the [`binary`](api.md#transformoptionsbinary) nor [`preserveNewlines`](api.md#transformoptionspreservenewlines) options.Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 113–117why, one step back113114```js115import {createGzip} from 'node:zlib';116import {execa} from 'execa';117Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 118–121why, one step back118const {stdout} = await execa({119 stdout: {transform: createGzip()},120 encoding: 'buffer',121})`npm run build`;Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 122–125why, one step back122console.log(stdout); // `stdout` is compressed with gzip123```124125```jsAdd user guides (#989)ehmickybefore adoptionbe91ecf
lines 126–129why, one step back126const {stdout} = await execa({127 stdout: new CompressionStream('gzip'),128 encoding: 'buffer',129})`npm run build`;Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 130–134why, one step back130console.log(stdout); // `stdout` is compressed with gzip131```132133## Combining134Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 135why, one step back135The [`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'`.Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 136–137why, one step back136137```jsAdd user guides (#989)ehmickybefore adoptionbe91ecf
line 138why, one step back138await execa({stdout: [transform, 'inherit']})`npm run build`;Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 139–143why, one step back139```140141This also allows using multiple transforms.142143```jsAdd user guides (#989)ehmickybefore adoptionbe91ecf
line 144why, one step back144await execa({stdout: [transform, otherTransform]})`npm run build`;Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 145–146why, one step back145```146Add user guides (#989)ehmickybefore adoptionbe91ecf
line 147why, one step back147Or saving to archives.Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 148–149why, one step back148149```jsAdd user guides (#989)ehmickybefore adoptionbe91ecf
line 150why, one step back150await execa({stdout: [new CompressionStream('gzip'), {file: './output.gz'}]})`npm run build`;Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
lines 151–152why, one step back151```152Add user guides (#989)ehmickybefore adoptionbe91ecf
line 153why, one step back153<hr>Add support for sync iterables with `execaSync()` (#944)ehmickybefore adoptionf744f7d
line 154why, one step back154Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 155–157why, one step back155[**Next**: 🔀 Piping multiple subprocesses](pipe.md)\156[**Previous**: 🤖 Binary data](binary.md)\157[**Top**: Table of contents](../readme.md#documentation)