Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Feb 11, 2023
1 parent 30c7a7a commit 09df476
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 55 deletions.
55 changes: 25 additions & 30 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Buffer} from 'node:buffer';
import {ChildProcess} from 'node:child_process';
import {Stream, Readable as ReadableStream} from 'node:stream';
import {type Buffer} from 'node:buffer';
import {type ChildProcess} from 'node:child_process';
import {type Stream, type Readable as ReadableStream} from 'node:stream';

export type StdioOption =
| 'pipe'
Expand All @@ -12,7 +12,7 @@ export type StdioOption =
| number
| undefined;

export interface CommonOptions<EncodingType> {
export type CommonOptions<EncodingType> = {
/**
Kill the spawned process when the parent process exits unless either:
- the spawned process is [`detached`](https://nodejs.org/api/child_process.html#child_process_options_detached)
Expand Down Expand Up @@ -244,23 +244,23 @@ export interface CommonOptions<EncodingType> {
@default true
*/
readonly windowsHide?: boolean;
}
};

export interface Options<EncodingType = string> extends CommonOptions<EncodingType> {
export type Options<EncodingType = string> = {
/**
Write some input to the `stdin` of your binary.
*/
readonly input?: string | Buffer | ReadableStream;
}
} & CommonOptions<EncodingType>;

export interface SyncOptions<EncodingType = string> extends CommonOptions<EncodingType> {
export type SyncOptions<EncodingType = string> = {
/**
Write some input to the `stdin` of your binary.
*/
readonly input?: string | Buffer;
}
} & CommonOptions<EncodingType>;

export interface NodeOptions<EncodingType = string> extends Options<EncodingType> {
export type NodeOptions<EncodingType = string> = {
/**
The Node.js executable to use.
Expand All @@ -274,9 +274,9 @@ export interface NodeOptions<EncodingType = string> extends Options<EncodingType
@default process.execArgv
*/
readonly nodeOptions?: string[];
}
} & Options<EncodingType>;

export interface ExecaReturnBase<StdoutStderrType> {
export type ExecaReturnBase<StdoutStderrType> = {
/**
The file and arguments that were run, for logging purposes.
Expand Down Expand Up @@ -335,11 +335,10 @@ export interface ExecaReturnBase<StdoutStderrType> {
If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. It is also `undefined` when the signal is very uncommon which should seldomly happen.
*/
signalDescription?: string;
}
};

export interface ExecaSyncReturnValue<StdoutErrorType = string>
extends ExecaReturnBase<StdoutErrorType> {
}
export type ExecaSyncReturnValue<StdoutErrorType = string> = {
} & ExecaReturnBase<StdoutErrorType>;

/**
Result of a child process execution. On success this is a plain object. On failure this is also an `Error` instance.
Expand All @@ -351,8 +350,7 @@ The child process fails when:
- being canceled
- there's not enough memory or there are already too many child processes
*/
export interface ExecaReturnValue<StdoutErrorType = string>
extends ExecaSyncReturnValue<StdoutErrorType> {
export type ExecaReturnValue<StdoutErrorType = string> = {
/**
The output of the process with `stdout` and `stderr` interleaved.
Expand All @@ -368,11 +366,9 @@ export interface ExecaReturnValue<StdoutErrorType = string>
You can cancel the spawned process using the [`signal`](https://github.com/sindresorhus/execa#signal-1) option.
*/
isCanceled: boolean;
}
} & ExecaSyncReturnValue<StdoutErrorType>;

export interface ExecaSyncError<StdoutErrorType = string>
extends Error,
ExecaReturnBase<StdoutErrorType> {
export type ExecaSyncError<StdoutErrorType = string> = {
/**
Error message when the child process failed to run. In addition to the underlying error message, it also contains some information related to why the child process errored.
Expand All @@ -391,10 +387,9 @@ export interface ExecaSyncError<StdoutErrorType = string>
This is `undefined` unless the child process exited due to an `error` event or a timeout.
*/
originalMessage?: string;
}
} & Error & ExecaReturnBase<StdoutErrorType>;

export interface ExecaError<StdoutErrorType = string>
extends ExecaSyncError<StdoutErrorType> {
export type ExecaError<StdoutErrorType = string> = {
/**
The output of the process with `stdout` and `stderr` interleaved.
Expand All @@ -408,9 +403,9 @@ export interface ExecaError<StdoutErrorType = string>
Whether the process was canceled.
*/
isCanceled: boolean;
}
} & ExecaSyncError<StdoutErrorType>;

