transform.md
This file
Path
docs/transform.md
128 lines · 3.9 KiB · Markdown
Last changed by
Validate against `yield undefined` in transforms (#756)ehmickybefore adoption192d818
History of this fileTake it
Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 1–4why, one step back1# Transforms23## Summary4Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
line 5why, one step back5Transforms map or filter the input or output of a child process. They are defined by passing a [generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*) to the [`stdin`](../readme.md#stdin), [`stdout`](../readme.md#stdout-1), [`stderr`](../readme.md#stderr-1) or [`stdio`](../readme.md#stdio-1) option. It can be [`async`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function*).Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 6–9why, one step back67```js8import {execa} from 'execa';9Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 10–12why, one step back10const transform = function * (line) {11 const prefix = line.includes('error') ? 'ERROR' : 'INFO';12 yield `${prefix}: ${line}`;Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 13–20why, one step back13};1415const {stdout} = await execa('echo', ['hello'], {stdout: transform});16console.log(stdout); // HELLO17```1819## Encoding20Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
line 21why, one step back21The `line` argument passed to the transform is a string. If the [`encoding`](../readme.md#encoding) option is `buffer`, it is an `Uint8Array` instead.Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 22why, one step back22Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 23why, one step back23The transform can `yield` either a `string` or an `Uint8Array`, regardless of the `lines` argument's type.Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 24–26why, one step back2425## Filtering26Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 27why, one step back27`yield` can be called 0, 1 or multiple times. Not calling `yield` enables filtering a specific line.Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 28–31why, one step back2829```js30import {execa} from 'execa';31Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 32–34why, one step back32const transform = function * (line) {33 if (!line.includes('secret')) {34 yield line;Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 35–40why, one step back35 }36};3738const {stdout} = await execa('echo', ['This is a secret.'], {stdout: transform});39console.log(stdout); // ''40```Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 41–53why, one step back4142## Binary data4344The transform iterates over lines by default.\45However, if a `{transform, binary: true}` plain object is passed, it iterates over arbitrary chunks of data instead.4647```js48await execa('./binary.js', {stdout: {transform, binary: true}});49```5051This is more efficient and recommended if the data is either:52 - Binary: Which does not have lines.53 - Text: But the transform works even if a line or word is split across multiple chunks.Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 54why, one step back54Line-wise standard streams (#741)ehmickybefore adoption479d12e
lines 55–56why, one step back55Please note the [`lines`](../readme.md#lines) option is unrelated: it has no impact on transforms.56Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 57–58why, one step back57## Object mode58Validate against `yield undefined` in transforms (#756)ehmickybefore adoption192d818
line 59why, one step back59By default, `stdout` and `stderr`'s transforms must return a string or an `Uint8Array`. However, if a `{transform, objectMode: true}` plain object is passed, any type can be returned instead, except `null` or `undefined`. The process' [`stdout`](../readme.md#stdout)/[`stderr`](../readme.md#stderr) will be an array of values.Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 60–61why, one step back6061```jsSimplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 62–64why, one step back62const transform = function * (line) {63 yield JSON.parse(line);64};Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 65–67why, one step back6566const {stdout} = await execa('./jsonlines-output.js', {stdout: {transform, objectMode: true}});67for (const data of stdout) {Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
line 68why, one step back68 console.log(stdout); // {...}Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 69–74why, one step back69}70```7172`stdin` can also use `objectMode: true`.7374```jsSimplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 75–77why, one step back75const transform = function * (line) {76 yield JSON.stringify(line);77};Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 78why, one step back78Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
line 79why, one step back79const input = [{event: 'example'}, {event: 'otherExample'}];Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 80why, one step back80await execa('./jsonlines-input.js', {stdin: [input, {transform, objectMode: true}]});Simplify transforms syntax (#748)ehmickybefore adoptionfd00294
lines 81–113why, one step back81```8283## Sharing state8485State can be shared between calls of the `transform` and [`final`](#finalizing) functions.8687```js88let count = 08990// Prefix line number91const transform = function * (line) {92 yield `[${count++}] ${line}`;93};94```9596## Finalizing9798To create additional lines after the last one, a `final` generator function can be used by passing a `{transform, final}` plain object.99100```js101let count = 0;102103const transform = function * (line) {104 count += 1;105 yield line;106};107108const final = function * () {109 yield `Number of lines: ${count}`;110};111112const {stdout} = await execa('./command.js', {stdout: {transform, final}});113console.log(stdout); // Ends with: 'Number of lines: 54'Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 114–115why, one step back114```115Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 116–128why, one step back116## Combining117118The [`stdin`](../readme.md#stdin), [`stdout`](../readme.md#stdout-1), [`stderr`](../readme.md#stderr-1) and [`stdio`](../readme.md#stdio-1) options can accept an array of values. 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`.119120```js121await execa('echo', ['hello'], {stdout: [transform, 'inherit']});122```123124This also allows using multiple transforms.125126```js127await execa('echo', ['hello'], {stdout: [transform, otherTransform]});128```