Skip to content
Myr

lines.md

This file

Path

docs/lines.md

144 lines · 4.8 KiB · Markdown

Last changed by

Not known.

History of this file

Take it

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

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

lines 1–10why, 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# 📃 Text lines89## Simple splitting10

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

line 11why, one step back
11If the [`lines`](api.md#optionslines) option is `true`, the output is split into lines, as an array of strings.

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

lines 12–23why, one step back
12
13```js14import {execa} from 'execa';1516const lines = await execa({lines: true})`npm run build`;17console.log(lines.join('\n'));18```1920## Iteration2122### Progressive splitting23

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

line 24why, one step back
24The subprocess' return value is an [async iterable](api.md#subprocesssymbolasynciterator). It iterates over the output's lines while the subprocess is still running.

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

lines 25–33why, one step back
25
26```js27for await (const line of execa`npm run build`) {28 if (line.includes('ERROR')) {29 console.log(line);30 }31}32```33

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

line 34why, one step back
34Alternatively, [`subprocess.iterable()`](api.md#subprocessiterablereadableoptions) can be called to pass [iterable options](api.md#readableoptions).

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

line 35why, one step back
35

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

line 36why, one step back
36The iteration waits for the subprocess to end (even when using [`break`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/break) or [`return`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return)). It throws if the subprocess [fails](api.md#result). This means you do not need to `await` the subprocess' [promise](execution.md#result).

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

lines 37–45why, one step back
37
38```js39for await (const line of execa`npm run build`.iterable())) {40 /* ... */41}42```4344### Stdout/stderr45

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

line 46why, one step back
46By default, the subprocess' [`stdout`](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)) is used. The [`from`](api.md#readableoptionsfrom) iterable option can select a different file descriptor, such as [`'stderr'`](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)), [`'all'`](output.md#interleaved-output) or [`'fd3'`](output.md#additional-file-descriptors).

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

lines 47–57why, one step back
47
48```js49for await (const stderrLine of execa`npm run build`.iterable({from: 'stderr'})) {50 /* ... */51}52```5354## Newlines5556### Final newline57

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

line 58why, one step back
58The final newline is stripped from the output's last line, unless the [`stripFinalNewline`](api.md#optionsstripfinalnewline) option is `false`.

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

lines 59–66why, one step back
59
60```js61const {stdout} = await execa({stripFinalNewline: false})`npm run build`;62console.log(stdout.endsWith('\n')); // true63```6465### Array of lines66

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

line 67why, one step back
67When using the [`lines`](#simple-splitting) option, newlines are stripped from each line, unless the [`stripFinalNewline`](api.md#optionsstripfinalnewline) option is `false`.

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

lines 68–77why, one step back
68
69```js70// Each line now ends with '\n'.71// The last `line` might or might not end with '\n', depending on the output.72const lines = await execa({lines: true, stripFinalNewline: false})`npm run build`;73console.log(lines.join(''));74```7576### Iterable77

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

line 78why, one step back
78When [iterating](#progressive-splitting) over lines, newlines are stripped from each line, unless the [`preserveNewlines`](api.md#readableoptionspreservenewlines) iterable option is `true`.

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

line 79why, one step back
79

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

line 80why, one step back
80This option can also be used with [streams produced](streams.md#converting-a-subprocess-to-a-stream) by [`subprocess.readable()`](api.md#subprocessreadablereadableoptions) or [`subprocess.duplex()`](api.md#subprocessduplexduplexoptions), providing the [`binary`](binary.md#streams) option is `false`.

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

lines 81–91why, one step back
81
82```js83// `line` now ends with '\n'.84// The last `line` might or might not end with '\n', depending on the output.85for await (const line of execa`npm run build`.iterable({preserveNewlines: true})) {86 /* ... */87}88```8990### Transforms91

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

line 92why, one step back
92When using [transforms](transform.md), newlines are stripped from each `line` argument, unless the [`preserveNewlines`](api.md#transformoptionspreservenewlines) transform option is `true`.

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

lines 93–123why, one step back
93
94```js95// `line` now ends with '\n'.96// The last `line` might or might not end with '\n', depending on the output.97const transform = function * (line) { /* ... */ };9899await execa({stdout: {transform, preserveNewlines: true}})`npm run build`;100```101102Each `yield` produces at least one line. Calling `yield` multiple times or calling `yield *` produces multiples lines.103104```js105const transform = function * (line) {106 yield 'Important note:';107 yield 'Read the comments below.';108109 // Or:110 yield * [111 'Important note:',112 'Read the comments below.',113 ];114115 // Is the same as:116 yield 'Important note:\nRead the comments below.\n';117118 yield line;119};120121await execa({stdout: transform})`npm run build`;122```123

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

line 124why, one step back
124However, if the [`preserveNewlines`](api.md#transformoptionspreservenewlines) transform option is `true`, multiple `yield`s produce a single line instead.

Validate `verbose` option (#990)ehmickybefore adoptionf092dec

lines 125–144why, one step back
125
126```js127const transform = function * (line) {128 yield 'Important note: ';129 yield 'Read the comments below.\n';130131 // Is the same as:132 yield 'Important note: Read the comments below.\n';133134 yield line;135};136137await execa({stdout: {transform, preserveNewlines: true}})`npm run build`;138```139140<hr>141142[**Next**: 🤖 Binary data](binary.md)\143[**Previous**: 📢 Output](output.md)\144[**Top**: Table of contents](../readme.md#documentation)