UNPKG

4.98 kBTypeScriptView Raw
1// Modified from the node.js definitions.
2// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/node/child_process.d.ts
3
4import {
5 ChildProcess,
6 ExecException,
7 ExecOptions,
8 ExecFileOptions,
9 ExecFileOptionsWithStringEncoding,
10} from "child_process";
11export * from "child_process";
12
13export function exec(
14 command: string,
15 callback: (error: ExecException | null, stdout: string, stderr: string) => void
16): ChildProcess;
17export function exec(
18 command: string,
19 options: { encoding: "buffer" | null | undefined } & ExecOptions,
20 callback: (error: ExecException | null, stdout: Buffer, stderr: Buffer) => void
21): ChildProcess;
22export function exec(
23 command: string,
24 options: ({ encoding?: BufferEncoding } & ExecOptions) | null | undefined,
25 callback: (error: ExecException | null, stdout: string, stderr: string) => void
26): ChildProcess;
27export function exec(
28 command: string,
29 options: ({ encoding?: string | null } & ExecOptions) | null | undefined,
30 callback: (error: ExecException | null, stdout: string | Buffer, stderr: string | Buffer) => void
31): ChildProcess;
32
33export function exec(
34 command: string,
35 options: { encoding: "buffer" | null | undefined } & ExecOptions
36): Promise<[Buffer, Buffer]>;
37export function exec(
38 command: string,
39 options?: ({ encoding?: BufferEncoding } & ExecOptions) | null
40): Promise<[string, string]>;
41export function exec(
42 command: string,
43 options?: ({ encoding?: string | null } & ExecOptions) | null
44): Promise<[string | Buffer, string | Buffer]>;
45
46interface ExecFileOptionsWithBufferEncoding extends ExecFileOptions {
47 encoding: "buffer" | null | undefined;
48}
49
50interface ExecFileOptionsWithOtherEncoding extends ExecFileOptions {
51 encoding?: string | null;
52}
53
54// no `options` definitely means stdout/stderr are `string`.
55export function execFile(
56 file: string,
57 callback: (error: Error | null, stdout: string, stderr: string) => void
58): ChildProcess;
59export function execFile(
60 file: string,
61 args: ReadonlyArray<string> | null | undefined,
62 callback: (error: Error | null, stdout: string, stderr: string) => void
63): ChildProcess;
64
65// `options` with `"buffer"` or `null` for `encoding` means stdout/stderr are definitely `Buffer`.
66export function execFile(
67 file: string,
68 options: ExecFileOptionsWithBufferEncoding,
69 callback: (error: Error | null, stdout: Buffer, stderr: Buffer) => void
70): ChildProcess;
71export function execFile(
72 file: string,
73 args: ReadonlyArray<string> | null | undefined,
74 options: ExecFileOptionsWithBufferEncoding,
75 callback: (error: Error | null, stdout: Buffer, stderr: Buffer) => void
76): ChildProcess;
77
78// `options` without an or with a well known `encoding` means stdout/stderr are definitely `string`.
79export function execFile(
80 file: string,
81 // `options` can't be mixed into `args`
82 // tslint:disable-next-line: unified-signatures
83 options: ExecFileOptions | ExecFileOptionsWithStringEncoding,
84 callback: (error: Error | null, stdout: string, stderr: string) => void
85): ChildProcess;
86export function execFile(
87 file: string,
88 args: ReadonlyArray<string> | null | undefined,
89 options: ExecFileOptions | ExecFileOptionsWithStringEncoding,
90 callback: (error: Error | null, stdout: string, stderr: string) => void
91): ChildProcess;
92
93// `options` with an `encoding` whose type is `string` means stdout/stderr could either be `Buffer` or `string`.
94// There is no guarantee the `encoding` is unknown as `string` is a superset of `BufferEncoding`.
95export function execFile(
96 file: string,
97 options: ExecFileOptionsWithOtherEncoding | null | undefined,
98 callback: (error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void
99): ChildProcess;
100export function execFile(
101 file: string,
102 args: ReadonlyArray<string> | null | undefined,
103 options: ExecFileOptionsWithOtherEncoding | null | undefined,
104 callback: (error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void
105): ChildProcess;
106
107export function execFile(
108 file: string,
109 args: string[] | null | undefined,
110 options: ExecFileOptionsWithBufferEncoding
111): Promise<[Buffer, Buffer]>;
112export function execFile(file: string, options: ExecFileOptionsWithBufferEncoding): Promise<[Buffer, Buffer]>;
113export function execFile(
114 file: string,
115 args?: string[] | null,
116 options?: ExecFileOptions | ExecFileOptionsWithStringEncoding | null
117): Promise<[string, string]>;
118export function execFile(
119 file: string,
120 options?: ExecFileOptions | ExecFileOptionsWithStringEncoding | null
121): Promise<[string, string]>;
122export function execFile(
123 file: string,
124 args?: string[] | null,
125 options?: ExecFileOptionsWithOtherEncoding | null
126): Promise<[string | Buffer, string | Buffer]>;
127export function execFile(
128 file: string,
129 options?: ExecFileOptionsWithOtherEncoding | null
130): Promise<[string | Buffer, string | Buffer]>;