Skip to content
Myr

Add TypeScript definition (#188)

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

authored by Dimitri Benin · committed by Sindre Sorhus · parent f24e7c7 · Browse files · 4 files+669 added−7 removed
This commit

State

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

Diff stats

4 files+669 added−7 removed

  • other:+669 added−7 removed

4 files+669 added−7 removed

added:index.d.ts+494 added
1/// <reference types="node"/>2import {ChildProcess} from 'child_process';3import {Stream, Readable as ReadableStream} from 'stream';45export type StdioOption =6 | 'pipe'7 | 'ipc'8 | 'ignore'9 | 'inherit'10 | Stream11 | number12 | null13 | undefined;1415export interface CommonOptions<EncodingType> {16 /**17 Current working directory of the child process.1819 @default process.cwd()20 */21 readonly cwd?: string;2223 /**24 Environment key-value pairs. Extends automatically from `process.env`. Set `extendEnv` to `false` if you don't want this.2526 @default process.env27 */28 readonly env?: NodeJS.ProcessEnv;2930 /**31 Set to `false` if you don't want to extend the environment variables when providing the `env` property.3233 @default true34 */35 readonly extendEnv?: boolean;3637 /**38 Explicitly set the value of `argv[0]` sent to the child process. This will be set to `command` or `file` if not specified.39 */40 readonly argv0?: string;4142 /**43 Child's [stdio](https://nodejs.org/api/child_process.html#child_process_options_stdio) configuration.4445 @default 'pipe'46 */47 readonly stdio?: 'pipe' | 'ignore' | 'inherit' | ReadonlyArray<StdioOption>;4849 /**50 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).5152 @default false53 */54 readonly detached?: boolean;5556 /**57 Sets the user identity of the process.58 */59 readonly uid?: number;6061 /**62 Sets the group identity of the process.63 */64 readonly gid?: number;6566 /**67 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.6869 @default false70 */71 readonly shell?: boolean | string;7273 /**74 Strip the final [newline character](https://en.wikipedia.org/wiki/Newline) from the output.7576 @default true77 */78 readonly stripFinalNewline?: boolean;7980 /**81 Prefer locally installed binaries when looking for a binary to execute.8283 If you `$ npm install foo`, you can then `execa('foo')`.8485 @default true86 */87 readonly preferLocal?: boolean;8889 /**90 Preferred path to find locally installed binaries in (use with `preferLocal`).9192 @default process.cwd()93 */94 readonly localDir?: string;9596 /**97 Setting this to `false` resolves the promise with the error instead of rejecting it.9899 @default true100 */101 readonly reject?: boolean;102103 /**104 Keep track of the spawned process and `kill` it when the parent process exits.105106 @default true107 */108 readonly cleanup?: boolean;109110 /**111 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.112113 @default 'utf8'114 */115 readonly encoding?: EncodingType;116117 /**118 If `timeout` is greater than `0`, the parent will send the signal identified by the `killSignal` property (the default is `SIGTERM`) if the child runs longer than `timeout` milliseconds.119120 @default 0121 */122 readonly timeout?: number;123124 /**125 Buffer the output from the spawned process. When buffering is disabled you must consume the output of the `stdout` and `stderr` streams because the promise will not be resolved/rejected until they have completed.126127 @default true128 */129 readonly buffer?: boolean;130131 /**132 Largest amount of data in bytes allowed on `stdout` or `stderr`. Default: 10MB.133134 @default 10000000135 */136 readonly maxBuffer?: number;137138 /**139 Signal value to be used when the spawned process will be killed.140141 @default 'SIGTERM'142 */143 readonly killSignal?: string | number;144145 /**146 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).147148 @default 'pipe'149 */150 readonly stdin?: StdioOption;151152 /**153 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).154155 @default 'pipe'156 */157 readonly stdout?: StdioOption;158159 /**160 Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).161162 @default 'pipe'163 */164 readonly stderr?: StdioOption;165166 /**167 If `true`, no quoting or escaping of arguments is done on Windows. Ignored on other platforms. This is set to `true` automatically when the `shell` option is `true`.168169 @default false170 */171 readonly windowsVerbatimArguments?: boolean;172}173174export interface Options<EncodingType = string>175 extends CommonOptions<EncodingType> {176 /**177 Write some input to the `stdin` of your binary.178 */179 readonly input?: string | Buffer | ReadableStream;180}181182export interface SyncOptions<EncodingType = string>183 extends CommonOptions<EncodingType> {184 /**185 Write some input to the `stdin` of your binary.186 */187 readonly input?: string | Buffer;188}189190export interface ExecaReturnBase<StdoutStderrType> {191 /**192 The numeric exit code of the process that was run.193 */194 exitCode: number;195196 /**197 The textual exit code of the process that was run.198 */199 exitCodeName: string;200201 /**202 The output of the process on stdout.203 */204 stdout: StdoutStderrType;205206 /**207 The output of the process on stderr.208 */209 stderr: StdoutStderrType;210211 /**212 Whether the process failed to run.213 */214 failed: boolean;215216 /**217 The signal that was used to terminate the process.218 */219 signal?: string;220221 /**222 The command that was run.223 */224 cmd: string;225226 /**227 Whether the process timed out.228 */229 timedOut: boolean;230231 /**232 Whether the process was killed.233 */234 killed: boolean;235}236237export interface ExecaSyncReturnValue<StdOutErrType = string>238 extends ExecaReturnBase<StdOutErrType> {239 /**240 The exit code of the process that was run.241 */242 code: number;243}244245export interface ExecaReturnValue<StdOutErrType = string>246 extends ExecaSyncReturnValue<StdOutErrType> {247 /**248 The output of the process with `stdout` and `stderr` interleaved.249 */250 all: StdOutErrType;251252 /**253 Whether the process was canceled.254 */255 isCanceled: boolean;256}257258export interface ExecaSyncError<StdOutErrType = string>259 extends Error,260 ExecaReturnBase<StdOutErrType> {261 /**262 The error message.263 */264 message: string;265266 /**267 The exit code (either numeric or textual) of the process that was run.268 */269 code: number | string;270}271272export interface ExecaError<StdOutErrType = string>273 extends ExecaSyncError<StdOutErrType> {274 /**275 The output of the process with `stdout` and `stderr` interleaved.276 */277 all: StdOutErrType;278279 /**280 Whether the process was canceled.281 */282 isCanceled: boolean;283}284285export interface ExecaChildPromise<StdOutErrType> {286 catch<ResultType = never>(287 onRejected?:288 | ((289 reason: ExecaError<StdOutErrType>290 ) => ResultType | PromiseLike<ResultType>)291 | null292 ): Promise<ExecaReturnValue<StdOutErrType> | ResultType>;293294 /**295 Cancel the subprocess.296297 Causes the promise to reject an error with a `.isCanceled = true` property, provided the process gets canceled. The process will not be canceled if it has already exited.298 */299 cancel(): void;300}301302export type ExecaChildProcess<StdOutErrType = string> = ChildProcess &303 ExecaChildPromise<StdOutErrType> &304 Promise<ExecaReturnValue<StdOutErrType>>;305306declare const execa: {307 /**308 Execute a file.309310 Think of this as a mix of `child_process.execFile` and `child_process.spawn`.311312 @param file - The program/script to execute.313 @param arguments - Arguments to pass to `file` on execution.314 @returns A [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess), which is enhanced to also be a `Promise` for a result `Object` with `stdout` and `stderr` properties.315316 @example317 ```318 import execa from 'execa';319320 (async () => {321 const {stdout} = await execa('echo', ['unicorns']);322 console.log(stdout);323 //=> 'unicorns'324325 // Cancelling a spawned process326 const subprocess = execa('node');327 setTimeout(() => { spawned.cancel() }, 1000);328 try {329 await subprocess;330 } catch (error) {331 console.log(subprocess.killed); // true332 console.log(error.isCanceled); // true333 }334 })();335336 // Pipe the child process stdout to the current stdout337 execa('echo', ['unicorns']).stdout.pipe(process.stdout);338 ```339 */340 (341 file: string,342 arguments?: ReadonlyArray<string>,343 options?: Options344 ): ExecaChildProcess;345 (346 file: string,347 arguments?: ReadonlyArray<string>,348 options?: Options<null>349 ): ExecaChildProcess<Buffer>;350 (file: string, options?: Options): ExecaChildProcess;351 (file: string, options?: Options<null>): ExecaChildProcess<Buffer>;352353 /**354 Same as `execa()`, but returns only `stdout`.355356 @param file - The program/script to execute.357 @param arguments - Arguments to pass to `file` on execution.358 @returns The contents of the executed process' `stdout`.359 */360 stdout(361 file: string,362 arguments?: ReadonlyArray<string>,363 options?: Options364 ): Promise<string>;365 stdout(366 file: string,367 arguments?: ReadonlyArray<string>,368 options?: Options<null>369 ): Promise<Buffer>;370 stdout(file: string, options?: Options): Promise<string>;371 stdout(file: string, options?: Options<null>): Promise<Buffer>;372373 /**374 Same as `execa()`, but returns only `stderr`.375376 @param file - The program/script to execute.377 @param arguments - Arguments to pass to `file` on execution.378 @returns The contents of the executed process' `stderr`.379 */380 stderr(381 file: string,382 arguments?: ReadonlyArray<string>,383 options?: Options384 ): Promise<string>;385 stderr(386 file: string,387 arguments?: ReadonlyArray<string>,388 options?: Options<null>389 ): Promise<Buffer>;390 stderr(file: string, options?: Options): Promise<string>;391 stderr(file: string, options?: Options<null>): Promise<Buffer>;392393 /**394 Execute a command through the system shell.395396 Prefer `execa()` whenever possible, as it's both faster and safer.397398 @param command - The command to execute.399 @returns A [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess).400401 @example402 ```403 import execa from 'execa';404405 (async => {406 // Run a shell command407 const {stdout} = await execa.shell('echo unicorns');408 //=> 'unicorns'409410 // Catching an error411 try {412 await execa.shell('exit 3');413 } catch (error) {414 console.log(error);415 //{416 // message: 'Command failed with exit code 3 (ESRCH): exit 3',417 // code: 3,418 // exitCode: 3,419 // exitCodeName: 'ESRCH',420 // stdout: '',421 // stderr: '',422 // all: '',423 // failed: true,424 // cmd: 'exit 3',425 // timedOut: false,426 // killed: false427 //}428 }429 })();430 ```431 */432 shell(command: string, options?: Options): ExecaChildProcess;433 shell(command: string, options?: Options<null>): ExecaChildProcess<Buffer>;434435 /**436 Execute a file synchronously.437438 This method throws an `Error` if the command fails.439440 @param file - The program/script to execute.441 @param arguments - Arguments to pass to `file` on execution.442 @returns The same result object as [`child_process.spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options).443 */444 sync(445 file: string,446 arguments?: ReadonlyArray<string>,447 options?: SyncOptions448 ): ExecaSyncReturnValue;449 sync(450 file: string,451 arguments?: ReadonlyArray<string>,452 options?: SyncOptions<null>453 ): ExecaSyncReturnValue<Buffer>;454 sync(file: string, options?: SyncOptions): ExecaSyncReturnValue;455 sync(file: string, options?: SyncOptions<null>): ExecaSyncReturnValue<Buffer>;456457 /**458 Execute a command synchronously through the system shell.459460 This method throws an `Error` if the command fails.461462 @param command - The command to execute.463 @returns The same result object as [`child_process.spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options).464465 @example466 ```467 import execa from 'execa';468469 try {470 execa.shellSync('exit 3');471 } catch (error) {472 console.log(error);473 //{474 // message: 'Command failed with exit code 3 (ESRCH): exit 3',475 // code: 3,476 // exitCode: 3,477 // exitCodeName: 'ESRCH',478 // stdout: '',479 // stderr: '',480 // failed: true,481 // cmd: 'exit 3',482 // timedOut: false483 //}484 }485 ```486 */487 shellSync(command: string, options?: Options): ExecaSyncReturnValue;488 shellSync(489 command: string,490 options?: Options<null>491 ): ExecaSyncReturnValue<Buffer>;492};493494export default execa;
modified:index.js+8 added-5 removed
223 unmodified lines
224225 return joinedCommand;226}227228module.exports = (command, args, options) => {228const execa = (command, args, options) => {229 const parsed = handleArgs(command, args, options);230 const {encoding, buffer, maxBuffer} = parsed.options;231 const joinedCommand = joinCommand(command, args);232
140 unmodified lines
373374 return spawned;375};376377module.exports = execa;378module.exports.default = execa;379380// TODO: set `stderr: 'ignore'` when that option is implemented381module.exports.stdout = async (...args) => {379 const {stdout} = await module.exports(...args);382 const {stdout} = await execa(...args);383 return stdout;384};385386// TODO: set `stdout: 'ignore'` when that option is implemented387module.exports.stderr = async (...args) => {385 const {stderr} = await module.exports(...args);388 const {stderr} = await execa(...args);389 return stderr;390};391389module.exports.shell = (command, options) => handleShell(module.exports, command, options);392module.exports.shell = (command, options) => handleShell(execa, command, options);393394module.exports.sync = (command, args, options) => {395 const parsed = handleArgs(command, args, options);396 const joinedCommand = joinCommand(command, args);
29 unmodified lines
426 timedOut: false427 };428};429427module.exports.shellSync = (command, options) => handleShell(module.exports.sync, command, options);430module.exports.shellSync = (command, options) => handleShell(execa.sync, command, options);
added:index.test-d.ts+162 added
1import {expectType, expectError} from 'tsd';2import execa, {3 ExecaReturnValue,4 ExecaChildProcess,5 ExecaError,6 ExecaSyncReturnValue,7 ExecaSyncError8} from '.';910try {11 const execaPromise = execa('unicorns');12 execaPromise.cancel();1314 const unicornsResult = await execaPromise;15 expectType<string>(unicornsResult.cmd);16 expectType<string | number>(unicornsResult.code);17 expectType<boolean>(unicornsResult.failed);18 expectType<boolean>(unicornsResult.killed);19 expectType<string | undefined>(unicornsResult.signal);20 expectType<string>(unicornsResult.stderr);21 expectType<string>(unicornsResult.stdout);22 expectType<string>(unicornsResult.all);23 expectType<boolean>(unicornsResult.timedOut);24 expectType<boolean>(unicornsResult.isCanceled);25} catch (error) {26 const execaError: ExecaError = error;2728 expectType<string>(execaError.message);29 expectType<number | string>(execaError.code);30 expectType<string>(execaError.all);31 expectType<boolean>(execaError.isCanceled);32}3334try {35 const unicornsResult = execa.sync('unicorns');36 expectType<string>(unicornsResult.cmd);37 expectType<string | number>(unicornsResult.code);38 expectType<boolean>(unicornsResult.failed);39 expectType<boolean>(unicornsResult.killed);40 expectType<string | undefined>(unicornsResult.signal);41 expectType<string>(unicornsResult.stderr);42 expectType<string>(unicornsResult.stdout);43 expectType<boolean>(unicornsResult.timedOut);44 expectError(unicornsResult.all);45 expectError(unicornsResult.isCanceled);46} catch (error) {47 const execaError: ExecaSyncError = error;4849 expectType<string>(execaError.message);50 expectType<number | string>(execaError.code);51 expectError(execaError.all);52 expectError(execaError.isCanceled);53}5455execa('unicorns', {cwd: '.'});56execa('unicorns', {env: {PATH: ''}});57execa('unicorns', {extendEnv: false});58execa('unicorns', {argv0: ''});59execa('unicorns', {stdio: 'pipe'});60execa('unicorns', {stdio: 'ignore'});61execa('unicorns', {stdio: 'inherit'});62execa('unicorns', {63 stdio: ['pipe', 'ipc', 'ignore', 'inherit', process.stdin, 1, null, undefined]64});65execa('unicorns', {detached: true});66execa('unicorns', {uid: 0});67execa('unicorns', {gid: 0});68execa('unicorns', {shell: true});69execa('unicorns', {shell: '/bin/sh'});70execa('unicorns', {stripFinalNewline: false});71execa('unicorns', {preferLocal: false});72execa('unicorns', {localDir: '.'});73execa('unicorns', {reject: false});74execa('unicorns', {cleanup: false});75execa('unicorns', {timeout: 1000});76execa('unicorns', {buffer: false});77execa('unicorns', {maxBuffer: 1000});78execa('unicorns', {killSignal: 'SIGTERM'});79execa('unicorns', {killSignal: 9});80execa('unicorns', {stdin: 'pipe'});81execa('unicorns', {stdin: 'ipc'});82execa('unicorns', {stdin: 'ignore'});83execa('unicorns', {stdin: 'inherit'});84execa('unicorns', {stdin: process.stdin});85execa('unicorns', {stdin: 1});86execa('unicorns', {stdin: null});87execa('unicorns', {stdin: undefined});88execa('unicorns', {stderr: 'pipe'});89execa('unicorns', {stderr: 'ipc'});90execa('unicorns', {stderr: 'ignore'});91execa('unicorns', {stderr: 'inherit'});92execa('unicorns', {stderr: process.stderr});93execa('unicorns', {stderr: 1});94execa('unicorns', {stderr: null});95execa('unicorns', {stderr: undefined});96execa('unicorns', {stdout: 'pipe'});97execa('unicorns', {stdout: 'ipc'});98execa('unicorns', {stdout: 'ignore'});99execa('unicorns', {stdout: 'inherit'});100execa('unicorns', {stdout: process.stdout});101execa('unicorns', {stdout: 1});102execa('unicorns', {stdout: null});103execa('unicorns', {stdout: undefined});104execa('unicorns', {windowsVerbatimArguments: true});105106expectType<ExecaChildProcess<string>>(execa('unicorns'));107expectType<ExecaReturnValue<string>>(await execa('unicorns'));108expectType<ExecaReturnValue<string>>(109 await execa('unicorns', {encoding: 'utf8'})110);111expectType<ExecaReturnValue<Buffer>>(await execa('unicorns', {encoding: null}));112expectType<ExecaReturnValue<string>>(113 await execa('unicorns', ['foo'], {encoding: 'utf8'})114);115expectType<ExecaReturnValue<Buffer>>(116 await execa('unicorns', ['foo'], {encoding: null})117);118119expectType<Promise<string>>(execa.stdout('unicorns'));120expectType<string>(await execa.stdout('unicorns'));121expectType<string>(await execa.stdout('unicorns', {encoding: 'utf8'}));122expectType<Buffer>(await execa.stdout('unicorns', {encoding: null}));123expectType<string>(await execa.stdout('unicorns', ['foo'], {encoding: 'utf8'}));124expectType<Buffer>(await execa.stdout('unicorns', ['foo'], {encoding: null}));125126expectType<Promise<string>>(execa.stderr('unicorns'));127expectType<string>(await execa.stderr('unicorns'));128expectType<string>(await execa.stderr('unicorns', {encoding: 'utf8'}));129expectType<Buffer>(await execa.stderr('unicorns', {encoding: null}));130expectType<string>(await execa.stderr('unicorns', ['foo'], {encoding: 'utf8'}));131expectType<Buffer>(await execa.stderr('unicorns', ['foo'], {encoding: null}));132133expectType<ExecaChildProcess<string>>(execa.shell('unicorns'));134expectType<ExecaReturnValue<string>>(await execa.shell('unicorns'));135expectType<ExecaReturnValue<string>>(136 await execa.shell('unicorns', {encoding: 'utf8'})137);138expectType<ExecaReturnValue<Buffer>>(139 await execa.shell('unicorns', {encoding: null})140);141142expectType<ExecaSyncReturnValue<string>>(execa.sync('unicorns'));143expectType<ExecaSyncReturnValue<string>>(144 execa.sync('unicorns', {encoding: 'utf8'})145);146expectType<ExecaSyncReturnValue<Buffer>>(147 execa.sync('unicorns', {encoding: null})148);149expectType<ExecaSyncReturnValue<string>>(150 execa.sync('unicorns', ['foo'], {encoding: 'utf8'})151);152expectType<ExecaSyncReturnValue<Buffer>>(153 execa.sync('unicorns', ['foo'], {encoding: null})154);155156expectType<ExecaSyncReturnValue<string>>(execa.shellSync('unicorns'));157expectType<ExecaSyncReturnValue<string>>(158 execa.shellSync('unicorns', {encoding: 'utf8'})159);160expectType<ExecaSyncReturnValue<Buffer>>(161 execa.shellSync('unicorns', {encoding: null})162);
modified:package.json+5 added-2 removed