transform.md
This file
Path
docs/transform.md
71 lines · 2.5 KiB · Markdown
Last changed by
Line-wise transforms (#699)ehmickybefore adoption6dd804f
History of this fileTake it
Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 1–9why, one step back1# Transforms23## Summary45Transforms map or filter the input or output of a child process. They are defined by passing an [async generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function*) to the [`stdin`](../readme.md#stdin), [`stdout`](../readme.md#stdout-1), [`stderr`](../readme.md#stderr-1) or [`stdio`](../readme.md#stdio-1) option.67```js8import {execa} from 'execa';9Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 10–13why, one step back10const transform = async function * (lines) {11 for await (const line of lines) {12 const prefix = line.includes('error') ? 'ERROR' : 'INFO'13 yield `${prefix}: ${line}`Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 14–22why, one step back14 }15};1617const {stdout} = await execa('echo', ['hello'], {stdout: transform});18console.log(stdout); // HELLO19```2021## Encoding22Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 23why, one step back23The `lines` argument passed to the transform is an [`AsyncIterable<string>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols). If the [`encoding`](../readme.md#encoding) option is `buffer`, it is an `AsyncIterable<Uint8Array>` instead.Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
line 24why, one step back24Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 25why, one step back25The 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 26–28why, one step back2627## Filtering28Line-wise transforms (#699)ehmickybefore adoption6dd804f
line 29why, one step back29`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 30–33why, one step back3031```js32import {execa} from 'execa';33Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 34–37why, one step back34const transform = async function * (lines) {35 for await (const line of lines) {36 if (!line.includes('secret')) {37 yield line;Allow passing generators to the `std*` options (#693)ehmickybefore adoptione8f6bbd
lines 38–44why, one step back38 }39 }40};4142const {stdout} = await execa('echo', ['This is a secret.'], {stdout: transform});43console.log(stdout); // ''44```Line-wise transforms (#699)ehmickybefore adoption6dd804f
lines 45–57why, one step back4546## Binary data4748The transform iterates over lines by default.\49However, if a `{transform, binary: true}` plain object is passed, it iterates over arbitrary chunks of data instead.5051```js52await execa('./binary.js', {stdout: {transform, binary: true}});53```5455This is more efficient and recommended if the data is either:56 - Binary: Which does not have lines.57 - 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
lines 58–71why, one step back5859## Combining6061The [`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`.6263```js64await execa('echo', ['hello'], {stdout: [transform, 'inherit']});65```6667This also allows using multiple transforms.6869```js70await execa('echo', ['hello'], {stdout: [transform, otherTransform]});71```