From 949cd224975f15bfeb1fd2d3a2e7ad284d4cbeab Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Mon, 4 Mar 2019 16:15:43 +0000 Subject: [PATCH] Add TypeScript definition (#11) --- index.d.ts | 45 +++++++++++++++++++++++++++++++++++++++++++++ index.js | 5 ++++- index.test-d.ts | 13 +++++++++++++ package.json | 10 ++++++---- readme.md | 1 + 5 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 index.d.ts create mode 100644 index.test-d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..d4608af --- /dev/null +++ b/index.d.ts @@ -0,0 +1,45 @@ +export interface Options { + /** + * **Don't use this option unless you really have to!** + * + * Suffix appended to the project name to avoid name conflicts with native apps. Pass an empty string to disable it. + * + * @default 'nodejs' + */ + readonly suffix?: string; +} + +export interface Paths { + /** + * Directory for data files. + */ + readonly data: string; + + /** + * Directory for data files. + */ + readonly config: string; + + /** + * Directory for non-essential data files. + */ + readonly cache: string; + + /** + * Directory for log files. + */ + readonly log: string; + + /** + * Directory for temporary files. + */ + readonly temp: string; +} + +/** + * Get paths for storing things like data, config, cache, etc. + * + * @param name - Name of your project. Used to generate the paths. + * @returns The paths to use for your project on current OS. + */ +export default function envPaths(name: string, options?: Options): Paths; diff --git a/index.js b/index.js index 083d9c7..426e267 100644 --- a/index.js +++ b/index.js @@ -46,7 +46,7 @@ const linux = name => { }; }; -module.exports = (name, options) => { +const envPaths = (name, options) => { if (typeof name !== 'string') { throw new TypeError(`Expected string, got ${typeof name}`); } @@ -68,3 +68,6 @@ module.exports = (name, options) => { return linux(name); }; + +module.exports = envPaths; +module.exports.default = envPaths; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..ee6ae65 --- /dev/null +++ b/index.test-d.ts @@ -0,0 +1,13 @@ +import {expectType} from 'tsd-check'; +import envPaths, {Paths} from '.'; + +expectType(envPaths('MyApp')); +expectType(envPaths('MyApp', {suffix: 'test'})); + +const paths = envPaths('MyApp'); + +expectType(paths.cache); +expectType(paths.config); +expectType(paths.data); +expectType(paths.log); +expectType(paths.temp); diff --git a/package.json b/package.json index 32ba82c..7936282 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,11 @@ "node": ">=6" }, "scripts": { - "test": "xo && ava" + "test": "xo && ava && tsd-check" }, "files": [ - "index.js" + "index.js", + "index.d.ts" ], "keywords": [ "common", @@ -37,7 +38,8 @@ "unix" ], "devDependencies": { - "ava": "^0.25.0", - "xo": "^0.23.0" + "ava": "^1.2.1", + "tsd-check": "^0.3.0", + "xo": "^0.24.0" } } diff --git a/readme.md b/readme.md index 8a20416..ec34393 100644 --- a/readme.md +++ b/readme.md @@ -16,6 +16,7 @@ $ npm install env-paths ```js const envPaths = require('env-paths'); + const paths = envPaths('MyApp'); paths.data;