Skip to content

Commit

Permalink
improve node.d.ts child_process definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Mar 16, 2016
1 parent 45ac539 commit 39812a6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 15 deletions.
81 changes: 68 additions & 13 deletions node/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ declare var SlowBuffer: {


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

/**
Expand Down Expand Up @@ -967,30 +968,50 @@ declare module "child_process" {
export interface ExecOptions {
cwd?: string;
env?: any;
encoding?: string;
shell?: string;
timeout?: number;
maxBuffer?: number;
killSignal?: string;
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;
Expand All @@ -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<T> {
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<Buffer>;
export function spawnSync(command: string, options?: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns<string>;
export function spawnSync(command: string, options?: SpawnSyncOptionsWithBufferEncoding): SpawnSyncReturns<Buffer>;
export function spawnSync(command: string, options?: SpawnSyncOptions): SpawnSyncReturns<Buffer>;
export function spawnSync(command: string, args?: string[], options?: SpawnSyncOptionsWithStringEncoding): SpawnSyncReturns<string>;
export function spawnSync(command: string, args?: string[], options?: SpawnSyncOptionsWithBufferEncoding): SpawnSyncReturns<Buffer>;
export function spawnSync(command: string, args?: string[], options?: SpawnSyncOptions): SpawnSyncReturns<Buffer>;

export interface ExecSyncOptions {
cwd?: string;
Expand All @@ -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;
Expand All @@ -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" {
Expand Down
4 changes: 2 additions & 2 deletions tabtab/tabtab-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ 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));
if (/^--\w?/.test(data.last)) return tabtab.log(parsed.longs, data, '--');
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');
Expand Down

0 comments on commit 39812a6

Please sign in to comment.