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 shell?: boolean | string;
194 }
195 interface ExecFileOptionsWithStringEncoding extends ExecFileOptions {
196 encoding: BufferEncoding;
197 }
198 interface ExecFileOptionsWithBufferEncoding extends ExecFileOptions {
199 encoding: 'buffer' | null;
200 }
201 interface ExecFileOptionsWithOtherEncoding extends ExecFileOptions {
202 encoding: string;
203 }
204
205 function execFile(file: string): ChildProcess;
206 function execFile(file: string, options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null): ChildProcess;
207 function execFile(file: string, args?: ReadonlyArray<string> | null): ChildProcess;
208 function execFile(file: string, args: ReadonlyArray<string> | undefined | null, options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null): ChildProcess;
209
210 // no `options` definitely means stdout/stderr are `string`.
211 function execFile(file: string, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
212 function execFile(file: string, args: ReadonlyArray<string> | undefined | null, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
213
214 // `options` with `"buffer"` or `null` for `encoding` means stdout/stderr are definitely `Buffer`.
215 function execFile(file: string, options: ExecFileOptionsWithBufferEncoding, callback: (error: Error | null, stdout: Buffer, stderr: Buffer) => void): ChildProcess;
216 function execFile(
217 file: string,
218 args: ReadonlyArray<string> | undefined | null,
219 options: ExecFileOptionsWithBufferEncoding,
220 callback: (error: Error | null, stdout: Buffer, stderr: Buffer) => void,
221 ): ChildProcess;
222
223 // `options` with well known `encoding` means stdout/stderr are definitely `string`.
224 function execFile(file: string, options: ExecFileOptionsWithStringEncoding, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
225 function execFile(
226 file: string,
227 args: ReadonlyArray<string> | undefined | null,
228 options: ExecFileOptionsWithStringEncoding,
229 callback: (error: Error | null, stdout: string, stderr: string) => void,
230 ): ChildProcess;
231
232 // `options` with an `encoding` whose type is `string` means stdout/stderr could either be `Buffer` or `string`.
233 // There is no guarantee the `encoding` is unknown as `string` is a superset of `BufferEncoding`.
234 function execFile(
235 file: string,
236 options: ExecFileOptionsWithOtherEncoding,
237 callback: (error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void,
238 ): ChildProcess;
239 function execFile(
240 file: string,
241 args: ReadonlyArray<string> | undefined | null,
242 options: ExecFileOptionsWithOtherEncoding,
243 callback: (error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void,
244 ): ChildProcess;
245
246 // `options` without an `encoding` means stdout/stderr are definitely `string`.
247 function execFile(file: string, options: ExecFileOptions, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
248 function execFile(file: string, args: ReadonlyArray<string> | undefined | null, options: ExecFileOptions, callback: (error: Error | null, stdout: string, stderr: string) => void): ChildProcess;
249
250 // fallback if nothing else matches. Worst case is always `string | Buffer`.
251 function execFile(
252 file: string,
253 options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null,
254 callback: ((error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void) | undefined | null,
255 ): ChildProcess;
256 function execFile(
257 file: string,
258 args: ReadonlyArray<string> | undefined | null,
259 options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null,
260 callback: ((error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void) | undefined | null,
261 ): ChildProcess;
262
263 // NOTE: This namespace provides design-time support for util.promisify. Exported members do not exist at runtime.
264 namespace execFile {
265 function __promisify__(file: string): Promise<{ stdout: string, stderr: string }>;
266 function __promisify__(file: string, args: string[] | undefined | null): Promise<{ stdout: string, stderr: string }>;
267 function __promisify__(file: string, options: ExecFileOptionsWithBufferEncoding): Promise<{ stdout: Buffer, stderr: Buffer }>;
268 function __promisify__(file: string, args: string[] | undefined | null, options: ExecFileOptionsWithBufferEncoding): Promise<{ stdout: Buffer, stderr: Buffer }>;
269 function __promisify__(file: string, options: ExecFileOptionsWithStringEncoding): Promise<{ stdout: string, stderr: string }>;
270 function __promisify__(file: string, args: string[] | undefined | null, options: ExecFileOptionsWithStringEncoding): Promise<{ stdout: string, stderr: string }>;
271 function __promisify__(file: string, options: ExecFileOptionsWithOtherEncoding): Promise<{ stdout: string | Buffer, stderr: string | Buffer }>;
272 function __promisify__(file: string, args: string[] | undefined | null, options: ExecFileOptionsWithOtherEncoding): Promise<{ stdout: string | Buffer, stderr: string | Buffer }>;
273 function __promisify__(file: string, options: ExecFileOptions): Promise<{ stdout: string, stderr: string }>;
274 function __promisify__(file: string, args: string[] | undefined | null, options: ExecFileOptions): Promise<{ stdout: string, stderr: string }>;
275 function __promisify__(file: string, options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null): Promise<{ stdout: string | Buffer, stderr: string | Buffer }>;
276 function __promisify__(
277 file: string,
278 args: string[] | undefined | null,
279 options: ({ encoding?: string | null } & ExecFileOptions) | undefined | null,
280 ): Promise<{ stdout: string | Buffer, stderr: string | Buffer }>;
281 }
282
283 interface ForkOptions extends ProcessEnvOptions {
284 execPath?: string;
285 execArgv?: string[];
286 silent?: boolean;
287 stdio?: StdioOptions;
288 detached?: boolean;
289 windowsVerbatimArguments?: boolean;
290 }
291 function fork(modulePath: string, args?: ReadonlyArray<string>, options?: ForkOptions): ChildProcess;
292
293 interface SpawnSyncOptions extends CommonOptions {
294 argv0?: string; // Not specified in the docs
295 input?: string | Buffer | NodeJS.TypedArray | DataView;
296 stdio?: StdioOptions;
297 killSignal?: string | number;
298 maxBuffer?: number;
299 encoding?: string;
300 shell?: boolean | string;
301 windowsVerbatimArguments?: boolean;
302 }
303 interface SpawnSyncOptionsWithStringEncoding extends SpawnSyncOptions {
304 encoding: BufferEncoding;
305 }
306 interface SpawnSyncOptionsWithBufferEncoding extends SpawnSyncOptions {
307 encoding: string; // specify `null`.
308 }
309 interface SpawnSyncReturns<T> {
310 pid: number;
311 output: string[];
312 stdout: T;
313 stderr: T;
314 status: number | null;
315 signal: string | null;
316 error?: Error;
317 }
318 function spawnSync(command: string): SpawnSyncReturns<Buffer>;
319 function spawnSync(command: string, options?: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns<string>;
320 function spawnSync(command: string, options?: SpawnSyncOptionsWithBufferEncoding): SpawnSyncReturns<Buffer>;
321 function spawnSync(command: string, options?: SpawnSyncOptions): SpawnSyncReturns<Buffer>;
322 function spawnSync(command: string, args?: ReadonlyArray<string>, options?: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns<string>;
323 function spawnSync(command: string, args?: ReadonlyArray<string>, options?: SpawnSyncOptionsWithBufferEncoding): SpawnSyncReturns<Buffer>;
324 function spawnSync(command: string, args?: ReadonlyArray<string>, options?: SpawnSyncOptions): SpawnSyncReturns<Buffer>;
325
326 interface ExecSyncOptions extends CommonOptions {
327 input?: string | Buffer | Uint8Array;
328 stdio?: StdioOptions;
329 shell?: string;
330 killSignal?: string | number;
331 maxBuffer?: number;
332 encoding?: string;
333 }
334 interface ExecSyncOptionsWithStringEncoding extends ExecSyncOptions {
335 encoding: BufferEncoding;
336 }
337 interface ExecSyncOptionsWithBufferEncoding extends ExecSyncOptions {
338 encoding: string; // specify `null`.
339 }
340 function execSync(command: string): Buffer;
341 function execSync(command: string, options?: ExecSyncOptionsWithStringEncoding): string;
342 function execSync(command: string, options?: ExecSyncOptionsWithBufferEncoding): Buffer;
343 function execSync(command: string, options?: ExecSyncOptions): Buffer;
344
345 interface ExecFileSyncOptions extends CommonOptions {
346 input?: string | Buffer | NodeJS.TypedArray | DataView;
347 stdio?: StdioOptions;
348 killSignal?: string | number;
349 maxBuffer?: number;
350 encoding?: string;
351 shell?: boolean | string;
352 }
353 interface ExecFileSyncOptionsWithStringEncoding extends ExecFileSyncOptions {
354 encoding: BufferEncoding;
355 }
356 interface ExecFileSyncOptionsWithBufferEncoding extends ExecFileSyncOptions {
357 encoding: string; // specify `null`.
358 }
359 function execFileSync(command: string): Buffer;
360 function execFileSync(command: string, options?: ExecFileSyncOptionsWithStringEncoding): string;
361 function execFileSync(command: string, options?: ExecFileSyncOptionsWithBufferEncoding): Buffer;
362 function execFileSync(command: string, options?: ExecFileSyncOptions): Buffer;
363 function execFileSync(command: string, args?: ReadonlyArray<string>, options?: ExecFileSyncOptionsWithStringEncoding): string;
364 function execFileSync(command: string, args?: ReadonlyArray<string>, options?: ExecFileSyncOptionsWithBufferEncoding): Buffer;
365 function execFileSync(command: string, args?: ReadonlyArray<string>, options?: ExecFileSyncOptions): Buffer;
366}