From ee8ee5429585c9b52afc76aa578c3dd30438a4d0 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Mon, 31 Dec 2018 16:11:17 +0000 Subject: [PATCH] [find-cache-dir] Add types (#31800) --- types/find-cache-dir/find-cache-dir-tests.ts | 19 +++++++ types/find-cache-dir/index.d.ts | 57 ++++++++++++++++++++ types/find-cache-dir/tsconfig.json | 23 ++++++++ types/find-cache-dir/tslint.json | 1 + 4 files changed, 100 insertions(+) create mode 100644 types/find-cache-dir/find-cache-dir-tests.ts create mode 100644 types/find-cache-dir/index.d.ts create mode 100644 types/find-cache-dir/tsconfig.json create mode 100644 types/find-cache-dir/tslint.json diff --git a/types/find-cache-dir/find-cache-dir-tests.ts b/types/find-cache-dir/find-cache-dir-tests.ts new file mode 100644 index 0000000000..5836be99b6 --- /dev/null +++ b/types/find-cache-dir/find-cache-dir-tests.ts @@ -0,0 +1,19 @@ +import findCacheDir = require('find-cache-dir'); + +findCacheDir({ name: 'unicorns' }); // $ExpectType string | null +findCacheDir({ name: 'unicorns', files: 'foo' }); // $ExpectType string | null +findCacheDir({ name: 'unicorns', files: ['foo', 'bar'] }); // $ExpectType string | null +findCacheDir({ name: 'unicorns', cwd: 'foo' }); // $ExpectType string | null +findCacheDir({ name: 'unicorns', create: true }); // $ExpectType string | null +findCacheDir({ name: 'unicorns', thunk: false }); // $ExpectType string | null +findCacheDir({}); // $ExpectError +findCacheDir(); // $ExpectError + +const thunk = findCacheDir({ name: 'unicorns', thunk: true }); +thunk; // $ExpectType ((...pathParts: string[]) => string) | null + +if (thunk) { + thunk(); // $ExpectType string + thunk('bar.js'); // $ExpectType string + thunk('baz', 'quz.js'); // $ExpectType string +} diff --git a/types/find-cache-dir/index.d.ts b/types/find-cache-dir/index.d.ts new file mode 100644 index 0000000000..4b69518e21 --- /dev/null +++ b/types/find-cache-dir/index.d.ts @@ -0,0 +1,57 @@ +// Type definitions for find-cache-dir 2.0 +// Project: https://github.com/avajs/find-cache-dir#readme +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export = findCacheDir; + +/** + * Finds the cache directory using the supplied options. The algorithm tries to find a `package.json` file, + * searching every parent directory of the `cwd` specified (or implied from other options). + * @param options + * @returns A string containing the absolute path to the cache directory, or null if package.json was never found. + */ +declare function findCacheDir( + options: findCacheDir.OptionsWithThunk +): ((...pathParts: string[]) => string) | null; +declare function findCacheDir(options: findCacheDir.Options): string | null; + +declare namespace findCacheDir { + interface Options { + /** + * Should be the same as your project name in `package.json`. + */ + name: string; + + /** + * An array of files that will be searched for a common parent directory. + * This common parent directory will be used in lieu of the `cwd` option below. + */ + files?: string | string[]; + + /** + * Directory to start searching for a `package.json` from. + */ + cwd?: string; + + /** + * If `true`, the directory will be created synchronously before returning. + * @default false + */ + create?: boolean; + + /** + * If `true`, this modifies the return type to be a function that is a thunk for `path.join(theFoundCacheDirectory)`. + * @default false + */ + thunk?: boolean; + } + + interface OptionsWithThunk extends Options { + /** + * If `true`, this modifies the return type to be a function that is a thunk for `path.join(theFoundCacheDirectory)`. + * @default false + */ + thunk: true; + } +} diff --git a/types/find-cache-dir/tsconfig.json b/types/find-cache-dir/tsconfig.json new file mode 100644 index 0000000000..68068b9343 --- /dev/null +++ b/types/find-cache-dir/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", + "find-cache-dir-tests.ts" + ] +} diff --git a/types/find-cache-dir/tslint.json b/types/find-cache-dir/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/find-cache-dir/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }