From 428195bee21d60868efcb2dbcb90b7ab01b88aa4 Mon Sep 17 00:00:00 2001 From: Rogier Schouten Date: Thu, 6 Oct 2016 18:56:16 +0200 Subject: [PATCH] Improve temp function signatures (#11774) --- temp/index.d.ts | 45 ++++++++++++++++++++++++++++----------------- temp/temp-tests.ts | 9 +++++---- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/temp/index.d.ts b/temp/index.d.ts index bb2f1abbf7..a6b6f42ace 100644 --- a/temp/index.d.ts +++ b/temp/index.d.ts @@ -8,34 +8,45 @@ import * as temp from "temp"; import * as fs from "fs"; +export interface OpenFile { + path: string; + fd: number; +} + +export interface Stats { + files: number; + dirs: number; +} + export interface AffixOptions { - prefix?: string; - suffix?: string; - dir?: string; + prefix?: string; + suffix?: string; + dir?: string; } export declare var dir: string; export declare function track(value?: boolean): typeof temp; -export declare function mkdir(affixes: string, callback?: (err: any, dirPath: string) => void): void; -export declare function mkdir(affixes: AffixOptions, callback?: (err: any, dirPath: string) => void): void; +export declare function mkdir(affixes?: string, callback?: (err: any, dirPath: string) => void): void; +export declare function mkdir(affixes?: AffixOptions, callback?: (err: any, dirPath: string) => void): void; -export declare function mkdirSync(affixes: string): string; -export declare function mkdirSync(affixes: AffixOptions): string; +export declare function mkdirSync(affixes?: string): string; +export declare function mkdirSync(affixes?: AffixOptions): string; -export declare function open(affixes: string, callback?: (err: any, result: { path: string, fd: number }) => void): void; -export declare function open(affixes: AffixOptions, callback?: (err: any, result: { path: string, fd: number }) => void): void; +export declare function open(affixes?: string, callback?: (err: any, result: OpenFile) => void): void; +export declare function open(affixes?: AffixOptions, callback?: (err: any, result: OpenFile) => void): void; -export declare function openSync(affixes: string): { path: string, fd: number }; -export declare function openSync(affixes: AffixOptions): { path: string, fd: number }; +export declare function openSync(affixes?: string): OpenFile; +export declare function openSync(affixes?: AffixOptions): OpenFile; -export declare function path(affixes: string, defaultPrefix?: string): string; -export declare function path(affixes: AffixOptions, defaultPrefix?: string): string; +export declare function path(affixes?: string, defaultPrefix?: string): string; +export declare function path(affixes?: AffixOptions, defaultPrefix?: string): string; -export declare function cleanup(callback?: (result: boolean | { files: number, dirs?: number }) => void): void; +export declare function cleanup(callback?: (result: boolean | Stats) => void): void; -export declare function cleanupSync(): boolean | { files: number, dirs: number }; +export declare function cleanupSync(): boolean | Stats; + +export declare function createWriteStream(affixes?: string): fs.WriteStream; +export declare function createWriteStream(affixes?: AffixOptions): fs.WriteStream; -export declare function createWriteStream(affixes: string): fs.WriteStream; -export declare function createWriteStream(affixes: AffixOptions): fs.WriteStream; diff --git a/temp/temp-tests.ts b/temp/temp-tests.ts index 3f8ed01327..78eff82298 100644 --- a/temp/temp-tests.ts +++ b/temp/temp-tests.ts @@ -16,7 +16,7 @@ function testCleanup() { } function testCleanupSync() { - const cleanupResult = temp.cleanupSync() + const cleanupResult: boolean | temp.Stats = temp.cleanupSync() if (typeof cleanupResult === "boolean") { const x = cleanupResult === true; } @@ -42,13 +42,14 @@ function testOpen() { } function testOpenSync() { - const { fd: openFd1, path: openPath1 } = temp.openSync({ dir: "tempDir", prefix: "pref", suffix: "suff" }); - const { fd: openFd2, path: openPath2 } = temp.openSync("str"); + const f1: temp.OpenFile = temp.openSync({ dir: "tempDir", prefix: "pref", suffix: "suff" }); + const f2: temp.OpenFile = temp.openSync("str"); } function testCreateWriteStream() { const stream = temp.createWriteStream("HelloStreamAffix"); stream.write("data"); + const stream2 = temp.createWriteStream(); } function testMkdir() { @@ -73,4 +74,4 @@ function testTrack() { const tempChained = temp.track().track(true).track(false); tempChained.dir; tempChained.cleanupSync(); -} \ No newline at end of file +}