Add `IfAsync` type helper (#690)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 07ce2ff · Browse files · 2 files·+56 added−63 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+56 added−63 removed
- other:+56 added−63 removed
Provenance
- Commit
0298730- Tree
c5cb24a- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
07ce2ff
2 files changed+56 added−63 removed
modified:index.d.ts+36 added-43 removed
1 import {type ChildProcess} from 'node:child_process';2 import {type Readable, type Writable} from 'node:stream';3 4+type IfAsync<IsSync extends boolean, AsyncValue> = IsSync extends true ? never : AsyncValue;5+6 type NoOutputStdioOption =7 | 'ignore'8 | 'inherit'9 | 'ipc'15 unmodified lines25 | undefined26 | URL27 | {file: string};28 27-type InputStdioOption<IsSync extends boolean = boolean> = IsSync extends true28- ? Uint8Array29- : Iterable<string | Uint8Array>30- | AsyncIterable<string | Uint8Array>29+type InputStdioOption<IsSync extends boolean = boolean> =30 | Uint8Array31+ | IfAsync<IsSync,32+ | Iterable<string | Uint8Array>33+ | AsyncIterable<string | Uint8Array>34 | Readable33- | ReadableStream;35+ | ReadableStream>;36 35-type OutputStdioOption<IsSync extends boolean = boolean> = IsSync extends true36- ? never37- : Writable38- | WritableStream;37+type OutputStdioOption<IsSync extends boolean = boolean> = IfAsync<IsSync,38+| Writable39+| WritableStream>;40 41 export type StdinOption<IsSync extends boolean = boolean> =42 CommonStdioOption | InputStdioOption<IsSync>43 | Array<CommonStdioOption | InputStdioOption<IsSync>>;35 unmodified lines79 // Whether `result.stdout|stderr|all` is `undefined`, excluding the `buffer` option80 type IgnoresStreamResult<81 StreamIndex extends string,82 OptionsType extends CommonOptions = CommonOptions,82- // `result.stdin` is always `undefined`83-> = StreamIndex extends '0' ? true84- // When using `stdout|stderr: 'inherit'`, or `'ignore'`, etc. , `result.std*` is `undefined`85- : IgnoresNormalPropertyResult<StreamIndex, OptionsType> extends true ? true86- // Otherwise87- : IgnoresStdioPropertyResult<StreamIndex, OptionsType['stdio']>;83+> = IgnoresNormalPropertyResult<StreamIndex, OptionsType> extends true84+ ? true85+ : IgnoresStdioPropertyResult<StreamIndex, OptionsType['stdio']>;86 87 type IgnoresNormalPropertyResult<88 StreamIndex extends string,89 OptionsType extends CommonOptions = CommonOptions,92-> = StreamIndex extends keyof StdioOptionNames93- ? StdioOptionNames[StreamIndex] extends keyof OptionsType94- ? OptionsType[StdioOptionNames[StreamIndex]] extends NoOutputStdioOption95- ? true96- : false97- : false98- : false;90+ // `result.stdin` is always `undefined`91+> = StreamIndex extends '0' ? true92+ // When using `stdout: 'inherit'`, or `'ignore'`, etc. , `result.stdout` is `undefined`93+ : StreamIndex extends '1' ? OptionsType['stdout'] extends NoOutputStdioOption ? true : false94+ // Same with `stderr`95+ : StreamIndex extends '2' ? OptionsType['stderr'] extends NoOutputStdioOption ? true : false96+ // Otherwise97+ : false;98 100-type StdioOptionNames = ['stdin', 'stdout', 'stderr'];101-99 type IgnoresStdioPropertyResult<100 StreamIndex extends string,101 StdioOptionType extends StdioOptions | undefined,102 // Same but with `stdio: 'ignore'`12 unmodified lines115 StdioOptionType extends NoOutputStdioOption ? true116 // `result.stdio[3+]` is `undefined` when it is an input stream117 : StdioOptionType extends StdinOption118 ? StdioOptionType extends StdoutStderrOption122-119 ? false120 : true121 : false;122 123 // Whether `result.stdout|stderr|all` is `undefined`124 type IgnoresStreamOutput<125 StreamIndex extends string,126 OptionsType extends CommonOptions = CommonOptions,131-> = HasBuffer<OptionsType> extends false127+> = LacksBuffer<OptionsType['buffer']> extends true128 ? true129 : IgnoresStreamResult<StreamIndex, OptionsType>;130 135-type HasBuffer<OptionsType extends CommonOptions> = OptionsType extends Options136- ? HasBufferOption<OptionsType['buffer']>137- : true;131+type LacksBuffer<BufferOption extends Options['buffer']> = BufferOption extends false ? true : false;132 139-type HasBufferOption<BufferOption extends Options['buffer']> = BufferOption extends false ? false : true;140-133 // Type of `result.stdout|stderr`134 type StdioOutput<135 StreamIndex extends string,136 OptionsType extends CommonOptions = CommonOptions,77 unmodified lines214 Write some input to the child process' `stdin`.215 216 See also the `inputFile` and `stdin` options.217 */226- readonly input?: IsSync extends true ? string | Uint8Array : string | Uint8Array | Readable;218+ readonly input?: string | Uint8Array | IfAsync<IsSync, Readable>;219 220 /**221 Use a file as input to the child process' `stdin`.222 182 unmodified lines405 406 @default false407 */408 readonly verbose?: boolean;417-} & (IsSync extends true ? {} : {409+410 /**411 Kill the spawned process when the parent process exits unless either:412 - the spawned process is [`detached`](https://nodejs.org/api/child_process.html#child_process_options_detached)413 - the parent process is terminated abruptly, for example, with `SIGKILL` as opposed to `SIGTERM` or a normal exit414 415 @default true416 */425- readonly cleanup?: boolean;417+ readonly cleanup?: IfAsync<IsSync, boolean>;418 419 /**420 Buffer the output from the spawned process. When set to `false`, you must read the output of `stdout` and `stderr` (or `all` if the `all` option is `true`). Otherwise the returned promise will not be resolved/rejected.421 422 If the spawned process fails, `error.stdout`, `error.stderr`, and `error.all` will contain the buffered data.423 424 @default true425 */434- readonly buffer?: boolean;426+ readonly buffer?: IfAsync<IsSync, boolean>;427 428 /**429 Add an `.all` property on the promise and the resolved value. The property contains the output of the process with `stdout` and `stderr` interleaved.430 431 @default false432 */441- readonly all?: boolean;433+ readonly all?: IfAsync<IsSync, boolean>;434 435 /**436 Specify the kind of serialization used for sending messages between processes when using the `stdio: 'ipc'` option or `execaNode()`:437 - `json`: Uses `JSON.stringify()` and `JSON.parse()`.2 unmodified lines440 [More info.](https://nodejs.org/api/child_process.html#child_process_advanced_serialization)441 442 @default 'json'443 */452- readonly serialization?: 'json' | 'advanced';444+ readonly serialization?: IfAsync<IsSync, 'json' | 'advanced'>;445 446 /**447 Prepare child to run independently of its parent process. Specific behavior [depends on the platform](https://nodejs.org/api/child_process.html#child_process_options_detached).448 449 @default false450 */459- readonly detached?: boolean;451+ readonly detached?: IfAsync<IsSync, boolean>;452 453 /**454 You can abort the spawned process using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).455 17 unmodified lines473 console.log(error.isCanceled); // true474 }475 ```476 */485- readonly signal?: AbortSignal;486-});477+ readonly signal?: IfAsync<IsSync, AbortSignal>;478+};479 480 export type Options = CommonOptions<false>;481 export type SyncOptions = CommonOptions<true>;482 110 unmodified lines593 594 You can cancel the spawned process using the [`signal`](https://github.com/sindresorhus/execa#signal-1) option.595 */596 isCanceled: boolean;605-} & (IsSync extends true ? {} : {597+598 /**599 The output of the process with `stdout` and `stderr` interleaved.600 601 This is `undefined` if either:602 - the `all` option is `false` (default value)603 - 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)604 */613- all: AllOutput<OptionsType>;614-});605+ all: IfAsync<IsSync, AllOutput<OptionsType>>;606+ // Workaround for a TypeScript bug: https://github.com/microsoft/TypeScript/issues/57062607+} & {};608 609 export type ExecaReturnValue<OptionsType extends Options = Options> = ExecaCommonReturnValue<false, OptionsType> & ErrorUnlessReject<OptionsType['reject']>;610 export type ExecaSyncReturnValue<OptionsType extends SyncOptions = SyncOptions> = ExecaCommonReturnValue<true, OptionsType> & ErrorUnlessReject<OptionsType['reject']>;611 602 unmodified linesmodified:index.test-d.ts+20 added-20 removed
412 unmodified lines413 expectType<string>(unicornsResult.stdout);414 expectType<string>(unicornsResult.stdio[1]);415 expectType<string>(unicornsResult.stderr);416 expectType<string>(unicornsResult.stdio[2]);417- expectError(unicornsResult.all);417+ expectError(unicornsResult.all.toString());418 419 const bufferResult = execaSync('unicorns', {encoding: 'buffer'});420 expectType<Uint8Array>(bufferResult.stdout);421 expectType<Uint8Array>(bufferResult.stdio[1]);422 expectType<Uint8Array>(bufferResult.stderr);423 expectType<Uint8Array>(bufferResult.stdio[2]);424- expectError(bufferResult.all);424+ expectError(bufferResult.all.toString());425 426 const ignoreStdoutResult = execaSync('unicorns', {stdout: 'ignore'});427 expectType<undefined>(ignoreStdoutResult.stdout);428 expectType<undefined>(ignoreStdoutResult.stdio[1]);429 expectType<string>(ignoreStdoutResult.stderr);430 expectType<string>(ignoreStdoutResult.stdio[2]);431- expectError(ignoreStdoutResult.all);431+ expectError(ignoreStdoutResult.all.toString());432 433 const ignoreStderrResult = execaSync('unicorns', {stderr: 'ignore'});434 expectType<string>(ignoreStderrResult.stdout);435 expectType<undefined>(ignoreStderrResult.stderr);436- expectError(ignoreStderrResult.all);436+ expectError(ignoreStderrResult.all.toString());437 438 const inheritStdoutResult = execaSync('unicorns', {stdout: 'inherit'});439 expectType<undefined>(inheritStdoutResult.stdout);440 expectType<string>(inheritStdoutResult.stderr);441- expectError(inheritStdoutResult.all);441+ expectError(inheritStdoutResult.all.toString());442 443 const inheritStderrResult = execaSync('unicorns', {stderr: 'inherit'});444 expectType<string>(inheritStderrResult.stdout);445 expectType<undefined>(inheritStderrResult.stderr);446- expectError(inheritStderrResult.all);446+ expectError(inheritStderrResult.all.toString());447 448 const ipcStdoutResult = execaSync('unicorns', {stdout: 'ipc'});449 expectType<undefined>(ipcStdoutResult.stdout);450 expectType<string>(ipcStdoutResult.stderr);451- expectError(ipcStdoutResult.all);451+ expectError(ipcStdoutResult.all.toString());452 453 const ipcStderrResult = execaSync('unicorns', {stderr: 'ipc'});454 expectType<string>(ipcStderrResult.stdout);455 expectType<undefined>(ipcStderrResult.stderr);456- expectError(ipcStderrResult.all);456+ expectError(ipcStderrResult.all.toString());457 458 const numberStdoutResult = execaSync('unicorns', {stdout: 1});459 expectType<undefined>(numberStdoutResult.stdout);460 expectType<string>(numberStdoutResult.stderr);461- expectError(numberStdoutResult.all);461+ expectError(numberStdoutResult.all.toString());462 463 const numberStderrResult = execaSync('unicorns', {stderr: 1});464 expectType<string>(numberStderrResult.stdout);465 expectType<undefined>(numberStderrResult.stderr);466- expectError(numberStderrResult.all);466+ expectError(numberStderrResult.all.toString());467 } catch (error: unknown) {468 const execaError = error as ExecaSyncError;469 470 expectType<ExecaSyncError>(execaError);15 unmodified lines486 expectType<string>(execaStringError.stdout);487 expectType<string>(execaStringError.stdio[1]);488 expectType<string>(execaStringError.stderr);489 expectType<string>(execaStringError.stdio[2]);490- expectError(execaStringError.all);490+ expectError(execaStringError.all.toString());491 492 const execaBufferError = error as ExecaSyncError<{encoding: 'buffer'}>;493 expectType<Uint8Array>(execaBufferError.stdout);494 expectType<Uint8Array>(execaBufferError.stdio[1]);495 expectType<Uint8Array>(execaBufferError.stderr);496 expectType<Uint8Array>(execaBufferError.stdio[2]);497- expectError(execaBufferError.all);497+ expectError(execaBufferError.all.toString());498 499 const ignoreStdoutError = error as ExecaSyncError<{stdout: 'ignore'}>;500 expectType<undefined>(ignoreStdoutError.stdout);501 expectType<undefined>(ignoreStdoutError.stdio[1]);502 expectType<string>(ignoreStdoutError.stderr);503 expectType<string>(ignoreStdoutError.stdio[2]);504- expectError(ignoreStdoutError.all);504+ expectError(ignoreStdoutError.all.toString());505 506 const ignoreStderrError = error as ExecaSyncError<{stderr: 'ignore'}>;507 expectType<string>(ignoreStderrError.stdout);508 expectType<undefined>(ignoreStderrError.stderr);509- expectError(ignoreStderrError.all);509+ expectError(ignoreStderrError.all.toString());510 511 const inheritStdoutError = error as ExecaSyncError<{stdout: 'inherit'}>;512 expectType<undefined>(inheritStdoutError.stdout);513 expectType<string>(inheritStdoutError.stderr);514- expectError(inheritStdoutError.all);514+ expectError(inheritStdoutError.all.toString());515 516 const inheritStderrError = error as ExecaSyncError<{stderr: 'inherit'}>;517 expectType<string>(inheritStderrError.stdout);518 expectType<undefined>(inheritStderrError.stderr);519- expectError(inheritStderrError.all);519+ expectError(inheritStderrError.all.toString());520 521 const ipcStdoutError = error as ExecaSyncError<{stdout: 'ipc'}>;522 expectType<undefined>(ipcStdoutError.stdout);523 expectType<string>(ipcStdoutError.stderr);524- expectError(ipcStdoutError.all);524+ expectError(ipcStdoutError.all.toString());525 526 const ipcStderrError = error as ExecaSyncError<{stderr: 'ipc'}>;527 expectType<string>(ipcStderrError.stdout);528 expectType<undefined>(ipcStderrError.stderr);529- expectError(ipcStderrError.all);529+ expectError(ipcStderrError.all.toString());530 531 const numberStdoutError = error as ExecaSyncError<{stdout: 1}>;532 expectType<undefined>(numberStdoutError.stdout);533 expectType<string>(numberStdoutError.stderr);534- expectError(numberStdoutError.all);534+ expectError(numberStdoutError.all.toString());535 536 const numberStderrError = error as ExecaSyncError<{stderr: 1}>;537 expectType<string>(numberStderrError.stdout);538 expectType<undefined>(numberStderrError.stderr);539- expectError(numberStderrError.all);539+ expectError(numberStderrError.all.toString());540 }541 542 const rejectsSyncResult = execaSync('unicorns');543 expectError(rejectsSyncResult.stack);550 unmodified lines