UNPKG

23.9 kBTypeScriptView Raw
1declare module "child_process" {
2 import * as events from "events";
3 import * as net from "net";
4 import { Writable, Readable, Stream, Pipe } from "stream";
5
6 interface ChildProcess extends events.EventEmitter {
7 stdin: Writable | null;
8 stdout: Readable | null;
9 stderr: Readable | null;
10 readonly channel?: Pipe | null;
11 readonly stdio: [
12 Writable | null, // stdin
13 Readable | null, // stdout
14 Readable | null, // stderr
15 Readable | Writable | null | undefined, // extra
16 Readable | Writable | null | undefined // extra
17 ];
18 readonly killed: boolean;
19 readonly pid: number;
20 readonly connected: boolean;
21 kill(signal?: string): void;
22 send(message: any, callback?: (error: Error | null) => void): boolean;
23 send(message: any, sendHandle?: net.Socket | net.Server, callback?: (error: Error | null) => void): boolean;
24 send(message: any, sendHandle?: net.Socket | net.Server, options?: MessageOptions, callback?: (error: Error | null) => void): boolean;
25 disconnect(): void;
26 unref(): void;
27 ref(): void;
28
29 /**
30 * events.EventEmitter
31 * 1. close
32 * 2. disconnect
33 * 3. error
34 * 4. exit
35 * 5. message
36 */
37
38 addListener(event: string, listener: (...args: any[]) => void): this;
39 addListener(event: "close", listener: (code: number, signal: string) => void): this;
40 addListener(event: "disconnect", listener: () => void): this;
41 addListener(event: "error", listener: (err: Error) => void): this;
42 addListener(event: "exit", listener: (code: number | null, signal: string | null) => void): this;
43 addListener(event: "message", listener: (message: any, sendHandle: net.Socket | net.Server) => void): this;
44
45 emit(event: string | symbol, ...args: any[]): boolean;
46 emit(event: "close", code: number, signal: string): boolean;
47 emit(event: "disconnect"): boolean;
48 emit(event: "error", err: Error): boolean;
49 emit(event: "exit", code: number | null, signal: string | null): boolean;
50 emit(event: "message", message: any, sendHandle: net.Socket | net.Server): boolean;
51
52 on(event: string, listener: (...args: any[]) => void): this;
53 on(event: "close", listener: (code: number, signal: string) => void): this;
54 on(event: "disconnect", listener: () => void): this;
55 on(event: "error", listener: (err: Error) => void): this;
56 on(event: "exit", listener: (code: number | null, signal: string | null) => void): this;
57 on(event: "message", listener: (message: any, sendHandle: net.Socket | net.Server) => void): this;
58
59 once(event: string, listener: (...args: any[]) => void): this;
60 once(event: "close", listener: (code: number, signal: string) => void): this;
61 once(event: "disconnect", listener: () => void): this;
62 once(event: "error", listener: (err: Error) => void): this;
63 once(event: "exit", listener: (code: number | null, signal: string | null) => void): this;
64 once(event: "message", listener: (message: any, sendHandle: net.Socket | net.Server) => void): this;
65
66 prependListener(event: string, listener: (...args: any[]) => void): this;
67 prependListener(event: "close", listener: (code: number, signal: string) => void): this;
68 prependListener(event: "disconnect", listener: () => void): this;
69 prependListener(event: "error", listener: (err: Error) => void): this;
70 prependListener(event: "exit", listener: (code: number | null, signal: string | null) => void): this;
71 prependListener(event: "message", listener: (message: any, sendHandle: net.Socket | net.Server) => void): this;
72
73 prependOnceListener(event: string, listener: (...args: any[]) => void): this;
74 prependOnceListener(event: "close", listener: (code: number, signal: string) => void): this;
75 prependOnceListener(event: "disconnect", listener: () => void): this;
76 prependOnceListener(event: "error", listener: (err: Error) => void): this;
77 prependOnceListener(event: "exit", listener: (code: number | null, signal: string | null) => void): this;
78 prependOnceListener(event: "message", listener: (message: any, sendHandle: net.Socket | net.Server) => void): this;
79 }
80
81 // return this object when stdio option is undefined or not specified
82 interface ChildProcessWithoutNullStreams extends ChildProcess {
83 stdin: Writable;
84 stdout: Readable;
85 stderr: Readable;
86 readonly stdio: [
87 Writable, // stdin
88 Readable, // stdout
89 Readable, // stderr
90 Readable | Writable | null | undefined, // extra, no modification
91 Readable | Writable | null | undefined // extra, no modification
92 ];
93 }
94
95 // return this object when stdio option is a tuple of 3
96 interface ChildProcessByStdio<
97 I extends null | Writable,
98 O extends null | Readable,
99 E extends null | Readable,
100 > extends ChildProcess {
101 stdin: I;
102 stdout: O;
103 stderr: E;
104 readonly stdio: [
105 I,
106 O,
107 E,
108 Readable | Writable | null | undefined, // extra, no modification
109 Readable | Writable | null | undefined // extra, no modification
110 ];
111 }
112
113 interface MessageOptions {
114 keepOpen?: boolean;
115 }
116
117 type StdioOptions = "pipe" | "ignore" | "inherit" | Array<("pipe" | "ipc" | "ignore" | "inherit" | Stream | number | null | undefined)>;
118
119 interface ProcessEnvOptions {
120 uid?: number;
121 gid?: number;
122 cwd?: string;
123 env?: NodeJS.ProcessEnv;
124 }
125
126 interface CommonOptions extends ProcessEnvOptions {
127 /**
128 * @default true
129 */
130 windowsHide?: boolean;
131 /**
132 * @default 0
133 */
134 timeout?: number;
135 }
136
137 interface SpawnOptions extends CommonOptions {
138 argv0?: string;
139 stdio?: StdioOptions;
140 detached?: boolean;
141 shell?: boolean | string;
142 windowsVerbatimArguments?: boolean;
143 }
144
145 interface SpawnOptionsWithoutStdio extends SpawnOptions {
146 stdio?: 'pipe' | Array<null | undefined | 'pipe'>;
147 }
148
149 type StdioNull = 'inherit' | 'ignore' | Stream;
150 type StdioPipe = undefined | null | 'pipe';
151
152 interface SpawnOptionsWithStdioTuple<
153 Stdin extends StdioNull | StdioPipe,
154 Stdout extends StdioNull | StdioPipe,
155 Stderr extends StdioNull | StdioPipe,
156 > extends SpawnOptions {
157 stdio: [Stdin, Stdout, Stderr];
158 }
159
160 // overloads of spawn without 'args'
161 function spawn(command: string, options?: SpawnOptionsWithoutStdio): ChildProcessWithoutNullStreams;
162
163 function spawn(
164 command: string,
165 options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>,
166 ): ChildProcessByStdio<Writable, Readable, Readable>;
167 function spawn(
168 command: string,
169 options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>,
170 ): ChildProcessByStdio<Writable, Readable, null>;
171 function spawn(
172 command: string,
173 options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe>,
174 ): ChildProcessByStdio<Writable, null, Readable>;
175 function spawn(
176 command: string,
177 options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>,
178 ): ChildProcessByStdio<null, Readable, Readable>;
179 function spawn(
180 command: string,
181 options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull>,
182 ): ChildProcessByStdio<Writable, null, null>;
183 function spawn(
184 command: string,
185 options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull>,
186 ): ChildProcessByStdio<null, Readable, null>;
187 function spawn(
188 command: string,
189 options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe>,
190 ): ChildProcessByStdio<null, null, Readable>;
191 function spawn(
192 command: string,
193 options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull>,
194 ): ChildProcessByStdio<null, null, null>;
195
196 function spawn(command: string, options: SpawnOptions): ChildProcess;
197
198 // overloads of spawn with 'args'
199 function spawn(command: string, args?: ReadonlyArray<string>, options?: SpawnOptionsWithoutStdio): ChildProcessWithoutNullStreams;
200
201 function spawn(
202 command: string,
203 args: ReadonlyArray<string>,
204 options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioPipe>,
205 ): ChildProcessByStdio<Writable, Readable, Readable>;
206 function spawn(
207 command: string,
208 args: ReadonlyArray<string>,
209 options: SpawnOptionsWithStdioTuple<StdioPipe, StdioPipe, StdioNull>,
210 ): ChildProcessByStdio<Writable, Readable, null>;
211 function spawn(
212 command: string,
213 args: ReadonlyArray<string>,
214 options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioPipe>,
215 ): ChildProcessByStdio<Writable, null, Readable>;
216 function spawn(
217 command: string,
218 args: ReadonlyArray<string>,
219 options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioPipe>,
220 ): ChildProcessByStdio<null, Readable, Readable>;
221 function spawn(
222 command: string,
223 args: ReadonlyArray<string>,
224 options: SpawnOptionsWithStdioTuple<StdioPipe, StdioNull, StdioNull>,
225 ): ChildProcessByStdio<Writable, null, null>;
226 function spawn(
227 command: string,
228 args: ReadonlyArray<string>,
229 options: SpawnOptionsWithStdioTuple<StdioNull, StdioPipe, StdioNull>,
230 ): ChildProcessByStdio<null, Readable, null>;
231 function spawn(
232 command: string,
233 args: ReadonlyArray<string>,
234 options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioPipe>,
235 ): ChildProcessByStdio<null, null, Readable>;
236 function spawn(
237 command: string,
238 args: ReadonlyArray<string>,
239 options: SpawnOptionsWithStdioTuple<StdioNull, StdioNull, StdioNull>,
240 ): ChildProcessByStdio<null, null, null>;
241
242 function spawn(command: string, args: ReadonlyArray<string>, options: SpawnOptions): ChildProcess;
243
244 interface ExecOptions extends CommonOptions {
245 shell?: string;
246 maxBuffer?: number;
247 killSignal?: string;
248 }
249
250 interface ExecOptionsWithStringEncoding extends ExecOptions {
251 encoding: BufferEncoding;
252 }
253
254 interface ExecOptionsWithBufferEncoding extends ExecOptions {
255 encoding: string | null; // specify `null`.
256 }
257
258 interface ExecException extends Error {
259 cmd?: string;
260 killed?: boolean;
261 code?: number;
262 signal?: string;
263 }
264
265 // no `options` definitely means stdout/stderr are `string`.
266 function exec(command: string, callback?: (error: ExecException | null, stdout: string, stderr: string) => void): ChildProcess;
267
268 // `options` with `"buffer"` or `null` for `encoding` means stdout/stderr are definitely `Buffer`.
269 function exec(command: string, options: { encoding: "buffer" | null } & ExecOptions, callback?: (error: ExecException | null, stdout: Buffer, stderr: Buffer) => void): ChildProcess;
270
271 // `options` with well known `encoding` means stdout/stderr are definitely `string`.
272 function exec(command: string, options: { encoding: BufferEncoding } & ExecOptions, callback?: (error: ExecException | null, stdout: string, stderr: string) => void): ChildProcess;
273
274 // `options` with an `encoding` whose type is `string` means stdout/stderr could either be `Buffer` or `string`.
275 // There is no guarantee the `encoding` is unknown as `string` is a superset of `BufferEncoding`.
276 function exec(command: string, options: { encoding: string } & ExecOptions, callback?: (error: ExecException | null, stdout: string | Buffer, stderr: string | Buffer) => void): ChildProcess;
277
278 // `options` without an `encoding` means stdout/stderr are definitely `string`.
279 function exec(command: string, options: ExecOptions, callback?: (error: ExecException | null, stdout: string, stderr: string) => void): ChildProcess;
280
281 // fallback if nothing else matches. Worst case is always `string | Buffer`.
282 function exec(
283 command: string,
284 options: ({ encoding?: string | null } & ExecOptions) | undefined | null,
285 callback?: (error: ExecException | null, stdout: string | Buffer, stderr: string | Buffer) => void,
286 ): ChildProcess;
287
288 interface PromiseWithChild<T> extends Promise<T> {
289 child: ChildProcess;
290 }
291
292 // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime.
293 namespace exec {
294 function __promisify__(command: string): PromiseWithChild<{ stdout: string, stderr: string }>;
295 function __promisify__(command: string, options: { encoding: "buffer" | null } & ExecOptions): PromiseWithChild<{ stdout: Buffer, stderr: Buffer }>;
296 function __promisify__(command: string, options: { encoding: BufferEncoding } & ExecOptions): PromiseWithChild<{ stdout: string, stderr: string }>;
297 function __promisify__(command: string, options: ExecOptions): PromiseWithChild<{ stdout: string, stderr: string }>;
298 function __promisify__(command: string, options?: ({ encoding?: string | null } & ExecOptions) | null): PromiseWithChild<{ stdout: string | Buffer, stderr: string | Buffer }>;
299 }
300
301 interface ExecFileOptions extends CommonOptions {
302 maxBuffer?: number;
303 killSignal?: string;
304 windowsVerbatimArguments?: boolean;
305 shell?: boolean | string;
306 }
307 interface ExecFileOptionsWithStringEncoding extends ExecFileOptions {
308 encoding: BufferEncoding;
309 }
310 interface ExecFileOptionsWithBufferEncoding extends ExecFileOptions {
311 encoding: 'buffer' | null;
312 }
313 interface ExecFileOptionsWithOtherEncoding extends ExecFileOptions {
314 encoding: string;
315 }
316
317 function execFile(file: string): ChildProcess;
318 function execFile(file: string, options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null): ChildProcess;
319 function execFile(file: string, args?: ReadonlyArray<string> | null): ChildProcess;
320 function execFile(file: string, args: ReadonlyArray<string> | undefined | null, options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null): ChildProcess;
321
322 // no `options` definitely means stdout/stderr are `string`.
323 function execFile(file: string, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
324 function execFile(file: string, args: ReadonlyArray<string> | undefined | null, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
325
326 // `options` with `"buffer"` or `null` for `encoding` means stdout/stderr are definitely `Buffer`.
327 function execFile(file: string, options: ExecFileOptionsWithBufferEncoding, callback: (error: Error | null, stdout: Buffer, stderr: Buffer) => void): ChildProcess;
328 function execFile(
329 file: string,
330 args: ReadonlyArray<string> | undefined | null,
331 options: ExecFileOptionsWithBufferEncoding,
332 callback: (error: Error | null, stdout: Buffer, stderr: Buffer) => void,
333 ): ChildProcess;
334
335 // `options` with well known `encoding` means stdout/stderr are definitely `string`.
336 function execFile(file: string, options: ExecFileOptionsWithStringEncoding, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
337 function execFile(
338 file: string,
339 args: ReadonlyArray<string> | undefined | null,
340 options: ExecFileOptionsWithStringEncoding,
341 callback: (error: Error | null, stdout: string, stderr: string) => void,
342 ): ChildProcess;
343
344 // `options` with an `encoding` whose type is `string` means stdout/stderr could either be `Buffer` or `string`.
345 // There is no guarantee the `encoding` is unknown as `string` is a superset of `BufferEncoding`.
346 function execFile(
347 file: string,
348 options: ExecFileOptionsWithOtherEncoding,
349 callback: (error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void,
350 ): ChildProcess;
351 function execFile(
352 file: string,
353 args: ReadonlyArray<string> | undefined | null,
354 options: ExecFileOptionsWithOtherEncoding,
355 callback: (error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void,
356 ): ChildProcess;
357
358 // `options` without an `encoding` means stdout/stderr are definitely `string`.
359 function execFile(file: string, options: ExecFileOptions, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
360 function execFile(file: string, args: ReadonlyArray<string> | undefined | null, options: ExecFileOptions, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
361
362 // fallback if nothing else matches. Worst case is always `string | Buffer`.
363 function execFile(
364 file: string,
365 options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null,
366 callback: ((error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void) | undefined | null,
367 ): ChildProcess;
368 function execFile(
369 file: string,
370 args: ReadonlyArray<string> | undefined | null,
371 options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null,
372 callback: ((error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void) | undefined | null,
373 ): ChildProcess;
374
375 // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime.
376 namespace execFile {
377 function __promisify__(file: string): PromiseWithChild<{ stdout: string, stderr: string }>;
378 function __promisify__(file: string, args: string[] | undefined | null): PromiseWithChild<{ stdout: string, stderr: string }>;
379 function __promisify__(file: string, options: ExecFileOptionsWithBufferEncoding): PromiseWithChild<{ stdout: Buffer, stderr: Buffer }>;
380 function __promisify__(file: string, args: string[] | undefined | null, options: ExecFileOptionsWithBufferEncoding): PromiseWithChild<{ stdout: Buffer, stderr: Buffer }>;
381 function __promisify__(file: string, options: ExecFileOptionsWithStringEncoding): PromiseWithChild<{ stdout: string, stderr: string }>;
382 function __promisify__(file: string, args: string[] | undefined | null, options: ExecFileOptionsWithStringEncoding): PromiseWithChild<{ stdout: string, stderr: string }>;
383 function __promisify__(file: string, options: ExecFileOptionsWithOtherEncoding): PromiseWithChild<{ stdout: string | Buffer, stderr: string | Buffer }>;
384 function __promisify__(file: string, args: string[] | undefined | null, options: ExecFileOptionsWithOtherEncoding): PromiseWithChild<{ stdout: string | Buffer, stderr: string | Buffer }>;
385 function __promisify__(file: string, options: ExecFileOptions): PromiseWithChild<{ stdout: string, stderr: string }>;
386 function __promisify__(file: string, args: string[] | undefined | null, options: ExecFileOptions): PromiseWithChild<{ stdout: string, stderr: string }>;
387 function __promisify__(file: string, options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null): PromiseWithChild<{ stdout: string | Buffer, stderr: string | Buffer }>;
388 function __promisify__(
389 file: string,
390 args: string[] | undefined | null,
391 options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null,
392 ): PromiseWithChild<{ stdout: string | Buffer, stderr: string | Buffer }>;
393 }
394
395 interface ForkOptions extends ProcessEnvOptions {
396 execPath?: string;
397 execArgv?: string[];
398 silent?: boolean;
399 stdio?: StdioOptions;
400 detached?: boolean;
401 windowsVerbatimArguments?: boolean;
402 }
403 function fork(modulePath: string, args?: ReadonlyArray<string>, options?: ForkOptions): ChildProcess;
404
405 interface SpawnSyncOptions extends CommonOptions {
406 argv0?: string; // Not specified in the docs
407 input?: string | NodeJS.ArrayBufferView;
408 stdio?: StdioOptions;
409 killSignal?: string | number;
410 maxBuffer?: number;
411 encoding?: string;
412 shell?: boolean | string;
413 windowsVerbatimArguments?: boolean;
414 }
415 interface SpawnSyncOptionsWithStringEncoding extends SpawnSyncOptions {
416 encoding: BufferEncoding;
417 }
418 interface SpawnSyncOptionsWithBufferEncoding extends SpawnSyncOptions {
419 encoding: string; // specify `null`.
420 }
421 interface SpawnSyncReturns<T> {
422 pid: number;
423 output: string[];
424 stdout: T;
425 stderr: T;
426 status: number | null;
427 signal: string | null;
428 error?: Error;
429 }
430 function spawnSync(command: string): SpawnSyncReturns<Buffer>;
431 function spawnSync(command: string, options?: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns<string>;
432 function spawnSync(command: string, options?: SpawnSyncOptionsWithBufferEncoding): SpawnSyncReturns<Buffer>;
433 function spawnSync(command: string, options?: SpawnSyncOptions): SpawnSyncReturns<Buffer>;
434 function spawnSync(command: string, args?: ReadonlyArray<string>, options?: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns<string>;
435 function spawnSync(command: string, args?: ReadonlyArray<string>, options?: SpawnSyncOptionsWithBufferEncoding): SpawnSyncReturns<Buffer>;
436 function spawnSync(command: string, args?: ReadonlyArray<string>, options?: SpawnSyncOptions): SpawnSyncReturns<Buffer>;
437
438 interface ExecSyncOptions extends CommonOptions {
439 input?: string | Uint8Array;
440 stdio?: StdioOptions;
441 shell?: string;
442 killSignal?: string | number;
443 maxBuffer?: number;
444 encoding?: string;
445 }
446 interface ExecSyncOptionsWithStringEncoding extends ExecSyncOptions {
447 encoding: BufferEncoding;
448 }
449 interface ExecSyncOptionsWithBufferEncoding extends ExecSyncOptions {
450 encoding: string; // specify `null`.
451 }
452 function execSync(command: string): Buffer;
453 function execSync(command: string, options?: ExecSyncOptionsWithStringEncoding): string;
454 function execSync(command: string, options?: ExecSyncOptionsWithBufferEncoding): Buffer;
455 function execSync(command: string, options?: ExecSyncOptions): Buffer;
456
457 interface ExecFileSyncOptions extends CommonOptions {
458 input?: string | NodeJS.ArrayBufferView;
459 stdio?: StdioOptions;
460 killSignal?: string | number;
461 maxBuffer?: number;
462 encoding?: string;
463 shell?: boolean | string;
464 }
465 interface ExecFileSyncOptionsWithStringEncoding extends ExecFileSyncOptions {
466 encoding: BufferEncoding;
467 }
468 interface ExecFileSyncOptionsWithBufferEncoding extends ExecFileSyncOptions {
469 encoding: string; // specify `null`.
470 }
471 function execFileSync(command: string): Buffer;
472 function execFileSync(command: string, options?: ExecFileSyncOptionsWithStringEncoding): string;
473 function execFileSync(command: string, options?: ExecFileSyncOptionsWithBufferEncoding): Buffer;
474 function execFileSync(command: string, options?: ExecFileSyncOptions): Buffer;
475 function execFileSync(command: string, args?: ReadonlyArray<string>, options?: ExecFileSyncOptionsWithStringEncoding): string;
476 function execFileSync(command: string, args?: ReadonlyArray<string>, options?: ExecFileSyncOptionsWithBufferEncoding): Buffer;
477 function execFileSync(command: string, args?: ReadonlyArray<string>, options?: ExecFileSyncOptions): Buffer;
478}