Skip to content
Myr

Add `ipc` option (#794)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 7710440 · Browse files · 10 files+154 added−146 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

10 files+154 added−146 removed

  • tests:+97 added−87 removed
  • other:+57 added−59 removed

10 files+154 added−146 removed

modified:lib/stdio/normalize.js+7 added-12 removed
modified:test/fixtures/send.js+2 added-6 removed
modified:test/stdio/normalize.js+1 added-22 removed
modified:test/kill.js+2 added-2 removed
12 unmodified lines
13const TIMEOUT_REGEXP = /timed out after/;1415const spawnNoKillable = async (forceKillAfterDelay, options) => {16 const subprocess = execa('no-killable.js', {17 stdio: ['pipe', 'pipe', 'pipe', 'ipc'],17 ipc: true,18 forceKillAfterDelay,19 ...options,20 });21 await pEvent(subprocess, 'message');
230 unmodified lines
252test('spawnAndExit cleanup detached', spawnAndExit, true, true);253254// When parent process exits before child process255const spawnAndKill = async (t, [signal, cleanup, detached, isKilled]) => {256 const subprocess = execa('sub-process.js', [cleanup, detached], {stdio: ['ignore', 'ignore', 'ignore', 'ipc']});256 const subprocess = execa('sub-process.js', [cleanup, detached], {stdio: 'ignore', ipc: true});257258 const pid = await pEvent(subprocess, 'message');259 t.true(Number.isInteger(pid));260 t.true(isRunning(pid));
173 unmodified lines
modified:test/node.js+83 added-46 removed
1import {dirname} from 'node:path';2import process from 'node:process';3import {pathToFileURL} from 'node:url';4import test from 'ava';5import getNode from 'get-node';6import {pEvent} from 'p-event';5import {execa, execaNode} from '../index.js';6import {setFixtureDir, FIXTURES_DIR} from './helpers/fixtures-dir.js';7import {fullStdio} from './helpers/stdio.js';7import {execa, execaSync, execaNode} from '../index.js';8import {FIXTURES_DIR} from './helpers/fixtures-dir.js';9import {identity, fullStdio} from './helpers/stdio.js';10import {foobarString} from './helpers/input.js';1110setFixtureDir();12process.chdir(FIXTURES_DIR);1312test('node()', async t => {13 const {exitCode} = await execaNode('test/fixtures/noop.js');14test('execaNode() succeeds', async t => {15 const {exitCode} = await execaNode('noop.js');16 t.is(exitCode, 0);17});1817test('node pipe stdout', async t => {18 const {stdout} = await execaNode('test/fixtures/noop.js', ['foo'], {19 stdout: 'pipe',20 });2119test('execaNode() returns stdout', async t => {20 const {stdout} = await execaNode('noop.js', ['foo']);21 t.is(stdout, 'foo');22});2325test('node does not return ipc channel\'s output', async t => {26 const {stdio} = await execaNode('test/fixtures/noop.js', ['foo']);27 t.deepEqual(stdio, [undefined, 'foo', '', undefined]);24const getNodePath = async () => {25 const {path} = await getNode(TEST_NODE_VERSION);26 return path;27};2829const TEST_NODE_VERSION = '16.0.0';3031const testNodePath = async (t, mapPath) => {32 const nodePath = mapPath(await getNodePath());33 const {stdout} = await execaNode('--version', {nodePath});34 t.is(stdout, `v${TEST_NODE_VERSION}`);35};3637test.serial('The "nodePath" option can be used', testNodePath, identity);38test.serial('The "nodePath" option can be a file URL', testNodePath, pathToFileURL);3940test('The "nodePath" option defaults to the current Node.js binary', async t => {41 const {stdout} = await execaNode('--version');42 t.is(stdout, process.version);43});4430test('node correctly use nodePath', async t => {31 const {stdout} = await execaNode(process.platform === 'win32' ? 'hello.cmd' : 'hello.sh', {32 stdout: 'pipe',33 nodePath: process.platform === 'win32' ? 'cmd.exe' : 'bash',34 nodeOptions: process.platform === 'win32' ? ['/c'] : [],35 });45const nodePathArguments = ['node', ['-p', 'process.env.Path || process.env.PATH']];4637 t.is(stdout, 'Hello World');47const testExecPath = async (t, mapPath) => {48 const execPath = mapPath(await getNodePath());49 const {stdout} = await execa(...nodePathArguments, {preferLocal: true, execPath});50 t.true(stdout.includes(TEST_NODE_VERSION));51};5253test.serial('The "execPath" option can be used', testExecPath, identity);54test.serial('The "execPath" option can be a file URL', testExecPath, pathToFileURL);5556test('The "execPath" option defaults to the current Node.js binary', async t => {57 const {stdout} = await execa(...nodePathArguments, {preferLocal: true});58 t.true(stdout.includes(dirname(process.execPath)));59});6040test('The nodePath option can be a file URL', async t => {41 const nodePath = pathToFileURL(process.execPath);42 const {stdout} = await execaNode('test/fixtures/noop.js', ['foo'], {nodePath});43 t.is(stdout, 'foo');61test.serial('The "execPath" option requires "preferLocal: true"', async t => {62 const execPath = await getNodePath();63 const {stdout} = await execa(...nodePathArguments, {execPath});64 t.false(stdout.includes(TEST_NODE_VERSION));65});6646test('node pass on nodeOptions', async t => {47 const {stdout} = await execaNode('console.log("foo")', {48 stdout: 'pipe',49 nodeOptions: ['-e'],50 });5152 t.is(stdout, 'foo');67test('The "nodeOptions" option can be used', async t => {68 const {stdout} = await execaNode('empty.js', {nodeOptions: ['--version']});69 t.is(stdout, process.version);70});7172const spawnNestedExecaNode = (realExecArgv, fakeExecArgv, nodeOptions) => execa(73 'node',
6 unmodified lines
80 t.is(stdout, foobarString);81 t.is(stdio[3], '');82};8367test('node removes --inspect without a port from nodeOptions when defined by parent process', testInspectRemoval, '--inspect');68test('node removes --inspect with a port from nodeOptions when defined by parent process', testInspectRemoval, '--inspect=9222');69test('node removes --inspect-brk without a port from nodeOptions when defined by parent process', testInspectRemoval, '--inspect-brk');70test('node removes --inspect-brk with a port from nodeOptions when defined by parent process', testInspectRemoval, '--inspect-brk=9223');84test('The "nodeOptions" option removes --inspect without a port when defined by parent process', testInspectRemoval, '--inspect');85test('The "nodeOptions" option removes --inspect with a port when defined by parent process', testInspectRemoval, '--inspect=9222');86test('The "nodeOptions" option removes --inspect-brk without a port when defined by parent process', testInspectRemoval, '--inspect-brk');87test('The "nodeOptions" option removes --inspect-brk with a port when defined by parent process', testInspectRemoval, '--inspect-brk=9223');8872test('node allows --inspect with a different port from nodeOptions even when defined by parent process', async t => {89test('The "nodeOptions" option allows --inspect with a different port even when defined by parent process', async t => {90 const {stdout, stdio} = await spawnNestedExecaNode(['--inspect=9225'], '', '--inspect=9224');91 t.is(stdout, foobarString);92 t.true(stdio[3].includes('Debugger listening'));93});9478test('node forbids --inspect with the same port from nodeOptions when defined by parent process', async t => {95test('The "nodeOptions" option forbids --inspect with the same port when defined by parent process', async t => {96 const {stdout, stdio} = await spawnNestedExecaNode(['--inspect=9226'], '', '--inspect=9226');97 t.is(stdout, foobarString);98 t.true(stdio[3].includes('address already in use'));99});10084test('node\'s forked script has a communication channel', async t => {85 const subprocess = execaNode('test/fixtures/send.js');101const runWithIpc = (file, options) => execa('node', [file], {...options, ipc: true});102103const testIpc = async (t, execaMethod, options) => {104 const subprocess = execaMethod('send.js', options);105 await pEvent(subprocess, 'message');106 subprocess.send(foobarString);107 const {stdout, stdio} = await subprocess;108 t.is(stdout, foobarString);109 t.is(stdio.length, 4);110 t.is(stdio[3], undefined);111};11288 subprocess.send('ping');113test('execaNode() adds an ipc channel', testIpc, execaNode, {});114test('The "ipc" option adds an ipc channel', testIpc, runWithIpc, {});115test('The "ipc" option works with "stdio: \'pipe\'"', testIpc, runWithIpc, {stdio: 'pipe'});116test('The "ipc" option works with "stdio: [\'pipe\', \'pipe\', \'pipe\']"', testIpc, runWithIpc, {stdio: ['pipe', 'pipe', 'pipe']});117test('The "ipc" option works with "stdio: [\'pipe\', \'pipe\', \'pipe\', \'ipc\']"', testIpc, runWithIpc, {stdio: ['pipe', 'pipe', 'pipe', 'ipc']});118test('The "ipc" option works with "stdout: \'pipe\'"', testIpc, runWithIpc, {stdout: 'pipe'});11990 const message = await pEvent(subprocess, 'message');91 t.is(message, 'pong');120test('No ipc channel is added by default', async t => {121 const {stdio} = await t.throwsAsync(execa('node', ['send.js']), {message: /process.send is not a function/});122 t.is(stdio.length, 3);123});12493 subprocess.kill();125test('Can disable "ipc" with execaNode', async t => {126 const {stdio} = await t.throwsAsync(execaNode('send.js', {ipc: false}), {message: /process.send is not a function/});127 t.is(stdio.length, 3);128});12995 const {signal} = await t.throwsAsync(subprocess);96 t.is(signal, 'SIGTERM');130test('Cannot use the "ipc" option with execaSync()', t => {131 t.throws(() => {132 execaSync('node', ['send.js'], {ipc: true});133 }, {message: /The "ipc: true" option cannot be used/});134});
modified:test/test.js+9 added-11 removed
1 unmodified line
2import process from 'node:process';3import {fileURLToPath, pathToFileURL} from 'node:url';4import test from 'ava';5import isRunning from 'is-running';6import getNode from 'get-node';6import which from 'which';7import {execa, execaSync, execaNode, $} from '../index.js';8import {setFixtureDir, PATH_KEY, FIXTURES_DIR_URL} from './helpers/fixtures-dir.js';9import {identity, fullStdio, getStdio} from './helpers/stdio.js';10import {noopGenerator} from './helpers/generator.js';11import {foobarString} from './helpers/input.js';1213setFixtureDir();14process.env.FOO = 'foo';15
41 unmodified lines
57 t.is(stdout, 'Hello World');58 });59}6061const testNullOptions = async (t, execaMethod) => {62 const {stdout} = await execaMethod('noop.js', [foobarString], null);63 t.is(stdout, foobarString);64};6566test('Can pass null to options', testNullOptions, execa);67test('Can pass null to options - sync', testNullOptions, execaSync);6869test('execaSync() throws error if ENOENT', t => {70 t.throws(() => {71 execaSync('foo');72 }, {message: ENOENT_REGEXP});
71 unmodified lines
144 const envPaths = stdout.split(path.delimiter);145 t.true(envPaths.some(envPath => envPath.endsWith('.bin')));146});147140const testExecPath = async (t, mapPath) => {141 const {path} = await getNode('16.0.0');142 const execPath = mapPath(path);143 const {stdout} = await execa('node', ['-p', 'process.env.Path || process.env.PATH'], {preferLocal: true, execPath});144 t.true(stdout.includes('16.0.0'));145};146147test.serial('execPath option', testExecPath, identity);148test.serial('execPath option can be a file URL', testExecPath, pathToFileURL);149148test('execa() returns a promise with pid', async t => {149 const subprocess = execa('noop.js', ['foo']);150 t.is(typeof subprocess.pid, 'number');151 await subprocess;
170 unmodified lines
modified:index.d.ts+17 added-15 removed
307 unmodified lines
308 [How to setup](https://nodejs.org/api/child_process.html#child_process_options_stdio) the child process' standard input. This can be:309 - `'pipe'`: Sets [`childProcess.stdin`](https://nodejs.org/api/child_process.html#subprocessstdin) stream.310 - `'overlapped'`: Like `'pipe'` but asynchronous on Windows.311 - `'ignore'`: Do not use `stdin`.312 - `'ipc'`: Sets an [IPC channel](https://nodejs.org/api/child_process.html#subprocesssendmessage-sendhandle-options-callback). You can also use `execaNode()` instead.312 - `'inherit'`: Re-use the current process' `stdin`.313 - an integer: Re-use a specific file descriptor from the current process.314 - a Node.js `Readable` stream.315 - `{ file: 'path' }` object.
14 unmodified lines
330 [How to setup](https://nodejs.org/api/child_process.html#child_process_options_stdio) the child process' standard output. This can be:331 - `'pipe'`: Sets `childProcessResult.stdout` (as a string or `Uint8Array`) and [`childProcess.stdout`](https://nodejs.org/api/child_process.html#subprocessstdout) (as a stream).332 - `'overlapped'`: Like `'pipe'` but asynchronous on Windows.333 - `'ignore'`: Do not use `stdout`.335 - `'ipc'`: Sets an [IPC channel](https://nodejs.org/api/child_process.html#subprocesssendmessage-sendhandle-options-callback). You can also use `execaNode()` instead.334 - `'inherit'`: Re-use the current process' `stdout`.335 - an integer: Re-use a specific file descriptor from the current process.336 - a Node.js `Writable` stream.337 - `{ file: 'path' }` object.
12 unmodified lines
350 [How to setup](https://nodejs.org/api/child_process.html#child_process_options_stdio) the child process' standard error. This can be:351 - `'pipe'`: Sets `childProcessResult.stderr` (as a string or `Uint8Array`) and [`childProcess.stderr`](https://nodejs.org/api/child_process.html#subprocessstderr) (as a stream).352 - `'overlapped'`: Like `'pipe'` but asynchronous on Windows.353 - `'ignore'`: Do not use `stderr`.356 - `'ipc'`: Sets an [IPC channel](https://nodejs.org/api/child_process.html#subprocesssendmessage-sendhandle-options-callback). You can also use `execaNode()` instead.354 - `'inherit'`: Re-use the current process' `stderr`.355 - an integer: Re-use a specific file descriptor from the current process.356 - a Node.js `Writable` stream.357 - `{ file: 'path' }` object.
12 unmodified lines
370 Like the `stdin`, `stdout` and `stderr` options but for all file descriptors at once. For example, `{stdio: ['ignore', 'pipe', 'pipe']}` is the same as `{stdin: 'ignore', stdout: 'pipe', stderr: 'pipe'}`.371372 A single string can be used as a shortcut. For example, `{stdio: 'pipe'}` is the same as `{stdin: 'pipe', stdout: 'pipe', stderr: 'pipe'}`.373377 The array can have more than 3 items, to create additional file descriptors beyond `stdin`/`stdout`/`stderr`. For example, `{stdio: ['pipe', 'pipe', 'pipe', 'ipc']}` sets a fourth file descriptor `'ipc'`.374 The array can have more than 3 items, to create additional file descriptors beyond `stdin`/`stdout`/`stderr`. For example, `{stdio: ['pipe', 'pipe', 'pipe', 'pipe']}` sets a fourth file descriptor.375376 @default 'pipe'377 */378 readonly stdio?: StdioOptions<IsSync>;
175 unmodified lines
554 */555 readonly all?: IfAsync<IsSync, boolean>;556557 /**558 Enables exchanging messages with the child process using [`childProcess.send(value)`](https://nodejs.org/api/child_process.html#subprocesssendmessage-sendhandle-options-callback) and [`childProcess.on('message', (value) => {})`](https://nodejs.org/api/child_process.html#event-message).559 */560 readonly ipc?: IfAsync<IsSync, boolean>;561562 /**561 Specify the kind of serialization used for sending messages between processes when using the `stdio: 'ipc'` option or `execaNode()`:563 Specify the kind of serialization used for sending messages between processes when using the `ipc` option:564 - `json`: Uses `JSON.stringify()` and `JSON.parse()`.565 - `advanced`: Uses [`v8.serialize()`](https://nodejs.org/api/v8.html#v8_v8_serialize_value)566567 [More info.](https://nodejs.org/api/child_process.html#child_process_advanced_serialization)
90 unmodified lines
658659 /**660 The output of the process on `stdout`.661660 This is `undefined` if the `stdout` option is set to only [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). This is an array if the `lines` option is `true, or if the `stdout` option is a transform in object mode.662 This is `undefined` if the `stdout` option is set to only [`'inherit'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). This is an array if the `lines` option is `true, or if the `stdout` option is a transform in object mode.663 */664 stdout: StdioOutput<'1', OptionsType>;665666 /**667 The output of the process on `stderr`.668667 This is `undefined` if the `stderr` option is set to only [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). This is an array if the `lines` option is `true, or if the `stderr` option is a transform in object mode.669 This is `undefined` if the `stderr` option is set to only [`'inherit'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). This is an array if the `lines` option is `true, or if the `stderr` option is a transform in object mode.670 */671 stderr: StdioOutput<'2', OptionsType>;672673 /**674 The output of the process on `stdin`, `stdout`, `stderr` and other file descriptors.675674 Items are `undefined` when their corresponding `stdio` option is set to only [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). Items are arrays when their corresponding `stdio` option is a transform in object mode.676 Items are `undefined` when their corresponding `stdio` option is set to only [`'inherit'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). Items are arrays when their corresponding `stdio` option is a transform in object mode.677 */678 stdio: StdioArrayOutput<OptionsType>;679680 /**
43 unmodified lines
724 The output of the process with `stdout` and `stderr` interleaved.725726 This is `undefined` if either:727 - the `all` option is `false` (default value)726 - both `stdout` and `stderr` options are set to [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio)728 - both `stdout` and `stderr` options are set to [`'inherit'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio)729730 This is an array if the `lines` option is `true, or if either the `stdout` or `stderr` option is a transform in object mode.731 */732 all: IfAsync<IsSync, AllOutput<OptionsType>>;
69 unmodified lines
802 Stream combining/interleaving [`stdout`](https://nodejs.org/api/child_process.html#child_process_subprocess_stdout) and [`stderr`](https://nodejs.org/api/child_process.html#child_process_subprocess_stderr).803804 This is `undefined` if either:805 - the `all` option is `false` (the default value)804 - both `stdout` and `stderr` options are set to [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio)806 - both `stdout` and `stderr` options are set to [`'inherit'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio)807 */808 all: AllStream<OptionsType>;809810 catch<ResultType = never>(
128 unmodified lines
939940/**941Same as `execa()` but synchronous.942941Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `serialization`, `signal` and `lines`. Also, the `stdin`, `stdout`, `stderr`, `stdio` and `input` options cannot be an array, an iterable or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.943Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `ipc`, `serialization`, `signal` and `lines`. Also, the `stdin`, `stdout`, `stderr`, `stdio` and `input` options cannot be an array, an iterable or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.944945Returns or throws a `childProcessResult`. The `childProcess` is not returned: its methods and properties are not available. This includes [`.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal), [`.pid`](https://nodejs.org/api/child_process.html#subprocesspid), `.pipe()` and the [`.stdin`/`.stdout`/`.stderr`](https://nodejs.org/api/child_process.html#subprocessstdout) streams.946947@param file - The program/script to execute, as a string or file URL
92 unmodified lines
10401041/**1042Same as `execaCommand()` but synchronous.10431042Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `serialization`, `signal` and `lines`. Also, the `stdin`, `stdout`, `stderr`, `stdio` and `input` options cannot be an array, an iterable or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.1044Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `ipc`, `serialization`, `signal` and `lines`. Also, the `stdin`, `stdout`, `stderr`, `stdio` and `input` options cannot be an array, an iterable or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.10451046Returns or throws a `childProcessResult`. The `childProcess` is not returned: its methods and properties are not available. This includes [`.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal), [`.pid`](https://nodejs.org/api/child_process.html#subprocesspid), `.pipe()` and the [`.stdin`/`.stdout`/`.stderr`](https://nodejs.org/api/child_process.html#subprocessstdout) streams.10471048@param command - The program/script to execute and its arguments.
50 unmodified lines
10991100 /**1101 Same as $\`command\` but synchronous.11021101 Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `serialization`, `signal` and `lines`. Also, the `stdin`, `stdout`, `stderr`, `stdio` and `input` options cannot be an array, an iterable or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.1103 Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `ipc`, `serialization`, `signal` and `lines`. Also, the `stdin`, `stdout`, `stderr`, `stdio` and `input` options cannot be an array, an iterable or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.11041105 Returns or throws a `childProcessResult`. The `childProcess` is not returned: its methods and properties are not available. This includes [`.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal), [`.pid`](https://nodejs.org/api/child_process.html#subprocesspid), `.pipe()` and the [`.stdin`/`.stdout`/`.stderr`](https://nodejs.org/api/child_process.html#subprocessstdout) streams.11061107 @returns A `childProcessResult` object
45 unmodified lines
11531154 /**1155 Same as $\`command\` but synchronous.11561155 Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `serialization`, `signal` and `lines`. Also, the `stdin`, `stdout`, `stderr`, `stdio` and `input` options cannot be an array, an iterable or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.1157 Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `ipc`, `serialization`, `signal` and `lines`. Also, the `stdin`, `stdout`, `stderr`, `stdio` and `input` options cannot be an array, an iterable or a web stream. Node.js streams must have a file descriptor unless the `input` option is used.11581159 Returns or throws a `childProcessResult`. The `childProcess` is not returned: its methods and properties are not available. This includes [`.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal), [`.pid`](https://nodejs.org/api/child_process.html#subprocesspid), `.pipe()` and the [`.stdin`/`.stdout`/`.stderr`](https://nodejs.org/api/child_process.html#subprocessstdout) streams.11601161 @returns A `childProcessResult` object
108 unmodified lines
12701271Like [`child_process#fork()`](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options):1272- the current Node version and options are used. This can be overridden using the `nodePath` and `nodeOptions` options.1273- the `shell` option cannot be used1272- an extra channel [`ipc`](https://nodejs.org/api/child_process.html#child_process_options_stdio) is passed to `stdio`1274- the `ipc` option defaults to `true`12751276@param scriptPath - Node.js script to execute, as a string or file URL1277@param arguments - Arguments to pass to `scriptPath` on execution.1278@returns An `ExecaChildProcess` that is both:
20 unmodified lines
modified:index.js+13 added-18 removed
8 unmodified lines
9import {npmRunPathEnv} from 'npm-run-path';10import {makeError} from './lib/error.js';11import {handleInputAsync, pipeOutputAsync, cleanupStdioStreams} from './lib/stdio/async.js';12import {handleInputSync, pipeOutputSync} from './lib/stdio/sync.js';13import {normalizeStdioNode} from './lib/stdio/normalize.js';13import {spawnedKill, validateTimeout, normalizeForceKillAfterDelay, cleanupOnExit} from './lib/kill.js';14import {pipeToProcess} from './lib/pipe.js';15import {getSpawnedResult, makeAllStream} from './lib/stream.js';16import {mergePromise} from './lib/promise.js';
67 unmodified lines
84 verbose = verboseDefault,85 killSignal = 'SIGTERM',86 forceKillAfterDelay = true,87 lines = false,88 ipc = false,89 ...options90}) => ({91 ...options,92 maxBuffer,
12 unmodified lines
105 verbose,106 killSignal,107 forceKillAfterDelay,108 lines,109 ipc,110});111112// Prevent passing the `timeout` option directly to `child_process.spawn()`113const handleAsyncOptions = ({timeout, ...options}) => ({...options, timeoutDuration: timeout});
109 unmodified lines
223};224225export function execaSync(rawFile, rawArgs, rawOptions) {226 const {file, args, command, escapedCommand, options} = handleArguments(rawFile, rawArgs, rawOptions);227 validateSyncOptions(options);228229 const stdioStreamsGroups = handleInputSync(options);230231 let result;
55 unmodified lines
287 isTerminated: false,288 };289}290291const validateSyncOptions = ({ipc}) => {292 if (ipc) {293 throw new TypeError('The "ipc: true" option cannot be used with synchronous methods.');294 }295};296297const normalizeScriptStdin = ({input, inputFile, stdio}) => input === undefined && inputFile === undefined && stdio === undefined298 ? {stdin: 'inherit'}299 : {};300
38 unmodified lines
339 const [file, ...args] = parseCommand(command);340 return execaSync(file, args, options);341}342335export function execaNode(scriptPath, args, options = {}) {336 if (args && !Array.isArray(args) && typeof args === 'object') {343export function execaNode(scriptPath, args = [], options = {}) {344 if (!Array.isArray(args)) {345 options = args;346 args = [];347 }348341 const stdio = normalizeStdioNode(options);349 const defaultExecArgv = process.execArgv.filter(arg => !arg.startsWith('--inspect'));343350 const {351 nodePath = process.execPath,352 nodeOptions = defaultExecArgv,353 } = options;354355 return execa(356 nodePath,351 [352 ...nodeOptions,353 getFilePath(scriptPath),354 ...(Array.isArray(args) ? args : []),355 ],356 {357 ...options,358 stdin: undefined,359 stdout: undefined,360 stderr: undefined,361 stdio,362 shell: false,363 },357 [...nodeOptions, getFilePath(scriptPath), ...args],358 {ipc: true, ...options, shell: false},359 );360}
modified:index.test-d.ts+2 added
1231 unmodified lines
1232expectError(execaSync('unicorns', {stdio: [[[new Uint8Array(), new Uint8Array()]]]}));1233expectError(execaSync('unicorns', {stdio: [[[{}, {}]]]}));1234expectError(execaSync('unicorns', {stdio: [[emptyStringGenerator()]]}));1235expectError(execaSync('unicorns', {stdio: [[asyncStringGenerator()]]}));1236execa('unicorns', {ipc: true});1237expectError(execaSync('unicorns', {ipc: true}));1238execa('unicorns', {serialization: 'advanced'});1239expectError(execaSync('unicorns', {serialization: 'advanced'}));1240execa('unicorns', {detached: true});1241expectError(execaSync('unicorns', {detached: true}));
142 unmodified lines
modified:readme.md+18 added-14 removed
242 unmodified lines
243244Like [`child_process#fork()`](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options):245- the current Node version and options are used. This can be overridden using the [`nodePath`](#nodepath-for-node-only) and [`nodeOptions`](#nodeoptions-for-node-only) options.246- the [`shell`](#shell) option cannot be used247- an extra channel [`ipc`](https://nodejs.org/api/child_process.html#child_process_options_stdio) is passed to [`stdio`](#stdio-1)247- the [`ipc`](#ipc) option defaults to `true`248249#### $\`command\`250251Executes a command. The `command` string includes both the `file` and its `arguments`. Returns a [`childProcess`](#childprocess).
25 unmodified lines
277### execaSync(file, arguments?, options?)278279Same as [`execa()`](#execacommandcommand-options) but synchronous.280281Cannot use the following options: [`all`](#all-2), [`cleanup`](#cleanup), [`buffer`](#buffer), [`detached`](#detached), [`serialization`](#serialization), [`signal`](#signal) and [`lines`](#lines). Also, the [`stdin`](#stdin), [`stdout`](#stdout-1), [`stderr`](#stderr-1), [`stdio`](#stdio-1) and [`input`](#input) options cannot be an array, an iterable or a web stream. Node.js streams [must have a file descriptor](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr) unless the `input` option is used.281Cannot use the following options: [`all`](#all-2), [`cleanup`](#cleanup), [`buffer`](#buffer), [`detached`](#detached), [`ipc`](#ipc), [`serialization`](#serialization), [`signal`](#signal) and [`lines`](#lines). Also, the [`stdin`](#stdin), [`stdout`](#stdout-1), [`stderr`](#stderr-1), [`stdio`](#stdio-1) and [`input`](#input) options cannot be an array, an iterable or a web stream. Node.js streams [must have a file descriptor](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr) unless the `input` option is used.282283Returns or throws a [`childProcessResult`](#childProcessResult). The [`childProcess`](#childprocess) is not returned: its methods and properties are not available. This includes [`.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal), [`.pid`](https://nodejs.org/api/child_process.html#subprocesspid), [`.pipe()`](#pipeexecachildprocess-streamname) and the [`.stdin`/`.stdout`/`.stderr`](https://nodejs.org/api/child_process.html#subprocessstdout) streams.284285### $.sync\`command\`286### $.s\`command\`287288Same as [$\`command\`](#command) but synchronous.289290Cannot use the following options: [`all`](#all-2), [`cleanup`](#cleanup), [`buffer`](#buffer), [`detached`](#detached), [`serialization`](#serialization), [`signal`](#signal) and [`lines`](#lines). Also, the [`stdin`](#stdin), [`stdout`](#stdout-1), [`stderr`](#stderr-1), [`stdio`](#stdio-1) and [`input`](#input) options cannot be an array, an iterable or a web stream. Node.js streams [must have a file descriptor](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr) unless the `input` option is used.290Cannot use the following options: [`all`](#all-2), [`cleanup`](#cleanup), [`buffer`](#buffer), [`detached`](#detached), [`ipc`](#ipc), [`serialization`](#serialization), [`signal`](#signal) and [`lines`](#lines). Also, the [`stdin`](#stdin), [`stdout`](#stdout-1), [`stderr`](#stderr-1), [`stdio`](#stdio-1) and [`input`](#input) options cannot be an array, an iterable or a web stream. Node.js streams [must have a file descriptor](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr) unless the `input` option is used.291292Returns or throws a [`childProcessResult`](#childProcessResult). The [`childProcess`](#childprocess) is not returned: its methods and properties are not available. This includes [`.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal), [`.pid`](https://nodejs.org/api/child_process.html#subprocesspid), [`.pipe()`](#pipeexecachildprocess-streamname) and the [`.stdin`/`.stdout`/`.stderr`](https://nodejs.org/api/child_process.html#subprocessstdout) streams.293294### execaCommandSync(command, options?)295296Same as [`execaCommand()`](#execacommand-command-options) but synchronous.297298Cannot use the following options: [`all`](#all-2), [`cleanup`](#cleanup), [`buffer`](#buffer), [`detached`](#detached), [`serialization`](#serialization), [`signal`](#signal) and [`lines`](#lines). Also, the [`stdin`](#stdin), [`stdout`](#stdout-1), [`stderr`](#stderr-1), [`stdio`](#stdio-1) and [`input`](#input) options cannot be an array, an iterable or a web stream. Node.js streams [must have a file descriptor](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr) unless the `input` option is used.298Cannot use the following options: [`all`](#all-2), [`cleanup`](#cleanup), [`buffer`](#buffer), [`detached`](#detached), [`ipc`](#ipc), [`serialization`](#serialization), [`signal`](#signal) and [`lines`](#lines). Also, the [`stdin`](#stdin), [`stdout`](#stdout-1), [`stderr`](#stderr-1), [`stdio`](#stdio-1) and [`input`](#input) options cannot be an array, an iterable or a web stream. Node.js streams [must have a file descriptor](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr) unless the `input` option is used.299300Returns or throws a [`childProcessResult`](#childProcessResult). The [`childProcess`](#childprocess) is not returned: its methods and properties are not available. This includes [`.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal), [`.pid`](https://nodejs.org/api/child_process.html#subprocesspid), [`.pipe()`](#pipeexecachildprocess-streamname) and the [`.stdin`/`.stdout`/`.stderr`](https://nodejs.org/api/child_process.html#subprocessstdout) streams.301302### Shell syntax
13 unmodified lines
316Stream [combining/interleaving](#ensuring-all-output-is-interleaved) [`stdout`](https://nodejs.org/api/child_process.html#child_process_subprocess_stdout) and [`stderr`](https://nodejs.org/api/child_process.html#child_process_subprocess_stderr).317318This is `undefined` if either:319- the [`all` option](#all-2) is `false` (the default value)320- both [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options are set to [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio)320- both [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options are set to [`'inherit'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio)321322#### pipe(execaChildProcess, streamName?)323324`execaChildProcess`: [`execa()` return value](#pipe-multiple-processes)
50 unmodified lines
375Type: `string | Uint8Array | string[] | Uint8Array[] | unknown[] | undefined`376377The output of the process on `stdout`.378379This is `undefined` if the [`stdout`](#stdout-1) option is set to only [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). This is an array if the [`lines` option](#lines) is `true, or if the `stdout` option is a [transform in object mode](docs/transform.md#object-mode).379This is `undefined` if the [`stdout`](#stdout-1) option is set to only [`'inherit'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). This is an array if the [`lines` option](#lines) is `true, or if the `stdout` option is a [transform in object mode](docs/transform.md#object-mode).380381#### stderr382383Type: `string | Uint8Array | string[] | Uint8Array[] | unknown[] | undefined`384385The output of the process on `stderr`.386387This is `undefined` if the [`stderr`](#stderr-1) option is set to only [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). This is an array if the [`lines` option](#lines) is `true, or if the `stderr` option is a [transform in object mode](docs/transform.md#object-mode).387This is `undefined` if the [`stderr`](#stderr-1) option is set to only [`'inherit'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). This is an array if the [`lines` option](#lines) is `true, or if the `stderr` option is a [transform in object mode](docs/transform.md#object-mode).388389#### all390391Type: `string | Uint8Array | string[] | Uint8Array[] | unknown[] | undefined`
1 unmodified line
393The output of the process with `stdout` and `stderr` [interleaved](#ensuring-all-output-is-interleaved).394395This is `undefined` if either:396- the [`all` option](#all-2) is `false` (the default value)397- both [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options are set to only [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio)397- both [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options are set to only [`'inherit'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio)398399This is an array if the [`lines` option](#lines) is `true, or if either the `stdout` or `stderr` option is a [transform in object mode](docs/transform.md#object-mode).400401#### stdio
1 unmodified line
403Type: `Array<string | Uint8Array | string[] | Uint8Array[] | unknown[] | undefined>`404405The output of the process on [`stdin`](#stdin), [`stdout`](#stdout-1), [`stderr`](#stderr-1) and [other file descriptors](#stdio-1).406407Items are `undefined` when their corresponding [`stdio`](#stdio-1) option is set to [`'inherit'`, `'ipc'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). Items are arrays when their corresponding `stdio` option is a [transform in object mode](docs/transform.md#object-mode).407Items are `undefined` when their corresponding [`stdio`](#stdio-1) option is set to [`'inherit'`, `'ignore'`, `Stream` or `integer`](https://nodejs.org/api/child_process.html#child_process_options_stdio). Items are arrays when their corresponding `stdio` option is a [transform in object mode](docs/transform.md#object-mode).408409#### failed410411Type: `boolean`
142 unmodified lines
554[How to setup](https://nodejs.org/api/child_process.html#child_process_options_stdio) the child process' standard input. This can be:555- `'pipe'`: Sets [`childProcess.stdin`](https://nodejs.org/api/child_process.html#subprocessstdin) stream.556- `'overlapped'`: Like `'pipe'` but asynchronous on Windows.557- `'ignore'`: Do not use `stdin`.558- `'ipc'`: Sets an [IPC channel](https://nodejs.org/api/child_process.html#subprocesssendmessage-sendhandle-options-callback). You can also use [`execaNode()`](#execanodescriptpath-arguments-options) instead.558- `'inherit'`: Re-use the current process' `stdin`.559- an integer: Re-use a specific file descriptor from the current process.560- a [Node.js `Readable` stream](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr).561- `{ file: 'path' }` object.
14 unmodified lines
576[How to setup](https://nodejs.org/api/child_process.html#child_process_options_stdio) the child process' standard output. This can be:577- `'pipe'`: Sets [`childProcessResult.stdout`](#stdout) (as a string or `Uint8Array`) and [`childProcess.stdout`](https://nodejs.org/api/child_process.html#subprocessstdout) (as a stream).578- `'overlapped'`: Like `'pipe'` but asynchronous on Windows.579- `'ignore'`: Do not use `stdout`.581- `'ipc'`: Sets an [IPC channel](https://nodejs.org/api/child_process.html#subprocesssendmessage-sendhandle-options-callback). You can also use [`execaNode()`](#execanodescriptpath-arguments-options) instead.580- `'inherit'`: Re-use the current process' `stdout`.581- an integer: Re-use a specific file descriptor from the current process.582- a [Node.js `Writable` stream](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr).583- `{ file: 'path' }` object.
12 unmodified lines
596[How to setup](https://nodejs.org/api/child_process.html#child_process_options_stdio) the child process' standard error. This can be:597- `'pipe'`: Sets [`childProcessResult.stderr`](#stderr) (as a string or `Uint8Array`) and [`childProcess.stderr`](https://nodejs.org/api/child_process.html#subprocessstderr) (as a stream).598- `'overlapped'`: Like `'pipe'` but asynchronous on Windows.599- `'ignore'`: Do not use `stderr`.602- `'ipc'`: Sets an [IPC channel](https://nodejs.org/api/child_process.html#subprocesssendmessage-sendhandle-options-callback). You can also use [`execaNode()`](#execanodescriptpath-arguments-options) instead.600- `'inherit'`: Re-use the current process' `stderr`.601- an integer: Re-use a specific file descriptor from the current process.602- a [Node.js `Writable` stream](#redirect-a-nodejs-stream-fromto-stdinstdoutstderr).603- `{ file: 'path' }` object.
12 unmodified lines
616Like the [`stdin`](#stdin), [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options but for all file descriptors at once. For example, `{stdio: ['ignore', 'pipe', 'pipe']}` is the same as `{stdin: 'ignore', stdout: 'pipe', stderr: 'pipe'}`.617618A single string can be used as a shortcut. For example, `{stdio: 'pipe'}` is the same as `{stdin: 'pipe', stdout: 'pipe', stderr: 'pipe'}`.619623The array can have more than 3 items, to create additional file descriptors beyond `stdin`/`stdout`/`stderr`. For example, `{stdio: ['pipe', 'pipe', 'pipe', 'ipc']}` sets a fourth file descriptor `'ipc'`.620The array can have more than 3 items, to create additional file descriptors beyond `stdin`/`stdout`/`stderr`. For example, `{stdio: ['pipe', 'pipe', 'pipe', 'pipe']}` sets a fourth file descriptor.621622#### all623624Type: `boolean`\
10 unmodified lines
635- [`result.stdout`](#stdout), [`result.stderr`](#stderr), [`result.all`](#all-1) and [`result.stdio`](#stdio) are arrays of lines.636- [`childProcess.stdout`](https://nodejs.org/api/child_process.html#subprocessstdout), [`childProcess.stderr`](https://nodejs.org/api/child_process.html#subprocessstderr), [`childProcess.all`](#all) and [`childProcess.stdio`](https://nodejs.org/api/child_process.html#subprocessstdio) iterate over lines instead of arbitrary chunks.637- Any stream passed to the [`stdout`](#stdout-1), [`stderr`](#stderr-1) or [`stdio`](#stdio-1) option receives lines instead of arbitrary chunks.638639#### ipc640641Type: `boolean`\642Default: `true` with [`execaNode()`](#execanodescriptpath-arguments-options), `false` otherwise643644Enables exchanging messages with the child process using [`childProcess.send(value)`](https://nodejs.org/api/child_process.html#subprocesssendmessage-sendhandle-options-callback) and [`childProcess.on('message', (value) => {})`](https://nodejs.org/api/child_process.html#event-message).645646#### reject647648Type: `boolean`\649Default: `true`
45 unmodified lines
695696Type: `string`\697Default: `'json'`698695Specify the kind of serialization used for sending messages between processes when using the [`stdio: 'ipc'`](#stdio-1) option or [`execaNode()`](#execanodescriptpath-arguments-options):699Specify the kind of serialization used for sending messages between processes when using the [`ipc`](#ipc) option:700 - `json`: Uses `JSON.stringify()` and `JSON.parse()`.701 - `advanced`: Uses [`v8.serialize()`](https://nodejs.org/api/v8.html#v8_v8_serialize_value)702703[More info.](https://nodejs.org/api/child_process.html#child_process_advanced_serialization)
247 unmodified lines