From 45ac539215d010705754034b378ea6e0ac23db31 Mon Sep 17 00:00:00 2001 From: vvakame Date: Wed, 16 Mar 2016 10:59:21 +0900 Subject: [PATCH 1/2] extract options interface in node.d.ts child_process --- node/node.d.ts | 80 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 32 deletions(-) diff --git a/node/node.d.ts b/node/node.d.ts index 0a1e1ddc524012..66108434b742a8 100644 --- a/node/node.d.ts +++ b/node/node.d.ts @@ -227,8 +227,8 @@ declare module NodeJS { removeListener(event: string, listener: Function): this; removeAllListeners(event?: string): this; } - - export interface MemoryUsage { + + export interface MemoryUsage { rss: number; heapTotal: number; heapUsed: number; @@ -945,7 +945,7 @@ declare module "child_process" { stdin: stream.Writable; stdout: stream.Readable; stderr: stream.Readable; - stdio: (stream.Readable|stream.Writable)[]; + stdio: [stream.Writable, stream.Readable, stream.Readable]; pid: number; kill(signal?: string): void; send(message: any, sendHandle?: any): void; @@ -953,39 +953,46 @@ declare module "child_process" { unref(): void; } - export function spawn(command: string, args?: string[], options?: { + export interface SpawnOptions { cwd?: string; - stdio?: any; - custom?: any; env?: any; + stdio?: any; detached?: boolean; - }): ChildProcess; - export function exec(command: string, options: { + uid?: number; + gid?: number; + shell?: boolean | string; + } + export function spawn(command: string, args?: string[], options?: SpawnOptions): ChildProcess; + + export interface ExecOptions { cwd?: string; - stdio?: any; - customFds?: any; env?: any; encoding?: string; + shell?: string; timeout?: number; maxBuffer?: number; killSignal?: string; - }, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; + uid?: number; + gid?: number; + } + export function exec(command: string, options: ExecOptions, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; export function exec(command: string, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; - export function execFile(file: string, - callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; - export function execFile(file: string, args?: string[], - callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; - export function execFile(file: string, args?: string[], options?: { + + export interface ExecFileOptions { cwd?: string; - stdio?: any; - customFds?: any; env?: any; encoding?: string; timeout?: number; maxBuffer?: number; killSignal?: string; - }, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; - export function fork(modulePath: string, args?: string[], options?: { + uid?: number; + gid?: number; + } + export function execFile(file: string, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; + export function execFile(file: string, args?: string[], callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; + export function execFile(file: string, args?: string[], options?: ExecFileOptions, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; + + export interface ForkOptions { cwd?: string; env?: any; execPath?: string; @@ -993,8 +1000,10 @@ declare module "child_process" { silent?: boolean; uid?: number; gid?: number; - }): ChildProcess; - export function spawnSync(command: string, args?: string[], options?: { + } + export function fork(modulePath: string, args?: string[], options?: ForkOptions): ChildProcess; + + export interface SpawnSyncOptions { cwd?: string; input?: string | Buffer; stdio?: any; @@ -1002,10 +1011,12 @@ declare module "child_process" { uid?: number; gid?: number; timeout?: number; - maxBuffer?: number; killSignal?: string; + maxBuffer?: number; encoding?: string; - }): { + shell?: boolean | string; + } + export function spawnSync(command: string, args?: string[], options?: SpawnSyncOptions): { pid: number; output: string[]; stdout: string | Buffer; @@ -1014,30 +1025,35 @@ declare module "child_process" { signal: string; error: Error; }; - export function execSync(command: string, options?: { + + export interface ExecSyncOptions { cwd?: string; - input?: string|Buffer; + input?: string | Buffer; stdio?: any; env?: any; + shell?: string; uid?: number; gid?: number; timeout?: number; - maxBuffer?: number; killSignal?: string; + maxBuffer?: number; encoding?: string; - }): string | Buffer; - export function execFileSync(command: string, args?: string[], options?: { + } + export function execSync(command: string, options?: ExecSyncOptions): string | Buffer; + + export interface ExecFileSyncOptions { cwd?: string; - input?: string|Buffer; + input?: string | Buffer; stdio?: any; env?: any; uid?: number; gid?: number; timeout?: number; - maxBuffer?: number; killSignal?: string; + maxBuffer?: number; encoding?: string; - }): string | Buffer; + } + export function execFileSync(command: string, args?: string[], options?: ExecFileSyncOptions): string | Buffer; } declare module "url" { From 39812a6b1cd59ee1571127d87eebbbdcbb8b30db Mon Sep 17 00:00:00 2001 From: vvakame Date: Wed, 16 Mar 2016 13:20:00 +0900 Subject: [PATCH 2/2] improve node.d.ts child_process definitions --- node/node.d.ts | 81 +++++++++++++++++++++++++++++++++++------- tabtab/tabtab-tests.ts | 4 +-- 2 files changed, 70 insertions(+), 15 deletions(-) diff --git a/node/node.d.ts b/node/node.d.ts index 66108434b742a8..2176af4ab2dce6 100644 --- a/node/node.d.ts +++ b/node/node.d.ts @@ -80,6 +80,7 @@ declare var SlowBuffer: { // Buffer class +type BufferEncoding = "ascii" | "utf8" | "utf16le" | "ucs2" | "binary" | "hex"; interface Buffer extends NodeBuffer {} /** @@ -967,7 +968,6 @@ declare module "child_process" { export interface ExecOptions { cwd?: string; env?: any; - encoding?: string; shell?: string; timeout?: number; maxBuffer?: number; @@ -975,22 +975,43 @@ declare module "child_process" { uid?: number; gid?: number; } - export function exec(command: string, options: ExecOptions, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; - export function exec(command: string, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; + export interface ExecOptionsWithStringEncoding extends ExecOptions { + encoding: BufferEncoding; + } + export interface ExecOptionsWithBufferEncoding extends ExecOptions { + encoding: string; // specify `null`. + } + export function exec(command: string, callback?: (error: Error, stdout: string, stderr: string) =>void ): ChildProcess; + export function exec(command: string, options: ExecOptionsWithStringEncoding, callback?: (error: Error, stdout: string, stderr: string) =>void ): ChildProcess; + // usage. child_process.exec("tsc", {encoding: null as string}, (err, stdout, stderr) => {}); + export function exec(command: string, options: ExecOptionsWithBufferEncoding, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; + export function exec(command: string, options: ExecOptions, callback?: (error: Error, stdout: string, stderr: string) =>void ): ChildProcess; export interface ExecFileOptions { cwd?: string; env?: any; - encoding?: string; timeout?: number; maxBuffer?: number; killSignal?: string; uid?: number; gid?: number; } - export function execFile(file: string, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; - export function execFile(file: string, args?: string[], callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; - export function execFile(file: string, args?: string[], options?: ExecFileOptions, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; + export interface ExecFileOptionsWithStringEncoding extends ExecFileOptions { + encoding: BufferEncoding; + } + export interface ExecFileOptionsWithBufferEncoding extends ExecFileOptions { + encoding: string; // specify `null`. + } + export function execFile(file: string, callback?: (error: Error, stdout: string, stderr: string) =>void ): ChildProcess; + export function execFile(file: string, options?: ExecFileOptionsWithStringEncoding, callback?: (error: Error, stdout: string, stderr: string) =>void ): ChildProcess; + // usage. child_process.execFile("file.sh", {encoding: null as string}, (err, stdout, stderr) => {}); + export function execFile(file: string, options?: ExecFileOptionsWithBufferEncoding, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; + export function execFile(file: string, options?: ExecFileOptions, callback?: (error: Error, stdout: string, stderr: string) =>void ): ChildProcess; + export function execFile(file: string, args?: string[], callback?: (error: Error, stdout: string, stderr: string) =>void ): ChildProcess; + export function execFile(file: string, args?: string[], options?: ExecFileOptionsWithStringEncoding, callback?: (error: Error, stdout: string, stderr: string) =>void ): ChildProcess; + // usage. child_process.execFile("file.sh", ["foo"], {encoding: null as string}, (err, stdout, stderr) => {}); + export function execFile(file: string, args?: string[], options?: ExecFileOptionsWithBufferEncoding, callback?: (error: Error, stdout: Buffer, stderr: Buffer) =>void ): ChildProcess; + export function execFile(file: string, args?: string[], options?: ExecFileOptions, callback?: (error: Error, stdout: string, stderr: string) =>void ): ChildProcess; export interface ForkOptions { cwd?: string; @@ -1016,15 +1037,28 @@ declare module "child_process" { encoding?: string; shell?: boolean | string; } - export function spawnSync(command: string, args?: string[], options?: SpawnSyncOptions): { + export interface SpawnSyncOptionsWithStringEncoding extends SpawnSyncOptions { + encoding: BufferEncoding; + } + export interface SpawnSyncOptionsWithBufferEncoding extends SpawnSyncOptions { + encoding: string; // specify `null`. + } + export interface SpawnSyncReturns { pid: number; output: string[]; - stdout: string | Buffer; - stderr: string | Buffer; + stdout: T; + stderr: T; status: number; signal: string; error: Error; - }; + } + export function spawnSync(command: string): SpawnSyncReturns; + export function spawnSync(command: string, options?: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns; + export function spawnSync(command: string, options?: SpawnSyncOptionsWithBufferEncoding): SpawnSyncReturns; + export function spawnSync(command: string, options?: SpawnSyncOptions): SpawnSyncReturns; + export function spawnSync(command: string, args?: string[], options?: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns; + export function spawnSync(command: string, args?: string[], options?: SpawnSyncOptionsWithBufferEncoding): SpawnSyncReturns; + export function spawnSync(command: string, args?: string[], options?: SpawnSyncOptions): SpawnSyncReturns; export interface ExecSyncOptions { cwd?: string; @@ -1039,7 +1073,16 @@ declare module "child_process" { maxBuffer?: number; encoding?: string; } - export function execSync(command: string, options?: ExecSyncOptions): string | Buffer; + export interface ExecSyncOptionsWithStringEncoding extends ExecSyncOptions { + encoding: BufferEncoding; + } + export interface ExecSyncOptionsWithBufferEncoding extends ExecSyncOptions { + encoding: string; // specify `null`. + } + export function execSync(command: string): Buffer; + export function execSync(command: string, options?: ExecSyncOptionsWithStringEncoding): string; + export function execSync(command: string, options?: ExecSyncOptionsWithBufferEncoding): Buffer; + export function execSync(command: string, options?: ExecSyncOptions): Buffer; export interface ExecFileSyncOptions { cwd?: string; @@ -1053,7 +1096,19 @@ declare module "child_process" { maxBuffer?: number; encoding?: string; } - export function execFileSync(command: string, args?: string[], options?: ExecFileSyncOptions): string | Buffer; + export interface ExecFileSyncOptionsWithStringEncoding extends ExecFileSyncOptions { + encoding: BufferEncoding; + } + export interface ExecFileSyncOptionsWithBufferEncoding extends ExecFileSyncOptions { + encoding: string; // specify `null`. + } + export function execFileSync(command: string): Buffer; + export function execFileSync(command: string, options?: ExecFileSyncOptionsWithStringEncoding): string; + export function execFileSync(command: string, options?: ExecFileSyncOptionsWithBufferEncoding): Buffer; + export function execFileSync(command: string, options?: ExecFileSyncOptions): Buffer; + export function execFileSync(command: string, args?: string[], options?: ExecFileSyncOptionsWithStringEncoding): string; + export function execFileSync(command: string, args?: string[], options?: ExecFileSyncOptionsWithBufferEncoding): Buffer; + export function execFileSync(command: string, args?: string[], options?: ExecFileSyncOptions): Buffer; } declare module "url" { diff --git a/tabtab/tabtab-tests.ts b/tabtab/tabtab-tests.ts index 3204b7a05f35e4..69db6edbb3afcb 100644 --- a/tabtab/tabtab-tests.ts +++ b/tabtab/tabtab-tests.ts @@ -13,7 +13,7 @@ if (process.argv.slice(2)[0] === 'completion') { if (/^-\w?/.test(data.last)) return tabtab.log(['n', 'o', 'd', 'e'], data, '-'); tabtab.log(['list', 'of', 'commands'], data); - child_process.exec('rake -H', function(err, stdout, stderr) { + child_process.exec('rake -H', {encoding: null as string}, function(err, stdout, stderr) { if (err) return; var decoder = new string_decoder.StringDecoder('utf8'); var parsed = tabtab.parseOut(decoder.write(stdout)); @@ -21,7 +21,7 @@ if (process.argv.slice(2)[0] === 'completion') { if (/^-\w?/.test(data.last)) return tabtab.log(parsed.shorts, data, '-'); }); - child_process.exec('cake', function(err, stdout, stderr) { + child_process.exec('cake', {encoding: null as string}, function(err, stdout, stderr) { if (err) return; var decoder = new string_decoder.StringDecoder('utf8'); var tasks = tabtab.parseTasks(decoder.write(stdout), 'cake');