Skip to content
Myr

Rename `signal` option to `cancelSignal` (#880)

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

authored by ehmicky · committed by GitHub · parent 7d0943f · Browse files · 9 files+115 added−102 removed
This commit

State

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

Diff stats

9 files+115 added−102 removed

  • tests:+87 added−80 removed
  • other:+28 added−22 removed

9 files+115 added−102 removed

modified:docs/scripts.md+1 added-1 removed
44 unmodified lines
45Execa's scripting API mostly consists of only two methods: [`` $`command` ``](../readme.md#command) and [`$(options)`](../readme.md#options).4647[No special binary](#main-binary) is recommended, no [global variable](#global-variables) is injected: scripts are regular Node.js files.4849Execa is a thin wrapper around the core Node.js [`child_process` module](https://nodejs.org/api/child_process.html). Unlike zx, it lets you use [any of its native features](#background-processes): [`pid`](#pid), [IPC](https://nodejs.org/api/child_process.html#subprocesssendmessage-sendhandle-options-callback), [`unref()`](https://nodejs.org/api/child_process.html#subprocessunref), [`detached`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), [`uid`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), [`gid`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), [`signal`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), etc.49Execa is a thin wrapper around the core Node.js [`child_process` module](https://nodejs.org/api/child_process.html). Unlike zx, it lets you use [any of its native features](#background-processes): [`pid`](#pid), [IPC](https://nodejs.org/api/child_process.html#subprocesssendmessage-sendhandle-options-callback), [`unref()`](https://nodejs.org/api/child_process.html#subprocessunref), [`detached`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), [`uid`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), [`gid`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), [`cancelSignal`](https://nodejs.org/api/child_process.html#child_processspawncommand-args-options), etc.5051### Modularity5253zx includes many builtin utilities: `fetch()`, `question()`, `sleep()`, `stdin()`, `retry()`, `spinner()`, `chalk`, `fs-extra`, `os`, `path`, `globby`, `yaml`, `minimist`, `which`, Markdown scripts, remote scripts.
662 unmodified lines
modified:lib/stream/exit.js+1 added-1 removed
modified:lib/async.js+7 added-1 removed
added:test/exit/cancel.js+85 added
1import {once} from 'node:events';2import test from 'ava';3import {execa, execaSync} from '../../index.js';4import {setFixtureDir} from '../helpers/fixtures-dir.js';56setFixtureDir();78test('result.isCanceled is false when abort isn\'t called (success)', async t => {9 const {isCanceled} = await execa('noop.js');10 t.false(isCanceled);11});1213test('result.isCanceled is false when abort isn\'t called (failure)', async t => {14 const {isCanceled} = await t.throwsAsync(execa('fail.js'));15 t.false(isCanceled);16});1718test('result.isCanceled is false when abort isn\'t called in sync mode (success)', t => {19 const {isCanceled} = execaSync('noop.js');20 t.false(isCanceled);21});2223test('result.isCanceled is false when abort isn\'t called in sync mode (failure)', t => {24 const {isCanceled} = t.throws(() => {25 execaSync('fail.js');26 });27 t.false(isCanceled);28});2930test('error.isCanceled is true when abort is used', async t => {31 const abortController = new AbortController();32 const subprocess = execa('noop.js', {cancelSignal: abortController.signal});33 abortController.abort();34 const {isCanceled} = await t.throwsAsync(subprocess);35 t.true(isCanceled);36});3738test('error.isCanceled is false when kill method is used', async t => {39 const abortController = new AbortController();40 const subprocess = execa('noop.js', {cancelSignal: abortController.signal});41 subprocess.kill();42 const {isCanceled} = await t.throwsAsync(subprocess);43 t.false(isCanceled);44});4546test('calling abort is considered a signal termination', async t => {47 const abortController = new AbortController();48 const subprocess = execa('forever.js', {cancelSignal: abortController.signal});49 await once(subprocess, 'spawn');50 abortController.abort();51 const {isTerminated, signal} = await t.throwsAsync(subprocess);52 t.true(isTerminated);53 t.is(signal, 'SIGTERM');54});5556test('calling abort throws an error with message "Command was canceled"', async t => {57 const abortController = new AbortController();58 const subprocess = execa('noop.js', {cancelSignal: abortController.signal});59 abortController.abort();60 await t.throwsAsync(subprocess, {message: /Command was canceled/});61});6263test('calling abort twice should show the same behaviour as calling it once', async t => {64 const abortController = new AbortController();65 const subprocess = execa('noop.js', {cancelSignal: abortController.signal});66 abortController.abort();67 abortController.abort();68 const {isCanceled} = await t.throwsAsync(subprocess);69 t.true(isCanceled);70});7172test('calling abort on a successfully completed process does not make result.isCanceled true', async t => {73 const abortController = new AbortController();74 const subprocess = execa('noop.js', {cancelSignal: abortController.signal});75 const result = await subprocess;76 abortController.abort();77 t.false(result.isCanceled);78});7980test('Throws when using the former "signal" option name', t => {81 const abortController = new AbortController();82 t.throws(() => {83 execa('noop.js', {signal: abortController.signal});84 }, {message: /renamed to "cancelSignal"/});85});
modified:test/exit/kill.js+2 added-2 removed
96 unmodified lines
97 test('`forceKillAfterDelay` should kill after a timeout with an error and a killSignal', testForceKill, 50, new Error('test'), {killSignal: 'SIGINT'});9899 test('`forceKillAfterDelay` works with the "signal" option', async t => {100 const abortController = new AbortController();101 const subprocess = spawnNoKillableSimple({signal: abortController.signal});101 const subprocess = spawnNoKillableSimple({cancelSignal: abortController.signal});102 await once(subprocess, 'spawn');103 abortController.abort();104 const {isTerminated, signal, isCanceled} = await t.throwsAsync(subprocess);105 t.true(isTerminated);
173 unmodified lines
279});280281test('child process double errors are handled after spawn', async t => {282 const abortController = new AbortController();283 const subprocess = execa('forever.js', {signal: abortController.signal});283 const subprocess = execa('forever.js', {cancelSignal: abortController.signal});284 await once(subprocess, 'spawn');285 const error = new Error('test');286 subprocess.emit('error', error);287 await setImmediate();
19 unmodified lines
deleted:test/exit/signal.js-78 removed
modified:index.d.ts+10 added-10 removed
497 unmodified lines
498 readonly maxBuffer?: number;499500 /**501 Signal used to terminate the child process when:502 - using the `signal`, `timeout`, `maxBuffer` or `cleanup` option502 - using the `cancelSignal`, `timeout`, `maxBuffer` or `cleanup` option503 - calling [`subprocess.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal) with no arguments504505 This can be either a name (like `"SIGTERM"`) or a number (like `9`).506
6 unmodified lines
513514 The grace period is 5 seconds by default. This feature can be disabled with `false`.515516 This works when the child process is terminated by either:517 - the `signal`, `timeout`, `maxBuffer` or `cleanup` option517 - the `cancelSignal`, `timeout`, `maxBuffer` or `cleanup` option518 - calling [`subprocess.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal) with no arguments519520 This does not work when the child process is terminated by either:521 - calling [`subprocess.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal) with an argument
90 unmodified lines
612 ```613 import {execa} from 'execa';614615 const abortController = new AbortController();616 const subprocess = execa('node', [], {signal: abortController.signal});616 const subprocess = execa('node', [], {cancelSignal: abortController.signal});617618 setTimeout(() => {619 abortController.abort();620 }, 1000);
5 unmodified lines
626 console.log(error.isCanceled); // true627 }628 ```629 */630 readonly signal?: IfAsync<IsSync, AbortSignal>;630 readonly cancelSignal?: IfAsync<IsSync, AbortSignal>;631};632633export type Options = CommonOptions<false>;634export type SyncOptions = CommonOptions<true>;
28 unmodified lines
663664 /**665 The numeric exit code of the process that was run.666667 This is `undefined` when the process could not be spawned or was terminated by a [signal](#signal-1).667 This is `undefined` when the process could not be spawned or was terminated by a [signal](#signal).668 */669 exitCode?: number;670671 /**
55 unmodified lines
727 */728 cwd: string;729730 /**731 Whether the process was canceled using the [`signal`](https://github.com/sindresorhus/execa#signal-1) option.731 Whether the process was canceled using the `cancelSignal` option.732 */733 isCanceled: boolean;734735 /**
291 unmodified lines
10271028/**1029Same as `execa()` but synchronous.10301031Cannot 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.1031Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `ipc`, `serialization`, `cancelSignal` 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.10321033Returns 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.10341035@param file - The program/script to execute, as a string or file URL
92 unmodified lines
11281129/**1130Same as `execaCommand()` but synchronous.11311132Cannot 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.1132Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `ipc`, `serialization`, `cancelSignal` 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.11331134Returns 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.11351136@param command - The program/script to execute and its arguments.
49 unmodified lines
11861187 /**1188 Same as $\`command\` but synchronous.11891190 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.1190 Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `ipc`, `serialization`, `cancelSignal` 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.11911192 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.11931194 @returns A `childProcessResult` object
45 unmodified lines
12401241 /**1242 Same as $\`command\` but synchronous.12431244 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.1244 Cannot use the following options: `all`, `cleanup`, `buffer`, `detached`, `ipc`, `serialization`, `cancelSignal` 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.12451246 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.12471248 @returns A `childProcessResult` object
132 unmodified lines
modified:index.test-d.ts+2 added-2 removed
1432 unmodified lines
1433execa('unicorns', {forceKillAfterDelay: undefined});1434execaSync('unicorns', {forceKillAfterDelay: undefined});1435expectError(execa('unicorns', {forceKillAfterDelay: 'true'}));1436expectError(execaSync('unicorns', {forceKillAfterDelay: 'true'}));1437execa('unicorns', {signal: new AbortController().signal});1438expectError(execaSync('unicorns', {signal: new AbortController().signal}));1437execa('unicorns', {cancelSignal: new AbortController().signal});1438expectError(execaSync('unicorns', {cancelSignal: new AbortController().signal}));1439execa('unicorns', {windowsVerbatimArguments: true});1440execaSync('unicorns', {windowsVerbatimArguments: true});1441execa('unicorns', {windowsHide: false});1442execaSync('unicorns', {windowsHide: false});
121 unmodified lines
modified:readme.md+7 added-7 removed
313 unmodified lines
314#### $.s(options)\`command\`315316Same as [`execa()`](#execacommandcommand-options), [`execaCommand()`](#execacommand-command-options), [$\`command\`](#command) but synchronous.317318Cannot 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.318Cannot use the following options: [`all`](#all-2), [`cleanup`](#cleanup), [`buffer`](#buffer), [`detached`](#detached), [`ipc`](#ipc), [`serialization`](#serialization), [`cancelSignal`](#cancelsignal) 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.319320Returns 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()`](#pipefile-arguments-options) and the [`.stdin`/`.stdout`/`.stderr`](https://nodejs.org/api/child_process.html#subprocessstdout) streams.321322### Shell syntax
201 unmodified lines
524#### isCanceled525526Type: `boolean`527528Whether the process was canceled using the [`signal`](#signal-1) option.528Whether the process was canceled using the [`cancelSignal`](#cancelsignal) option.529530#### isTerminated531532Type: `boolean`
7 unmodified lines
540Type: `number | undefined`541542The numeric exit code of the process that was run.543544This is `undefined` when the process could not be spawned or was terminated by a [signal](#signal-1).544This is `undefined` when the process could not be spawned or was terminated by a [signal](#signal).545546#### signal547548Type: `string | undefined`
300 unmodified lines
849Default: `0`850851If `timeout` is greater than `0`, the child process will be [terminated](#killsignal) if it runs for longer than that amount of milliseconds.852853#### signal853#### cancelSignal854855Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)856857You can abort the spawned process using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
9 unmodified lines
867868The grace period is 5 seconds by default. This feature can be disabled with `false`.869870This works when the child process is terminated by either:871- the [`signal`](#signal-1), [`timeout`](#timeout), [`maxBuffer`](#maxbuffer) or [`cleanup`](#cleanup) option871- the [`cancelSignal`](#cancelsignal), [`timeout`](#timeout), [`maxBuffer`](#maxbuffer) or [`cleanup`](#cleanup) option872- calling [`subprocess.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal) with no arguments873874This does not work when the child process is terminated by either:875- calling [`subprocess.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal) with an argument
7 unmodified lines
883Type: `string | number`\884Default: `SIGTERM`885886Signal used to terminate the child process when:887- using the [`signal`](#signal-1), [`timeout`](#timeout), [`maxBuffer`](#maxbuffer) or [`cleanup`](#cleanup) option887- using the [`cancelSignal`](#cancelsignal), [`timeout`](#timeout), [`maxBuffer`](#maxbuffer) or [`cleanup`](#cleanup) option888- calling [`subprocess.kill()`](https://nodejs.org/api/child_process.html#subprocesskillsignal) with no arguments889890This can be either a name (like `"SIGTERM"`) or a number (like `9`).891
80 unmodified lines
972```js973import {execa} from 'execa';974975const abortController = new AbortController();976const subprocess = execa('node', [], {signal: abortController.signal});976const subprocess = execa('node', [], {cancelSignal: abortController.signal});977978setTimeout(() => {979 abortController.abort();980}, 1000);
62 unmodified lines