From 6fe162ecd915cf2722cf772eee7df2afbd2d206b Mon Sep 17 00:00:00 2001 From: Denis Malinochkin Date: Fri, 19 Jan 2018 00:15:17 +0300 Subject: [PATCH] =?UTF-8?q?feat(types):=20Add=20types=20for=20the=20=C2=AB?= =?UTF-8?q?readdir-enhanced=C2=BB=20package=20(#23022)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/readdir-enhanced/index.d.ts | 72 ++++++++++++++++ .../readdir-enhanced-tests.ts | 85 +++++++++++++++++++ types/readdir-enhanced/tsconfig.json | 23 +++++ types/readdir-enhanced/tslint.json | 1 + 4 files changed, 181 insertions(+) create mode 100644 types/readdir-enhanced/index.d.ts create mode 100644 types/readdir-enhanced/readdir-enhanced-tests.ts create mode 100644 types/readdir-enhanced/tsconfig.json create mode 100644 types/readdir-enhanced/tslint.json diff --git a/types/readdir-enhanced/index.d.ts b/types/readdir-enhanced/index.d.ts new file mode 100644 index 0000000000..afd724d57d --- /dev/null +++ b/types/readdir-enhanced/index.d.ts @@ -0,0 +1,72 @@ +// Type definitions for readdir-enhanced 2.2 +// Project: https://github.com/bigstickcarpet/readdir-enhanced +// Definitions by: mrmlnc +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +import fs = require('fs'); + +declare namespace re { + interface Entry extends fs.Stats { + path: string; + depth: number; + } + + type FilterFunction = (stat: Entry) => boolean; + type Callback = (err: NodeJS.ErrnoException, result: T) => void; + type CallbackString = Callback; + type CallbackEntry = Callback; + + interface FileSystem { + readdir?: (path: string, callback: Callback) => void; + lstat?: (path: string, callback: Callback) => void; + stat?: (path: string, callback: Callback) => void; + } + + interface Options { + filter?: string | RegExp | FilterFunction; + deep?: boolean | number | RegExp | FilterFunction; + sep?: string; + basePath?: string; + fs?: FileSystem; + } + + function stat(root: string, options?: Options): Promise; + function stat(root: string, callback: CallbackEntry): void; + function stat(root: string, options: Options, callback: CallbackEntry): void; + + function async(root: string, options?: Options): Promise; + function async(root: string, callback: CallbackString): void; + function async(root: string, options: Options, callback: CallbackString): void; + + function readdirAsyncStat(root: string, options?: Options): Promise; + function readdirAsyncStat(root: string, callback: CallbackEntry): void; + function readdirAsyncStat(root: string, options: Options, callback: CallbackEntry): void; + + namespace async { + function stat(root: string, options?: Options): Promise; + function stat(root: string, callback: CallbackEntry): void; + function stat(root: string, options: Options, callback: CallbackEntry): void; + } + + function stream(root: string, options?: Options): NodeJS.ReadableStream; + function readdirStreamStat(root: string, options?: Options): NodeJS.ReadableStream; + + namespace stream { + function stat(root: string, options?: Options): NodeJS.ReadableStream; + } + + function sync(root: string, options?: Options): string[]; + function readdirSyncStat(root: string, options?: Options): Entry[]; + + namespace sync { + function stat(root: string, options?: Options): Entry[]; + } +} + +declare function re(root: string, options?: re.Options): Promise; +declare function re(root: string, callback: re.CallbackString): void; +declare function re(root: string, options: re.Options, callback: re.CallbackString): void; + +export = re; diff --git a/types/readdir-enhanced/readdir-enhanced-tests.ts b/types/readdir-enhanced/readdir-enhanced-tests.ts new file mode 100644 index 0000000000..97ea5f76f2 --- /dev/null +++ b/types/readdir-enhanced/readdir-enhanced-tests.ts @@ -0,0 +1,85 @@ +import fs = require('fs'); + +import re = require('readdir-enhanced'); + +const reFs: re.FileSystem = { + stat: fs.stat, + lstat: fs.lstat, + readdir: fs.readdir +}; + +const reOptions: re.Options = { + basePath: '', + fs: reFs, + sep: '/' +}; + +reOptions.deep = true; +reOptions.deep = 2; +reOptions.deep = /regexp/; +reOptions.deep = () => true; + +reOptions.deep = () => 0; // $ExpectError +reOptions.deep = ''; // $ExpectError + +reOptions.filter = ''; +reOptions.filter = /regexp/; +reOptions.filter = () => true; + +reOptions.filter = () => 0; // $ExpectError +reOptions.filter = 1; // $ExpectError + +const callbackString: re.CallbackString = (err, result) => { }; +const callbackEntry: re.CallbackEntry = (err, result) => { }; + +re('root', callbackString); // $ExpectType void +re('root', reOptions, callbackString); // $ExpectType void +re('root'); // $ExpectType Promise +re('root', reOptions); // $ExpectType Promise + +re.stat('root', callbackEntry); // $ExpectType void +re.stat('root', reOptions, callbackEntry); // $ExpectType void +re.stat('root'); // $ExpectType Promise +re.stat('root', reOptions); // $ExpectType Promise + +re.readdirAsyncStat('root', callbackEntry); // $ExpectType void +re.readdirAsyncStat('root', reOptions, callbackEntry); // $ExpectType void +re.readdirAsyncStat('root'); // $ExpectType Promise +re.readdirAsyncStat('root', reOptions); // $ExpectType Promise + +re.async('root', callbackString); // $ExpectType void +re.async('root', reOptions, callbackString); // $ExpectType void +re.async('root'); // $ExpectType Promise +re.async('root', reOptions); // $ExpectType Promise + +re.async.stat('root', callbackEntry); // $ExpectType void +re.async.stat('root', reOptions, callbackEntry); // $ExpectType void +re.async.stat('root'); // $ExpectType Promise +re.async.stat('root', reOptions); // $ExpectType Promise + +re.stream('root'); // $ExpectType ReadableStream +re.stream('root', reOptions); // $ExpectType ReadableStream + +re.stream.stat('root'); // $ExpectType ReadableStream +re.stream.stat('root', reOptions); // $ExpectType ReadableStream + +re.readdirStreamStat('root'); // $ExpectType ReadableStream +re.readdirStreamStat('root', reOptions); // $ExpectType ReadableStream + +re.sync('root'); // $ExpectType string[] +re.sync('root', reOptions); // $ExpectType string[] + +re.sync.stat('root'); // $ExpectType Entry[] +re.sync.stat('root', reOptions); // $ExpectType Entry[] + +re.readdirSyncStat('root'); // $ExpectType Entry[] +re.readdirSyncStat('root', reOptions); // $ExpectType Entry[] + +// Entry +re.sync('root'); // $ExpectType string[] + +const entries = re.sync.stat('root'); // $ExpectType Entry[] + +entries[0]; // $ExpectType Entry +entries[0].path; // $ExpectType string +entries[0].depth; // $ExpectType number diff --git a/types/readdir-enhanced/tsconfig.json b/types/readdir-enhanced/tsconfig.json new file mode 100644 index 0000000000..a15bc6fd77 --- /dev/null +++ b/types/readdir-enhanced/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "readdir-enhanced-tests.ts" + ] +} diff --git a/types/readdir-enhanced/tslint.json b/types/readdir-enhanced/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/readdir-enhanced/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }