deepwiki-lab
Relevant source files
  • docs/input.md
  • docs/output.md
  • docs/lines.md
  • index.js
  • lib/methods/promise.js
  • docs/typescript.md
  • docs/bash.md
  • test/fixtures/ipc-disconnect.js
  • index.d.ts
  • lib/return/final-error.js
  • test-d/arguments/env.test-d.ts
  • test-d/ipc/get-each.test-d.ts

Input/Output

Overview

Execa provides various ways to pass input and retrieve output from subprocesses. This page explains the different methods and their usage.

Input

Environment Variables

Environment variables can be passed to subprocesses using the env option.

# Bash
export EXAMPLE=1
npm run build
// zx
await $({env: {EXAMPLE: '1'}})`npm run build`;
// Execa
await $({env: {EXAMPLE: '1'}})`npm run build`;

Local Binaries

Local binaries can be executed using the preferLocal option.

# Bash
npx tsc --version
// zx
await $({preferLocal: true})`tsc --version`;
// Execa
await $({preferLocal: true})`tsc --version`;

Passing Input to Stdin

Input can be passed to stdin using the input option.

# Bash
cat <<<"example"
// zx
$({input: 'example'})`cat`;
// Execa
$({input: 'example'})`cat`;

Passing Any Input Type

Execa allows passing any input type to subprocesses.

// Execa - main.js
const ipcInput = [
	{task: 'lint', ignore: /test\.js/},
	{task: 'copy', files: new Set(['main.js', 'index.js']),
}];
await $({ipcInput})`node build.js`;
// Execa - build.js
import {getOneMessage} from 'execa';

const ipcInput = await getOneMessage();

Output

Printing to Stdout

Output can be printed to stdout using the stdout option.

# Bash
echo example
// zx
echo`example`;
// Execa
console.log('example');

Silent Stdout

Stdout can be silenced using the quiet option.

# Bash
npm run build > /dev/null
// zx
await $`npm run build`.quiet();
// Execa does not print stdout by default
await $`npm run build`;

Binary Output

Binary output can be retrieved using the encoding option.

# Bash usually requires redirecting binary output
zip -r - input.txt > output.txt
// zx
const stdout = await $`zip -r - input.txt`.buffer();
// Execa
const {stdout} = await $({encoding: 'buffer'})`zip -r - input.txt`;

IPC

Sending Initial Message

An initial message can be sent to the subprocess using the ipcInput option.

// Execa - main.js
const ipcInput = [
	{task: 'lint', ignore: /test\.js/},
	{task: 'copy', files: new Set(['main.js', 'index.js']),
}];
await $({ipcInput})`node build.js`;
// Execa - build.js
import {getOneMessage} from 'execa';

const ipcInput = await getOneMessage();

Retrieving All Messages

All messages can be retrieved using the getEachMessage method.

// Execa - main.js
const {ipcOutput} = await $({ipc: true})`node build.js`;
console.log(ipcOutput[0]); // {kind: 'start', timestamp: date}
console.log(ipcOutput[1]); // {kind: 'stop', timestamp: date}
// Execa - build.js
import {sendMessage} from 'execa';

await sendMessage({kind: 'start', timestamp: new Date()});
await runBuild();
await sendMessage({kind: 'stop', timestamp: new Date()});

Verbose Mode

Verbose mode can be enabled using the verbose option.

# Bash
set -v
npm run build
set +v

Verbose Object

A verbose object can be passed to the subprocess using the verbose option.

// Execa
await $({verbose(verboseLine: string, verboseObject: VerboseObject) {
		return verboseObject.type === 'duration' ? verboseLine : undefined;
	}})`npm run build`;

Cancel Signal

A cancel signal can be passed to the subprocess using the cancelSignal option.

// Execa
await $({gracefulCancel: true, cancelSignal: AbortSignal.abort()})`npm run build`;

Diagram

graph TD
    A[Pass input to stdin] --> B[Pass any input type]
    B --> C[Retrieve all messages]
    C --> D[Enable verbose mode]
    D --> E[Pass verbose object]
    E --> F[Pass cancel signal]
    F --> G[Retrieve binary output]

Sources: index.js lib/methods/promise.js docs/typescript.md docs/bash.md test/fixtures/ipc-disconnect.js index.d.ts lib/return/final-error.js test-d/arguments/env.test-d.ts test-d/ipc/get-each.test-d.ts