Skip to content

Commit

Permalink
Improve the TS types and don't accept null in the stdio option
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 18, 2019
1 parent 10c73dc commit 4692dcd
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 33 deletions.
25 changes: 10 additions & 15 deletions index.d.ts
Expand Up @@ -10,7 +10,6 @@ declare namespace execa {
| 'inherit'
| Stream
| number
| null
| undefined;

interface CommonOptions<EncodingType> {
Expand Down Expand Up @@ -45,7 +44,7 @@ declare namespace execa {
@default 'pipe'
*/
readonly stdio?: 'pipe' | 'ignore' | 'inherit' | ReadonlyArray<StdioOption>;
readonly stdio?: 'pipe' | 'ignore' | 'inherit' | readonly StdioOption[];

/**
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).
Expand Down Expand Up @@ -284,11 +283,7 @@ declare namespace execa {

interface ExecaChildPromise<StdoutErrorType> {
catch<ResultType = never>(
onRejected?:
| ((
reason: ExecaError<StdoutErrorType>
) => ResultType | PromiseLike<ResultType>)
| null
onRejected?: (reason: ExecaError<StdoutErrorType>) => ResultType | PromiseLike<ResultType>
): Promise<ExecaReturnValue<StdoutErrorType> | ResultType>;

/**
Expand Down Expand Up @@ -340,12 +335,12 @@ declare const execa: {
*/
(
file: string,
arguments?: ReadonlyArray<string>,
arguments?: readonly string[],
options?: execa.Options
): execa.ExecaChildProcess;
(
file: string,
arguments?: ReadonlyArray<string>,
arguments?: readonly string[],
options?: execa.Options<null>
): execa.ExecaChildProcess<Buffer>;
(file: string, options?: execa.Options): execa.ExecaChildProcess;
Expand All @@ -362,12 +357,12 @@ declare const execa: {
*/
stdout(
file: string,
arguments?: ReadonlyArray<string>,
arguments?: readonly string[],
options?: execa.Options
): Promise<string>;
stdout(
file: string,
arguments?: ReadonlyArray<string>,
arguments?: readonly string[],
options?: execa.Options<null>
): Promise<Buffer>;
stdout(file: string, options?: execa.Options): Promise<string>;
Expand All @@ -382,12 +377,12 @@ declare const execa: {
*/
stderr(
file: string,
arguments?: ReadonlyArray<string>,
arguments?: readonly string[],
options?: execa.Options
): Promise<string>;
stderr(
file: string,
arguments?: ReadonlyArray<string>,
arguments?: readonly string[],
options?: execa.Options<null>
): Promise<Buffer>;
stderr(file: string, options?: execa.Options): Promise<string>;
Expand Down Expand Up @@ -449,12 +444,12 @@ declare const execa: {
*/
sync(
file: string,
arguments?: ReadonlyArray<string>,
arguments?: readonly string[],
options?: execa.SyncOptions
): execa.ExecaSyncReturnValue;
sync(
file: string,
arguments?: ReadonlyArray<string>,
arguments?: readonly string[],
options?: execa.SyncOptions<null>
): execa.ExecaSyncReturnValue<Buffer>;
sync(file: string, options?: execa.SyncOptions): execa.ExecaSyncReturnValue;
Expand Down
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -69,7 +69,7 @@ function handleArgs(command, args, options) {
}

function handleInput(spawned, input) {
if (input === null || input === undefined) {
if (input === undefined) {
return;
}

Expand Down
5 changes: 1 addition & 4 deletions index.test-d.ts
Expand Up @@ -61,7 +61,7 @@ execa('unicorns', {stdio: 'pipe'});
execa('unicorns', {stdio: 'ignore'});
execa('unicorns', {stdio: 'inherit'});
execa('unicorns', {
stdio: ['pipe', 'ipc', 'ignore', 'inherit', process.stdin, 1, null, undefined]
stdio: ['pipe', 'ipc', 'ignore', 'inherit', process.stdin, 1, undefined]
});
execa('unicorns', {detached: true});
execa('unicorns', {uid: 0});
Expand All @@ -84,23 +84,20 @@ execa('unicorns', {stdin: 'ignore'});
execa('unicorns', {stdin: 'inherit'});
execa('unicorns', {stdin: process.stdin});
execa('unicorns', {stdin: 1});
execa('unicorns', {stdin: null});
execa('unicorns', {stdin: undefined});
execa('unicorns', {stderr: 'pipe'});
execa('unicorns', {stderr: 'ipc'});
execa('unicorns', {stderr: 'ignore'});
execa('unicorns', {stderr: 'inherit'});
execa('unicorns', {stderr: process.stderr});
execa('unicorns', {stderr: 1});
execa('unicorns', {stderr: null});
execa('unicorns', {stderr: undefined});
execa('unicorns', {stdout: 'pipe'});
execa('unicorns', {stdout: 'ipc'});
execa('unicorns', {stdout: 'ignore'});
execa('unicorns', {stdout: 'inherit'});
execa('unicorns', {stdout: process.stdout});
execa('unicorns', {stdout: 1});
execa('unicorns', {stdout: null});
execa('unicorns', {stdout: undefined});
execa('unicorns', {windowsVerbatimArguments: true});

Expand Down
20 changes: 10 additions & 10 deletions readme.md
Expand Up @@ -162,7 +162,7 @@ Returns the same result object as [`child_process.spawnSync`](https://nodejs.org

### options

Type: `Object`
Type: `object`

#### cwd

Expand All @@ -173,7 +173,7 @@ Current working directory of the child process.

#### env

Type: `Object`<br>
Type: `object`<br>
Default: `process.env`

Environment key-value pairs. Extends automatically from `process.env`. Set `extendEnv` to `false` if you don't want this.
Expand All @@ -193,7 +193,7 @@ Explicitly set the value of `argv[0]` sent to the child process. This will be se

#### stdio

Type: `string[]` `string`<br>
Type: `string | string[]`<br>
Default: `pipe`

Child's [stdio](https://nodejs.org/api/child_process.html#child_process_options_stdio) configuration.
Expand All @@ -218,7 +218,7 @@ Sets the group identity of the process.

#### shell

Type: `boolean` `string`<br>
Type: `boolean | string`<br>
Default: `false`

If `true`, runs `command` inside of a shell. Uses `/bin/sh` on UNIX and `cmd.exe` on Windows. A different shell can be specified as a string. The shell should understand the `-c` switch on UNIX or `/d /s /c` on Windows.
Expand Down Expand Up @@ -247,7 +247,7 @@ Preferred path to find locally installed binaries in (use with `preferLocal`).

#### input

Type: `string` `Buffer` `stream.Readable`
Type: `string | Buffer | stream.Readable`

Write some input to the `stdin` of your binary.<br>
Streams are not allowed when using the synchronous methods.
Expand All @@ -268,7 +268,7 @@ Keep track of the spawned process and `kill` it when the parent process exits.

#### encoding

Type: `string` `null`<br>
Type: `string | null`<br>
Default: `utf8`

Specify the character encoding used to decode the `stdout` and `stderr` output. If set to `null`, then `stdout` and `stderr` will be a `Buffer` instead of a string.
Expand Down Expand Up @@ -296,28 +296,28 @@ Largest amount of data in bytes allowed on `stdout` or `stderr`.

#### killSignal

Type: `string` `number`<br>
Type: `string | number`<br>
Default: `SIGTERM`

Signal value to be used when the spawned process will be killed.

#### stdin

Type: `string` `number` `Stream` `undefined` `null`<br>
Type: `string | number | Stream | undefined`<br>
Default: `pipe`

Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).

#### stdout

Type: `string` `number` `Stream` `undefined` `null`<br>
Type: `string | number | Stream | undefined`<br>
Default: `pipe`

Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).

#### stderr

Type: `string` `number` `Stream` `undefined` `null`<br>
Type: `string | number | Stream | undefined`<br>
Default: `pipe`

Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).
Expand Down
4 changes: 2 additions & 2 deletions test.js
Expand Up @@ -76,7 +76,7 @@ test('do not include `stderr` and `stdout` in errors when set to `inherit`', asy
});

test('do not include `stderr` and `stdout` in errors when `stdio` is set to `inherit`', async t => {
await t.throwsAsync(execa('fixtures/error-message.js', {stdio: [null, 'inherit', 'inherit']}), {message: NO_NEWLINES_REGEXP});
await t.throwsAsync(execa('fixtures/error-message.js', {stdio: [undefined, 'inherit', 'inherit']}), {message: NO_NEWLINES_REGEXP});
});

test('do not include `stdout` in errors when set to `inherit`', async t => {
Expand Down Expand Up @@ -213,7 +213,7 @@ test('input option can be a Buffer - sync', t => {
test('opts.stdout:ignore - stdout will not collect data', async t => {
const {stdout} = await execa('stdin', {
input: 'hello',
stdio: [null, 'ignore', null]
stdio: [undefined, 'ignore', undefined]
});
t.is(stdout, undefined);
});
Expand Down
1 change: 0 additions & 1 deletion test/stdio.js
Expand Up @@ -22,7 +22,6 @@ function macro(t, input, expected) {
macro.title = (providedTitle, input) => providedTitle || util.inspect(input, {colors: true});

test(macro, undefined, undefined);
test(macro, null, undefined);

test(macro, {stdio: 'inherit'}, 'inherit');
test(macro, {stdio: 'pipe'}, 'pipe');
Expand Down

0 comments on commit 4692dcd

Please sign in to comment.