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
Provenance
- Commit
231beaf- Tree
430db1b- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
7d0943f
9 files changed+115 added−102 removed
modified:docs/scripts.md+1 added-1 removed
44 unmodified lines45 Execa's scripting API mostly consists of only two methods: [`` $`command` ``](../readme.md#command) and [`$(options)`](../readme.md#options).46 47 [No special binary](#main-binary) is recommended, no [global variable](#global-variables) is injected: scripts are regular Node.js files.48 49-Execa 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.49+Execa 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.50 51 ### Modularity52 53 zx 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 linesmodified:lib/stream/exit.js+1 added-1 removed
12344567823 unmodified linesimport {once} from 'node:events';
// If `error` is emitted before `spawn`, `exit` will never be emitted.// However, `error` might be emitted after `spawn`, e.g. with the `signal` option.// However, `error` might be emitted after `spawn`, e.g. with the `cancelSignal` option.// In that case, `exit` will still be emitted.// Since the `exit` event contains the signal name, we want to make sure we are listening for it.// This function also takes into account the following unlikely cases:// - `exit` being emitted in the same microtask as `spawn`23 unmodified linesmodified:lib/async.js+7 added-1 removed
30 unmodified lines313233343535363738394041424344454649 unmodified lines30 unmodified lines return {file, args, command, escapedCommand, options, stdioStreamsGroups};};
// Prevent passing the `timeout` option directly to `child_process.spawn()`const handleAsyncOptions = ({timeout, ...options}) => ({...options, timeoutDuration: timeout});const handleAsyncOptions = ({timeout, signal, cancelSignal, ...options}) => { if (signal !== undefined) { throw new TypeError('The "signal" option has been renamed to "cancelSignal" instead.'); }
return {...options, timeoutDuration: timeout, signal: cancelSignal};};
const spawnProcessAsync = ({file, args, options, command, escapedCommand, stdioStreamsGroups}) => { let spawned; try { spawned = spawn(file, args, options);49 unmodified linesadded:test/exit/cancel.js+85 added
1+import {once} from 'node:events';2+import test from 'ava';3+import {execa, execaSync} from '../../index.js';4+import {setFixtureDir} from '../helpers/fixtures-dir.js';5+6+setFixtureDir();7+8+test('result.isCanceled is false when abort isn\'t called (success)', async t => {9+ const {isCanceled} = await execa('noop.js');10+ t.false(isCanceled);11+});12+13+test('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+});17+18+test('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+});22+23+test('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+});29+30+test('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+});37+38+test('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+});45+46+test('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+});55+56+test('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+});62+63+test('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+});71+72+test('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+});79+80+test('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 lines97 test('`forceKillAfterDelay` should kill after a timeout with an error and a killSignal', testForceKill, 50, new Error('test'), {killSignal: 'SIGINT'});98 99 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 lines279 });280 281 test('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 linesdeleted:test/exit/signal.js-78 removed
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778import {once} from 'node:events';import test from 'ava';import {execa, execaSync} from '../../index.js';import {setFixtureDir} from '../helpers/fixtures-dir.js';
setFixtureDir();
test('result.isCanceled is false when abort isn\'t called (success)', async t => { const {isCanceled} = await execa('noop.js'); t.false(isCanceled);});
test('result.isCanceled is false when abort isn\'t called (failure)', async t => { const {isCanceled} = await t.throwsAsync(execa('fail.js')); t.false(isCanceled);});
test('result.isCanceled is false when abort isn\'t called in sync mode (success)', t => { const {isCanceled} = execaSync('noop.js'); t.false(isCanceled);});
test('result.isCanceled is false when abort isn\'t called in sync mode (failure)', t => { const {isCanceled} = t.throws(() => { execaSync('fail.js'); }); t.false(isCanceled);});
test('error.isCanceled is true when abort is used', async t => { const abortController = new AbortController(); const subprocess = execa('noop.js', {signal: abortController.signal}); abortController.abort(); const {isCanceled} = await t.throwsAsync(subprocess); t.true(isCanceled);});
test('error.isCanceled is false when kill method is used', async t => { const abortController = new AbortController(); const subprocess = execa('noop.js', {signal: abortController.signal}); subprocess.kill(); const {isCanceled} = await t.throwsAsync(subprocess); t.false(isCanceled);});
test('calling abort is considered a signal termination', async t => { const abortController = new AbortController(); const subprocess = execa('forever.js', {signal: abortController.signal}); await once(subprocess, 'spawn'); abortController.abort(); const {isTerminated, signal} = await t.throwsAsync(subprocess); t.true(isTerminated); t.is(signal, 'SIGTERM');});
test('calling abort throws an error with message "Command was canceled"', async t => { const abortController = new AbortController(); const subprocess = execa('noop.js', {signal: abortController.signal}); abortController.abort(); await t.throwsAsync(subprocess, {message: /Command was canceled/});});
test('calling abort twice should show the same behaviour as calling it once', async t => { const abortController = new AbortController(); const subprocess = execa('noop.js', {signal: abortController.signal}); abortController.abort(); abortController.abort(); const {isCanceled} = await t.throwsAsync(subprocess); t.true(isCanceled);});
test('calling abort on a successfully completed process does not make result.isCanceled true', async t => { const abortController = new AbortController(); const subprocess = execa('noop.js', {signal: abortController.signal}); const result = await subprocess; abortController.abort(); t.false(result.isCanceled);});modified:index.d.ts+10 added-10 removed
497 unmodified lines498 readonly maxBuffer?: number;499 500 /**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 arguments504 505 This can be either a name (like `"SIGTERM"`) or a number (like `9`).506 6 unmodified lines513 514 The grace period is 5 seconds by default. This feature can be disabled with `false`.515 516 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 arguments519 520 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 argument90 unmodified lines612 ```613 import {execa} from 'execa';614 615 const abortController = new AbortController();616- const subprocess = execa('node', [], {signal: abortController.signal});616+ const subprocess = execa('node', [], {cancelSignal: abortController.signal});617 618 setTimeout(() => {619 abortController.abort();620 }, 1000);5 unmodified lines626 console.log(error.isCanceled); // true627 }628 ```629 */630- readonly signal?: IfAsync<IsSync, AbortSignal>;630+ readonly cancelSignal?: IfAsync<IsSync, AbortSignal>;631 };632 633 export type Options = CommonOptions<false>;634 export type SyncOptions = CommonOptions<true>;28 unmodified lines663 664 /**665 The numeric exit code of the process that was run.666 667- 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;670 671 /**55 unmodified lines727 */728 cwd: string;729 730 /**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;734 735 /**291 unmodified lines1027 1028 /**1029 Same as `execa()` but synchronous.1030 1031-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.1031+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.1032 1033 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.1034 1035 @param file - The program/script to execute, as a string or file URL92 unmodified lines1128 1129 /**1130 Same as `execaCommand()` but synchronous.1131 1132-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.1132+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.1133 1134 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.1135 1136 @param command - The program/script to execute and its arguments.49 unmodified lines1186 1187 /**1188 Same as $\`command\` but synchronous.1189 1190- 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.1191 1192 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.1193 1194 @returns A `childProcessResult` object45 unmodified lines1240 1241 /**1242 Same as $\`command\` but synchronous.1243 1244- 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.1245 1246 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.1247 1248 @returns A `childProcessResult` object132 unmodified linesmodified:index.test-d.ts+2 added-2 removed
1432 unmodified lines1433 execa('unicorns', {forceKillAfterDelay: undefined});1434 execaSync('unicorns', {forceKillAfterDelay: undefined});1435 expectError(execa('unicorns', {forceKillAfterDelay: 'true'}));1436 expectError(execaSync('unicorns', {forceKillAfterDelay: 'true'}));1437-execa('unicorns', {signal: new AbortController().signal});1438-expectError(execaSync('unicorns', {signal: new AbortController().signal}));1437+execa('unicorns', {cancelSignal: new AbortController().signal});1438+expectError(execaSync('unicorns', {cancelSignal: new AbortController().signal}));1439 execa('unicorns', {windowsVerbatimArguments: true});1440 execaSync('unicorns', {windowsVerbatimArguments: true});1441 execa('unicorns', {windowsHide: false});1442 execaSync('unicorns', {windowsHide: false});121 unmodified linesmodified:readme.md+7 added-7 removed
313 unmodified lines314 #### $.s(options)\`command\`315 316 Same as [`execa()`](#execacommandcommand-options), [`execaCommand()`](#execacommand-command-options), [$\`command\`](#command) but synchronous.317 318-Cannot 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.318+Cannot 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.319 320 Returns 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.321 322 ### Shell syntax201 unmodified lines524 #### isCanceled525 526 Type: `boolean`527 528-Whether the process was canceled using the [`signal`](#signal-1) option.528+Whether the process was canceled using the [`cancelSignal`](#cancelsignal) option.529 530 #### isTerminated531 532 Type: `boolean`7 unmodified lines540 Type: `number | undefined`541 542 The numeric exit code of the process that was run.543 544-This is `undefined` when the process could not be spawned or was terminated by a [signal](#signal-1).544+This is `undefined` when the process could not be spawned or was terminated by a [signal](#signal).545 546 #### signal547 548 Type: `string | undefined`300 unmodified lines849 Default: `0`850 851 If `timeout` is greater than `0`, the child process will be [terminated](#killsignal) if it runs for longer than that amount of milliseconds.852 853-#### signal853+#### cancelSignal854 855 Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)856 857 You can abort the spawned process using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).9 unmodified lines867 868 The grace period is 5 seconds by default. This feature can be disabled with `false`.869 870 This 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 arguments873 874 This 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 argument7 unmodified lines883 Type: `string | number`\884 Default: `SIGTERM`885 886 Signal 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 arguments889 890 This can be either a name (like `"SIGTERM"`) or a number (like `9`).891 80 unmodified lines972 ```js973 import {execa} from 'execa';974 975 const abortController = new AbortController();976-const subprocess = execa('node', [], {signal: abortController.signal});976+const subprocess = execa('node', [], {cancelSignal: abortController.signal});977 978 setTimeout(() => {979 abortController.abort();980 }, 1000);62 unmodified lines