Skip to content

Commit

Permalink
Add TypeScript type definitions (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing authored Aug 18, 2023
1 parent e49c127 commit 213a339
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
47 changes: 47 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export type Options = {
/**
Should be the same as your project name in `package.json`.
*/
readonly 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.
*/
readonly files?: string[] | string;

/**
Directory to start searching for a `package.json` from.
@default process.cwd()
*/
readonly cwd?: string;

/**
If `true`, the directory will be created synchronously before returning.
@default false
*/
readonly create?: boolean;

/**
If `true`, this modifies the return type to be a function that is a thunk for
`path.join(theFoundCacheDirectory)`.
@default false
*/
readonly thunk?: boolean;
};

/**
Finds the cache directory using the supplied options. The algorithm checks for
the `CACHE_DIR` environmental variable and uses it if it is not set to `true`,
`false`, `1` or `0`. If one is not found, it tries to find a `package.json`
file, searching every parent directory of the `cwd` specified (or implied from
other options). It returns a `string` containing the absolute path to the cache
directory, or `undefined` if `package.json` was never found or if the
`node_modules` directory is unwritable.
*/
export default function findCacheDirectory(
options: Options,
): string | undefined;
9 changes: 9 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {expectType} from 'tsd';
import findCacheDirectory from './index.js';

expectType<string | undefined>(findCacheDirectory({name: 'find-cache-dir'}));
expectType<string | undefined>(findCacheDirectory({name: 'find-cache-dir', files: '/foo'}));
expectType<string | undefined>(findCacheDirectory({name: 'find-cache-dir', files: ['/bar']}));
expectType<string | undefined>(findCacheDirectory({name: 'find-cache-dir', cwd: '/fooz'}));
expectType<string | undefined>(findCacheDirectory({name: 'find-cache-dir', create: true}));
expectType<string | undefined>(findCacheDirectory({name: 'find-cache-dir', thunk: true}));
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
"node": ">=14.16"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js"
"index.js",
"index.d.ts"
],
"keywords": [
"cache",
Expand All @@ -37,7 +38,8 @@
"ava": "^5.0.1",
"del": "^7.0.0",
"tempy": "^3.0.0",
"xo": "^0.52.4"
"tsd": "^0.28.0",
"xo": "^0.56.0"
},
"ava": {
"workerThreads": false
Expand Down

0 comments on commit 213a339

Please sign in to comment.