Relevant source files
- docs/streams.md
- docs/pipe.md
- docs/ipc.md
- docs/small.md
- docs/node.md
- docs/bash.md
Advanced Usage
Piping Multiple Subprocesses
Execa allows piping multiple subprocesses together, creating a pipeline of commands. This can be achieved using the pipe() method, which takes another subprocess as an argument.
const {stdout} = await execa`npm run build`
.pipe`sort`
.pipe`head -n 2`;
Converting a Subprocess to a Stream
Execa also provides methods to convert a subprocess to a readable or writable stream. This can be useful when working with libraries that expect streams as arguments.
const readable = execa`npm run scaffold`.readable();
const writable = execa`npm run scaffold`.writable();
const duplex = execa`npm run scaffold`.duplex();
Converting a Subprocess to a Web Stream
Execa also provides methods to convert a subprocess to a readable or writable web stream. This can be useful when working with libraries that expect web streams as arguments.
const readableStream = execa`npm run scaffold`.readableStream();
const writableStream = execa`npm run scaffold`.writableStream();
const {readable, writable} = execa`npm run scaffold`.transformStream();
Inter-Process Communication (IPC)
Execa provides a way to exchange messages between subprocesses using IPC. This can be useful when working with complex workflows that involve multiple subprocesses.
// parent.js
import {execaNode} from 'execa';
const subprocess = execaNode`child.js`;
await subprocess.sendMessage('Hello from parent');
const message = await subprocess.getOneMessage();
console.log(message); // 'Hello from child'
// child.js
import {getOneMessage, sendMessage} from 'execa';
const message = await getOneMessage(); // 'Hello from parent'
const newMessage = message.replace('parent', 'child'); // 'Hello from child'
await sendMessage(newMessage);
Streaming
Execa provides a way to stream output from a subprocess, allowing for efficient processing of large amounts of data.
for await (const line of execa`npm run build`) {
if (line.includes('ERROR')) {
console.log(line);
}
}
Error Handling
Execa provides a way to handle errors that occur during the execution of a subprocess. This can be useful when working with complex workflows that involve multiple subprocesses.
try {
await execa`npm run build`
.pipe`sort`
.pipe`head -n2`;
} catch (error) {
console.error(error);
}
Piping Errors
Execa provides a way to pipe errors from one subprocess to another, allowing for efficient error handling.
try {
await $`npm run crash`
.pipe($`sort`)
.pipe($`head -n2`);
// This is never reached.
// The process crashes instead.
} catch (error) {
console.error(error);
}
Returning All Pipeline Results
Execa provides a way to return all pipeline results, allowing for efficient error handling and debugging.
const destinationResult = await execa`npm run build`
.pipe`head -n 2`;
console.log(destinationResult.stdout); // First 2 lines of `npm run build`
const sourceResult = destinationResult.pipedFrom[0];
console.log(sourceResult.stdout); // Full output of `npm run build`
Splitting Output into Lines
Execa provides a way to split output into lines, allowing for efficient processing of large amounts of data.
const lines = await $({lines: true})`npm run build`;
Iterating Over Output Lines
Execa provides a way to iterate over output lines, allowing for efficient processing of large amounts of data.
for await (const line of $`npm run build`) {
if (line.includes('ERROR')) {
console.log(line);
}
}
Detailed Errors
Execa provides a way to handle detailed errors that occur during the execution of a subprocess, allowing for efficient error handling and debugging.
try {
await $`npm run crash`
.pipe($`sort`)
.pipe($`head -n2`);
// This is never reached.
// The process crashes instead.
} catch (error) {
console.error(error);
}
Converting a Subprocess to a Duplex Stream
Execa provides a way to convert a subprocess to a duplex stream, allowing for efficient processing of large amounts of data.
const readable = execa`npm run scaffold`.readable();
const writable = execa`npm run scaffold`.writable();
const duplex = execa`npm run scaffold`.duplex();
Converting a Subprocess to a Web Stream
Execa provides a way to convert a subprocess to a web stream, allowing for efficient processing of large amounts of data.
const readableStream = execa`npm run scaffold`.readableStream();
const writableStream = execa`npm run scaffold`.writableStream();
const {readable, writable} = execa`npm run scaffold`.transformStream();
Inter-Process Communication (IPC)
Execa provides a way to exchange messages between subprocesses using IPC, allowing for efficient communication between multiple subprocesses.
// parent.js
import {execaNode} from 'execa';
const subprocess = execaNode`child.js`;
await subprocess.sendMessage('Hello from parent');
const message = await subprocess.getOneMessage();
console.log(message); // 'Hello from child'
// child.js
import {getOneMessage, sendMessage} from 'execa';
const message = await getOneMessage(); // 'Hello from parent'
const newMessage = message.replace('parent', 'child'); // 'Hello from child'
await sendMessage(newMessage);
Sources: