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 0a13f2b point at it.

Fix the type of the return value (#1007)ehmickybefore adoption0a13f2b

lines 1–144why, 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 splitting1011If the [`lines`](api.md#optionslines) option is `true`, the output is split into lines, as an array of strings.1213```js14import {execa} from 'execa';1516const lines = await execa({lines: true})`npm run build`;17console.log(lines.join('\n'));18```1920## Iteration2122### Progressive splitting2324The subprocess' return value is an [async iterable](api.md#subprocesssymbolasynciterator). It iterates over the output's lines while the subprocess is still running.2526```js27for await (const line of execa`npm run build`) {28 if (line.includes('ERROR')) {29 console.log(line);30 }31}32```3334Alternatively, [`subprocess.iterable()`](api.md#subprocessiterablereadableoptions) can be called to pass [iterable options](api.md#readableoptions).3536The 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).3738```js39for await (const line of execa`npm run build`.iterable())) {40 /* ... */41}42```4344### Stdout/stderr4546By 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).4748```js49for await (const stderrLine of execa`npm run build`.iterable({from: 'stderr'})) {50 /* ... */51}52```5354## Newlines5556### Final newline5758The final newline is stripped from the output's last line, unless the [`stripFinalNewline`](api.md#optionsstripfinalnewline) option is `false`.5960```js61const {stdout} = await execa({stripFinalNewline: false})`npm run build`;62console.log(stdout.endsWith('\n')); // true63```6465### Array of lines6667When using the [`lines`](#simple-splitting) option, newlines are stripped from each line, unless the [`stripFinalNewline`](api.md#optionsstripfinalnewline) option is `false`.6869```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### Iterable7778When [iterating](#progressive-splitting) over lines, newlines are stripped from each line, unless the [`preserveNewlines`](api.md#readableoptionspreservenewlines) iterable option is `true`.7980This 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`.8182```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### Transforms9192When using [transforms](transform.md), newlines are stripped from each `line` argument, unless the [`preserveNewlines`](api.md#transformoptionspreservenewlines) transform option is `true`.9394```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```123124However, if the [`preserveNewlines`](api.md#transformoptionspreservenewlines) transform option is `true`, multiple `yield`s produce a single line instead.125126```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)