export interface KillOptions {
export type KillOptions = {
/**
Milliseconds to wait for the child process to terminate before sending `SIGKILL`.
Expand All @@ -419,9 +414,9 @@ export interface KillOptions {
@default 5000
*/
forceKillAfterTimeout?: number | false;
}
};

export interface ExecaChildPromise<StdoutErrorType> {
export type ExecaChildPromise<StdoutErrorType> = {
/**
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).
Expand All @@ -444,7 +439,7 @@ export interface ExecaChildPromise<StdoutErrorType> {
Similar to [`childProcess.kill()`](https://nodejs.org/api/child_process.html#child_process_subprocess_kill_signal). This used to be preferred when cancelling the child process execution as the error is more descriptive and [`childProcessResult.isCanceled`](#iscanceled) is set to `true`. But now this is deprecated and you should either use `.kill()` or the `signal` option when creating the child process.
*/
cancel(): void;
}
};

export type ExecaChildProcess<StdoutErrorType = string> = ChildProcess &
ExecaChildPromise<StdoutErrorType> &
Expand Down
12 changes: 6 additions & 6 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import {Buffer} from 'node:buffer';
// `process.stdin`, `process.stderr`, and `process.stdout`
// to get treated as `any` by `@typescript-eslint/no-unsafe-assignment`.
import * as process from 'node:process';
import {Readable as ReadableStream} from 'node:stream';
import {type Readable as ReadableStream} from 'node:stream';
import {expectType, expectError} from 'tsd';
import {
execa,
execaSync,
execaCommand,
execaCommandSync,
execaNode,
ExecaReturnValue,
ExecaChildProcess,
ExecaError,
ExecaSyncReturnValue,
ExecaSyncError,
type ExecaReturnValue,
type ExecaChildProcess,
type ExecaError,
type ExecaSyncReturnValue,
type ExecaSyncError,
} from './index.js';

try {
Expand Down
2 changes: 2 additions & 0 deletions lib/promise.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// eslint-disable-next-line unicorn/prefer-top-level-await
const nativePromisePrototype = (async () => {})().constructor.prototype;

const descriptors = ['then', 'catch', 'finally'].map(property => [
property,
Reflect.getOwnPropertyDescriptor(nativePromisePrototype, property),
Expand Down
17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"dependencies": {
"cross-spawn": "^7.0.3",
"get-stream": "^6.0.1",
"human-signals": "^4.1.0",
"human-signals": "^4.3.0",
"is-stream": "^3.0.0",
"merge-stream": "^2.0.0",
"npm-run-path": "^5.1.0",
Expand All @@ -52,16 +52,16 @@
"strip-final-newline": "^3.0.0"
},
"devDependencies": {
"@types/node": "^17.0.17",
"ava": "^4.0.1",
"c8": "^7.11.0",
"get-node": "^12.0.0",
"@types/node": "^18.13.0",
"ava": "^5.2.0",
"c8": "^7.12.0",
"get-node": "^13.5.0",
"is-running": "^2.1.0",
"p-event": "^5.0.1",
"path-key": "^4.0.0",
"tempfile": "^4.0.0",
"tsd": "^0.19.1",
"xo": "^0.48.0"
"tsd": "^0.25.0",
"xo": "^0.53.1"
},
"c8": {
"reporter": [
Expand All @@ -74,6 +74,9 @@
"**/test/**"
]
},
"ava": {
"workerThreads": false
},
"xo": {
"rules": {
"unicorn/no-empty-file": "off",
Expand Down
16 changes: 6 additions & 10 deletions test/fixtures/sub-process-exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ import {execa} from '../../index.js';
const cleanup = process.argv[2] === 'true';
const detached = process.argv[3] === 'true';

const runChild = async () => {
try {
await execa('node', ['./test/fixtures/noop.js'], {cleanup, detached});
} catch (error) {
console.error(error);
process.exit(1);
}
};

runChild();
try {
await execa('node', ['./test/fixtures/noop.js'], {cleanup, detached});
} catch (error) {
console.error(error);
process.exit(1);
}
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ test('localDir option', async t => {
});

test('execPath option', async t => {
const {path: execPath} = await getNode('6.0.0');
const {path: execPath} = await getNode('16.0.0');
const {stdout} = await execa('node', ['-p', 'process.env.Path || process.env.PATH'], {preferLocal: true, execPath});
t.true(stdout.includes('6.0.0'));
t.true(stdout.includes('16.0.0'));
});

test('stdin errors are handled', async t => {
Expand Down

0 comments on commit 09df476

Please sign in to comment.