Skip to content

Commit

Permalink
Add TypeScript definition (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Mar 4, 2019
1 parent 4fdcf1e commit 949cd22
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
45 changes: 45 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand All @@ -68,3 +68,6 @@ module.exports = (name, options) => {

return linux(name);
};

module.exports = envPaths;
module.exports.default = envPaths;
13 changes: 13 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {expectType} from 'tsd-check';
import envPaths, {Paths} from '.';

expectType<Paths>(envPaths('MyApp'));
expectType<Paths>(envPaths('MyApp', {suffix: 'test'}));

const paths = envPaths('MyApp');

expectType<string>(paths.cache);
expectType<string>(paths.config);
expectType<string>(paths.data);
expectType<string>(paths.log);
expectType<string>(paths.temp);
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ $ npm install env-paths

```js
const envPaths = require('env-paths');

const paths = envPaths('MyApp');

paths.data;
Expand Down

0 comments on commit 949cd22

Please sign in to comment.