Skip to content

Commit 2f47c75

Browse files
committed
Merge pull request DefinitelyTyped#8584 from DefinitelyTyped/improve-node-child_process
Improve node child process
2 parents 3b5d9af + 39812a6 commit 2f47c75

File tree

2 files changed

+111
-40
lines changed

2 files changed

+111
-40
lines changed

node/node.d.ts

Lines changed: 109 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ declare var SlowBuffer: {
8080

8181

8282
// Buffer class
83+
type BufferEncoding = "ascii" | "utf8" | "utf16le" | "ucs2" | "binary" | "hex";
8384
interface Buffer extends NodeBuffer {}
8485

8586
/**
@@ -227,8 +228,8 @@ declare module NodeJS {
227228
removeListener(event: string, listener: Function): this;
228229
removeAllListeners(event?: string): this;
229230
}
230-
231-
export interface MemoryUsage {
231+
232+
export interface MemoryUsage {
232233
rss: number;
233234
heapTotal: number;
234235
heapUsed: number;
@@ -945,99 +946,169 @@ declare module "child_process" {
945946
stdin: stream.Writable;
946947
stdout: stream.Readable;
947948
stderr: stream.Readable;
948-
stdio: (stream.Readable|stream.Writable)[];
949+
stdio: [stream.Writable, stream.Readable, stream.Readable];
949950
pid: number;
950951
kill(signal?: string): void;
951952
send(message: any, sendHandle?: any): void;
952953
disconnect(): void;
953954
unref(): void;
954955
}
955956

956-
export function spawn(command: string, args?: string[], options?: {
957+
export interface SpawnOptions {
957958
cwd?: string;
958-
stdio?: any;
959-
custom?: any;
960959
env?: any;
960+
stdio?: any;
961961
detached?: boolean;
962-
}): ChildProcess;
963-
export function exec(command: string, options: {
962+
uid?: number;
963+
gid?: number;
964+
shell?: boolean | string;
965+
}
966+
export function spawn(command: string, args?: string[], options?: SpawnOptions): ChildProcess;
967+
968+
export interface ExecOptions {
964969
cwd?: string;
965-
stdio?: any;
966-
customFds?: any;
967970
env?: any;
968-
encoding?: string;
971+
shell?: string;
969972
timeout?: number;
970973
maxBuffer?: number;
971974
killSignal?: string;
972-
}, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
973-
export function exec(command: string, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
974-
export function execFile(file: string,
975-
callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
976-
export function execFile(file: string, args?: string[],
977-
callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
978-
export function execFile(file: string, args?: string[], options?: {
975+
uid?: number;
976+
gid?: number;
977+
}
978+
export interface ExecOptionsWithStringEncoding extends ExecOptions {
979+
encoding: BufferEncoding;
980+
}
981+
export interface ExecOptionsWithBufferEncoding extends ExecOptions {
982+
encoding: string; // specify `null`.
983+
}
984+
export function exec(command: string, callback?: (error: Error, stdout: string, stderr: string) =>void ): ChildProcess;
985+
export function exec(command: string, options: ExecOptionsWithStringEncoding, callback?: (error: Error, stdout: string, stderr: string) =>void ): ChildProcess;
986+
// usage. child_process.exec("tsc", {encoding: null as string}, (err, stdout, stderr) => {});
987+
export function exec(command: string, options: ExecOptionsWithBufferEncoding, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
988+
export function exec(command: string, options: ExecOptions, callback?: (error: Error, stdout: string, stderr: string) =>void ): ChildProcess;
989+
990+
export interface ExecFileOptions {
979991
cwd?: string;
980-
stdio?: any;
981-
customFds?: any;
982992
env?: any;
983-
encoding?: string;
984993
timeout?: number;
985994
maxBuffer?: number;
986995
killSignal?: string;
987-
}, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
988-
export function fork(modulePath: string, args?: string[], options?: {
996+
uid?: number;
997+
gid?: number;
998+
}
999+
export interface ExecFileOptionsWithStringEncoding extends ExecFileOptions {
1000+
encoding: BufferEncoding;
1001+
}
1002+
export interface ExecFileOptionsWithBufferEncoding extends ExecFileOptions {
1003+
encoding: string; // specify `null`.
1004+
}
1005+
export function execFile(file: string, callback?: (error: Error, stdout: string, stderr: string) =>void ): ChildProcess;
1006+
export function execFile(file: string, options?: ExecFileOptionsWithStringEncoding, callback?: (error: Error, stdout: string, stderr: string) =>void ): ChildProcess;
1007+
// usage. child_process.execFile("file.sh", {encoding: null as string}, (err, stdout, stderr) => {});
1008+
export function execFile(file: string, options?: ExecFileOptionsWithBufferEncoding, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
1009+
export function execFile(file: string, options?: ExecFileOptions, callback?: (error: Error, stdout: string, stderr: string) =>void ): ChildProcess;
1010+
export function execFile(file: string, args?: string[], callback?: (error: Error, stdout: string, stderr: string) =>void ): ChildProcess;
1011+
export function execFile(file: string, args?: string[], options?: ExecFileOptionsWithStringEncoding, callback?: (error: Error, stdout: string, stderr: string) =>void ): ChildProcess;
1012+
// usage. child_process.execFile("file.sh", ["foo"], {encoding: null as string}, (err, stdout, stderr) => {});
1013+
export function execFile(file: string, args?: string[], options?: ExecFileOptionsWithBufferEncoding, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
1014+
export function execFile(file: string, args?: string[], options?: ExecFileOptions, callback?: (error: Error, stdout: string, stderr: string) =>void ): ChildProcess;
1015+
1016+
export interface ForkOptions {
9891017
cwd?: string;
9901018
env?: any;
9911019
execPath?: string;
9921020
execArgv?: string[];
9931021
silent?: boolean;
9941022
uid?: number;
9951023
gid?: number;
996-
}): ChildProcess;
997-
export function spawnSync(command: string, args?: string[], options?: {
1024+
}
1025+
export function fork(modulePath: string, args?: string[], options?: ForkOptions): ChildProcess;
1026+
1027+
export interface SpawnSyncOptions {
9981028
cwd?: string;
9991029
input?: string | Buffer;
10001030
stdio?: any;
10011031
env?: any;
10021032
uid?: number;
10031033
gid?: number;
10041034
timeout?: number;
1005-
maxBuffer?: number;
10061035
killSignal?: string;
1036+
maxBuffer?: number;
10071037
encoding?: string;
1008-
}): {
1038+
shell?: boolean | string;
1039+
}
1040+
export interface SpawnSyncOptionsWithStringEncoding extends SpawnSyncOptions {
1041+
encoding: BufferEncoding;
1042+
}
1043+
export interface SpawnSyncOptionsWithBufferEncoding extends SpawnSyncOptions {
1044+
encoding: string; // specify `null`.
1045+
}
1046+
export interface SpawnSyncReturns<T> {
10091047
pid: number;
10101048
output: string[];
1011-
stdout: string | Buffer;
1012-
stderr: string | Buffer;
1049+
stdout: T;
1050+
stderr: T;
10131051
status: number;
10141052
signal: string;
10151053
error: Error;
1016-
};
1017-
export function execSync(command: string, options?: {
1054+
}
1055+
export function spawnSync(command: string): SpawnSyncReturns<Buffer>;
1056+
export function spawnSync(command: string, options?: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns<string>;
1057+
export function spawnSync(command: string, options?: SpawnSyncOptionsWithBufferEncoding): SpawnSyncReturns<Buffer>;
1058+
export function spawnSync(command: string, options?: SpawnSyncOptions): SpawnSyncReturns<Buffer>;
1059+
export function spawnSync(command: string, args?: string[], options?: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns<string>;
1060+
export function spawnSync(command: string, args?: string[], options?: SpawnSyncOptionsWithBufferEncoding): SpawnSyncReturns<Buffer>;
1061+
export function spawnSync(command: string, args?: string[], options?: SpawnSyncOptions): SpawnSyncReturns<Buffer>;
1062+
1063+
export interface ExecSyncOptions {
10181064
cwd?: string;
1019-
input?: string|Buffer;
1065+
input?: string | Buffer;
10201066
stdio?: any;
10211067
env?: any;
1068+
shell?: string;
10221069
uid?: number;
10231070
gid?: number;
10241071
timeout?: number;
1025-
maxBuffer?: number;
10261072
killSignal?: string;
1073+
maxBuffer?: number;
10271074
encoding?: string;
1028-
}): string | Buffer;
1029-
export function execFileSync(command: string, args?: string[], options?: {
1075+
}
1076+
export interface ExecSyncOptionsWithStringEncoding extends ExecSyncOptions {
1077+
encoding: BufferEncoding;
1078+
}
1079+
export interface ExecSyncOptionsWithBufferEncoding extends ExecSyncOptions {
1080+
encoding: string; // specify `null`.
1081+
}
1082+
export function execSync(command: string): Buffer;
1083+
export function execSync(command: string, options?: ExecSyncOptionsWithStringEncoding): string;
1084+
export function execSync(command: string, options?: ExecSyncOptionsWithBufferEncoding): Buffer;
1085+
export function execSync(command: string, options?: ExecSyncOptions): Buffer;
1086+
1087+
export interface ExecFileSyncOptions {
10301088
cwd?: string;
1031-
input?: string|Buffer;
1089+
input?: string | Buffer;
10321090
stdio?: any;
10331091
env?: any;
10341092
uid?: number;
10351093
gid?: number;
10361094
timeout?: number;
1037-
maxBuffer?: number;
10381095
killSignal?: string;
1096+
maxBuffer?: number;
10391097
encoding?: string;
1040-
}): string | Buffer;
1098+
}
1099+
export interface ExecFileSyncOptionsWithStringEncoding extends ExecFileSyncOptions {
1100+
encoding: BufferEncoding;
1101+
}
1102+
export interface ExecFileSyncOptionsWithBufferEncoding extends ExecFileSyncOptions {
1103+
encoding: string; // specify `null`.
1104+
}
1105+
export function execFileSync(command: string): Buffer;
1106+
export function execFileSync(command: string, options?: ExecFileSyncOptionsWithStringEncoding): string;
1107+
export function execFileSync(command: string, options?: ExecFileSyncOptionsWithBufferEncoding): Buffer;
1108+
export function execFileSync(command: string, options?: ExecFileSyncOptions): Buffer;
1109+
export function execFileSync(command: string, args?: string[], options?: ExecFileSyncOptionsWithStringEncoding): string;
1110+
export function execFileSync(command: string, args?: string[], options?: ExecFileSyncOptionsWithBufferEncoding): Buffer;
1111+
export function execFileSync(command: string, args?: string[], options?: ExecFileSyncOptions): Buffer;
10411112
}
10421113

10431114
declare module "url" {

tabtab/tabtab-tests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ if (process.argv.slice(2)[0] === 'completion') {
1313
if (/^-\w?/.test(data.last)) return tabtab.log(['n', 'o', 'd', 'e'], data, '-');
1414
tabtab.log(['list', 'of', 'commands'], data);
1515

16-
child_process.exec('rake -H', function(err, stdout, stderr) {
16+
child_process.exec('rake -H', {encoding: null as string}, function(err, stdout, stderr) {
1717
if (err) return;
1818
var decoder = new string_decoder.StringDecoder('utf8');
1919
var parsed = tabtab.parseOut(decoder.write(stdout));
2020
if (/^--\w?/.test(data.last)) return tabtab.log(parsed.longs, data, '--');
2121
if (/^-\w?/.test(data.last)) return tabtab.log(parsed.shorts, data, '-');
2222
});
2323

24-
child_process.exec('cake', function(err, stdout, stderr) {
24+
child_process.exec('cake', {encoding: null as string}, function(err, stdout, stderr) {
2525
if (err) return;
2626
var decoder = new string_decoder.StringDecoder('utf8');
2727
var tasks = tabtab.parseTasks(decoder.write(stdout), 'cake');

0 commit comments

Comments
 (0)