UNPKG

19.6 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 interface MessageOptions {
96 keepOpen?: boolean;
97 }
98
99 type StdioOptions = "pipe" | "ignore" | "inherit" | Array<("pipe" | "ipc" | "ignore" | "inherit" | Stream | number | null | undefined)>;
100
101 interface ProcessEnvOptions {
102 uid?: number;
103 gid?: number;
104 cwd?: string;
105 env?: NodeJS.ProcessEnv;
106 }
107
108 interface CommonOptions extends ProcessEnvOptions {
109 /**
110 * @default true
111 */
112 windowsHide?: boolean;
113 /**
114 * @default 0
115 */
116 timeout?: number;
117 }
118
119 interface SpawnOptions extends CommonOptions {
120 argv0?: string;
121 stdio?: StdioOptions;
122 detached?: boolean;
123 shell?: boolean | string;
124 windowsVerbatimArguments?: boolean;
125 }
126
127 interface SpawnOptionsWithoutStdio extends SpawnOptions {
128 stdio?: 'pipe' | Array<null | undefined | 'pipe'>;
129 }
130
131 function spawn(command: string, options?: SpawnOptionsWithoutStdio): ChildProcessWithoutNullStreams;
132 function spawn(command: string, options: SpawnOptions): ChildProcess;
133 function spawn(command: string, args?: ReadonlyArray<string>, options?: SpawnOptionsWithoutStdio): ChildProcessWithoutNullStreams;
134 function spawn(command: string, args: ReadonlyArray<string>, options: SpawnOptions): ChildProcess;
135
136 interface ExecOptions extends CommonOptions {
137 shell?: string;
138 maxBuffer?: number;
139 killSignal?: string;
140 }
141
142 interface ExecOptionsWithStringEncoding extends ExecOptions {
143 encoding: BufferEncoding;
144 }
145
146 interface ExecOptionsWithBufferEncoding extends ExecOptions {
147 encoding: string | null; // specify `null`.
148 }
149
150 interface ExecException extends Error {
151 cmd?: string;
152 killed?: boolean;
153 code?: number;
154 signal?: string;
155 }
156
157 // no `options` definitely means stdout/stderr are `string`.
158 function exec(command: string, callback?: (error: ExecException | null, stdout: string, stderr: string) => void): ChildProcess;
159
160 // `options` with `"buffer"` or `null` for `encoding` means stdout/stderr are definitely `Buffer`.
161 function exec(command: string, options: { encoding: "buffer" | null } & ExecOptions, callback?: (error: ExecException | null, stdout: Buffer, stderr: Buffer) => void): ChildProcess;
162
163 // `options` with well known `encoding` means stdout/stderr are definitely `string`.
164 function exec(command: string, options: { encoding: BufferEncoding } & ExecOptions, callback?: (error: ExecException | null, stdout: string, stderr: string) => void): ChildProcess;
165
166 // `options` with an `encoding` whose type is `string` means stdout/stderr could either be `Buffer` or `string`.
167 // There is no guarantee the `encoding` is unknown as `string` is a superset of `BufferEncoding`.
168 function exec(command: string, options: { encoding: string } & ExecOptions, callback?: (error: ExecException | null, stdout: string | Buffer, stderr: string | Buffer) => void): ChildProcess;
169
170 // `options` without an `encoding` means stdout/stderr are definitely `string`.
171 function exec(command: string, options: ExecOptions, callback?: (error: ExecException | null, stdout: string, stderr: string) => void): ChildProcess;
172
173 // fallback if nothing else matches. Worst case is always `string | Buffer`.
174 function exec(
175 command: string,
176 options: ({ encoding?: string | null } & ExecOptions) | undefined | null,
177 callback?: (error: ExecException | null, stdout: string | Buffer, stderr: string | Buffer) => void,
178 ): ChildProcess;
179
180 // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime.
181 namespace exec {
182 function __promisify__(command: string): Promise<{ stdout: string, stderr: string }>;
183 function __promisify__(command: string, options: { encoding: "buffer" | null } & ExecOptions): Promise<{ stdout: Buffer, stderr: Buffer }>;
184 function __promisify__(command: string, options: { encoding: BufferEncoding } & ExecOptions): Promise<{ stdout: string, stderr: string }>;
185 function __promisify__(command: string, options: ExecOptions): Promise<{ stdout: string, stderr: string }>;
186 function __promisify__(command: string, options?: ({ encoding?: string | null } & ExecOptions) | null): Promise<{ stdout: string | Buffer, stderr: string | Buffer }>;
187 }
188
189 interface ExecFileOptions extends CommonOptions {
190 maxBuffer?: number;
191 killSignal?: string;
192 windowsVerbatimArguments?: boolean;
193 }
194 interface ExecFileOptionsWithStringEncoding extends ExecFileOptions {
195 encoding: BufferEncoding;
196 }
197 interface ExecFileOptionsWithBufferEncoding extends ExecFileOptions {
198 encoding: 'buffer' | null;
199 }
200 interface ExecFileOptionsWithOtherEncoding extends ExecFileOptions {
201 encoding: string;
202 }
203
204 function execFile(file: string): ChildProcess;
205 function execFile(file: string, options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null): ChildProcess;
206 function execFile(file: string, args?: ReadonlyArray<string> | null): ChildProcess;
207 function execFile(file: string, args: ReadonlyArray<string> | undefined | null, options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null): ChildProcess;
208
209 // no `options` definitely means stdout/stderr are `string`.
210 function execFile(file: string, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
211 function execFile(file: string, args: ReadonlyArray<string> | undefined | null, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
212
213 // `options` with `"buffer"` or `null` for `encoding` means stdout/stderr are definitely `Buffer`.
214 function execFile(file: string, options: ExecFileOptionsWithBufferEncoding, callback: (error: Error | null, stdout: Buffer, stderr: Buffer) => void): ChildProcess;
215 function execFile(
216 file: string,
217 args: ReadonlyArray<string> | undefined | null,
218 options: ExecFileOptionsWithBufferEncoding,
219 callback: (error: Error | null, stdout: Buffer, stderr: Buffer) => void,
220 ): ChildProcess;
221
222 // `options` with well known `encoding` means stdout/stderr are definitely `string`.
223 function execFile(file: string, options: ExecFileOptionsWithStringEncoding, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
224 function execFile(
225 file: string,
226 args: ReadonlyArray<string> | undefined | null,
227 options: ExecFileOptionsWithStringEncoding,
228 callback: (error: Error | null, stdout: string, stderr: string) => void,
229 ): ChildProcess;
230
231 // `options` with an `encoding` whose type is `string` means stdout/stderr could either be `Buffer` or `string`.
232 // There is no guarantee the `encoding` is unknown as `string` is a superset of `BufferEncoding`.
233 function execFile(
234 file: string,
235 options: ExecFileOptionsWithOtherEncoding,
236 callback: (error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void,
237 ): ChildProcess;
238 function execFile(
239 file: string,
240 args: ReadonlyArray<string> | undefined | null,
241 options: ExecFileOptionsWithOtherEncoding,
242 callback: (error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void,
243 ): ChildProcess;
244
245 // `options` without an `encoding` means stdout/stderr are definitely `string`.
246 function execFile(file: string, options: ExecFileOptions, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
247 function execFile(file: string, args: ReadonlyArray<string> | undefined | null, options: ExecFileOptions, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
248
249 // fallback if nothing else matches. Worst case is always `string | Buffer`.
250 function execFile(
251 file: string,
252 options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null,
253 callback: ((error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void) | undefined | null,
254 ): ChildProcess;
255 function execFile(
256 file: string,
257 args: ReadonlyArray<string> | undefined | null,
258 options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null,
259 callback: ((error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void) | undefined | null,
260 ): ChildProcess;
261
262 // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime.
263 namespace execFile {
264 function __promisify__(file: string): Promise<{ stdout: string, stderr: string }>;
265 function __promisify__(file: string, args: string[] | undefined | null): Promise<{ stdout: string, stderr: string }>;
266 function __promisify__(file: string, options: ExecFileOptionsWithBufferEncoding): Promise<{ stdout: Buffer, stderr: Buffer }>;
267 function __promisify__(file: string, args: string[] | undefined | null, options: ExecFileOptionsWithBufferEncoding): Promise<{ stdout: Buffer, stderr: Buffer }>;
268 function __promisify__(file: string, options: ExecFileOptionsWithStringEncoding): Promise<{ stdout: string, stderr: string }>;
269 function __promisify__(file: string, args: string[] | undefined | null, options: ExecFileOptionsWithStringEncoding): Promise<{ stdout: string, stderr: string }>;
270 function __promisify__(file: string, options: ExecFileOptionsWithOtherEncoding): Promise<{ stdout: string | Buffer, stderr: string | Buffer }>;
271 function __promisify__(file: string, args: string[] | undefined | null, options: ExecFileOptionsWithOtherEncoding): Promise<{ stdout: string | Buffer, stderr: string | Buffer }>;
272 function __promisify__(file: string, options: ExecFileOptions): Promise<{ stdout: string, stderr: string }>;
273 function __promisify__(file: string, args: string[] | undefined | null, options: ExecFileOptions): Promise<{ stdout: string, stderr: string }>;
274 function __promisify__(file: string, options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null): Promise<{ stdout: string | Buffer, stderr: string | Buffer }>;
275 function __promisify__(
276 file: string,
277 args: string[] | undefined | null,
278 options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null,
279 ): Promise<{ stdout: string | Buffer, stderr: string | Buffer }>;
280 }
281
282 interface ForkOptions extends ProcessEnvOptions {
283 execPath?: string;
284 execArgv?: string[];
285 silent?: boolean;
286 stdio?: StdioOptions;
287 detached?: boolean;
288 windowsVerbatimArguments?: boolean;
289 }
290 function fork(modulePath: string, args?: ReadonlyArray<string>, options?: ForkOptions): ChildProcess;
291
292 interface SpawnSyncOptions extends CommonOptions {
293 argv0?: string; // Not specified in the docs
294 input?: string | Buffer | NodeJS.TypedArray | DataView;
295 stdio?: StdioOptions;
296 killSignal?: string | number;
297 maxBuffer?: number;
298 encoding?: string;
299 shell?: boolean | string;
300 windowsVerbatimArguments?: boolean;
301 }
302 interface SpawnSyncOptionsWithStringEncoding extends SpawnSyncOptions {
303 encoding: BufferEncoding;
304 }
305 interface SpawnSyncOptionsWithBufferEncoding extends SpawnSyncOptions {
306 encoding: string; // specify `null`.
307 }
308 interface SpawnSyncReturns<T> {
309 pid: number;
310 output: string[];
311 stdout: T;
312 stderr: T;
313 status: number;
314 signal: string;
315 error?: Error;
316 }
317 function spawnSync(command: string): SpawnSyncReturns<Buffer>;
318 function spawnSync(command: string, options?: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns<string>;
319 function spawnSync(command: string, options?: SpawnSyncOptionsWithBufferEncoding): SpawnSyncReturns<Buffer>;
320 function spawnSync(command: string, options?: SpawnSyncOptions): SpawnSyncReturns<Buffer>;
321 function spawnSync(command: string, args?: ReadonlyArray<string>, options?: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns<string>;
322 function spawnSync(command: string, args?: ReadonlyArray<string>, options?: SpawnSyncOptionsWithBufferEncoding): SpawnSyncReturns<Buffer>;
323 function spawnSync(command: string, args?: ReadonlyArray<string>, options?: SpawnSyncOptions): SpawnSyncReturns<Buffer>;
324
325 interface ExecSyncOptions extends CommonOptions {
326 input?: string | Buffer | Uint8Array;
327 stdio?: StdioOptions;
328 shell?: string;
329 killSignal?: string | number;
330 maxBuffer?: number;
331 encoding?: string;
332 }
333 interface ExecSyncOptionsWithStringEncoding extends ExecSyncOptions {
334 encoding: BufferEncoding;
335 }
336 interface ExecSyncOptionsWithBufferEncoding extends ExecSyncOptions {
337 encoding: string; // specify `null`.
338 }
339 function execSync(command: string): Buffer;
340 function execSync(command: string, options?: ExecSyncOptionsWithStringEncoding): string;
341 function execSync(command: string, options?: ExecSyncOptionsWithBufferEncoding): Buffer;
342 function execSync(command: string, options?: ExecSyncOptions): Buffer;
343
344 interface ExecFileSyncOptions extends CommonOptions {
345 input?: string | Buffer | NodeJS.TypedArray | DataView;
346 stdio?: StdioOptions;
347 killSignal?: string | number;
348 maxBuffer?: number;
349 encoding?: string;
350 shell?: boolean | string;
351 }
352 interface ExecFileSyncOptionsWithStringEncoding extends ExecFileSyncOptions {
353 encoding: BufferEncoding;
354 }
355 interface ExecFileSyncOptionsWithBufferEncoding extends ExecFileSyncOptions {
356 encoding: string; // specify `null`.
357 }
358 function execFileSync(command: string): Buffer;
359 function execFileSync(command: string, options?: ExecFileSyncOptionsWithStringEncoding): string;
360 function execFileSync(command: string, options?: ExecFileSyncOptionsWithBufferEncoding): Buffer;
361 function execFileSync(command: string, options?: ExecFileSyncOptions): Buffer;
362 function execFileSync(command: string, args?: ReadonlyArray<string>, options?: ExecFileSyncOptionsWithStringEncoding): string;
363 function execFileSync(command: string, args?: ReadonlyArray<string>, options?: ExecFileSyncOptionsWithBufferEncoding): Buffer;
364 function execFileSync(command: string, args?: ReadonlyArray<string>, options?: ExecFileSyncOptions): Buffer;
365}