UNPKG

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