Skip to content

Commit 39812a6

Browse files
committed
improve node.d.ts child_process definitions
1 parent 45ac539 commit 39812a6

File tree

2 files changed

+70
-15
lines changed

2 files changed

+70
-15
lines changed

node/node.d.ts

Lines changed: 68 additions & 13 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
/**
@@ -967,30 +968,50 @@ declare module "child_process" {
967968
export interface ExecOptions {
968969
cwd?: string;
969970
env?: any;
970-
encoding?: string;
971971
shell?: string;
972972
timeout?: number;
973973
maxBuffer?: number;
974974
killSignal?: string;
975975
uid?: number;
976976
gid?: number;
977977
}
978-
export function exec(command: string, options: ExecOptions, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
979-
export function exec(command: string, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
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;
980989

981990
export interface ExecFileOptions {
982991
cwd?: string;
983992
env?: any;
984-
encoding?: string;
985993
timeout?: number;
986994
maxBuffer?: number;
987995
killSignal?: string;
988996
uid?: number;
989997
gid?: number;
990998
}
991-
export function execFile(file: string, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
992-
export function execFile(file: string, args?: string[], callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
993-
export function execFile(file: string, args?: string[], options?: ExecFileOptions, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess;
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;
9941015

9951016
export interface ForkOptions {
9961017
cwd?: string;
@@ -1016,15 +1037,28 @@ declare module "child_process" {
10161037
encoding?: string;
10171038
shell?: boolean | string;
10181039
}
1019-
export function spawnSync(command: string, args?: string[], options?: SpawnSyncOptions): {
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> {
10201047
pid: number;
10211048
output: string[];
1022-
stdout: string | Buffer;
1023-
stderr: string | Buffer;
1049+
stdout: T;
1050+
stderr: T;
10241051
status: number;
10251052
signal: string;
10261053
error: Error;
1027-
};
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>;
10281062

10291063
export interface ExecSyncOptions {
10301064
cwd?: string;
@@ -1039,7 +1073,16 @@ declare module "child_process" {
10391073
maxBuffer?: number;
10401074
encoding?: string;
10411075
}
1042-
export function execSync(command: string, options?: ExecSyncOptions): string | Buffer;
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;
10431086

10441087
export interface ExecFileSyncOptions {
10451088
cwd?: string;
@@ -1053,7 +1096,19 @@ declare module "child_process" {
10531096
maxBuffer?: number;
10541097
encoding?: string;
10551098
}
1056-
export function execFileSync(command: string, args?: string[], options?: ExecFileSyncOptions): string | Buffer;
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;
10571112
}
10581113

10591114
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)