Skip to content

Commit 949cd22

Browse files
BendingBendersindresorhus
authored andcommitted
Add TypeScript definition (#11)
1 parent 4fdcf1e commit 949cd22

5 files changed

Lines changed: 69 additions & 5 deletions

File tree

index.d.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
export interface Options {
2+
/**
3+
* **Don't use this option unless you really have to!**
4+
*
5+
* Suffix appended to the project name to avoid name conflicts with native apps. Pass an empty string to disable it.
6+
*
7+
* @default 'nodejs'
8+
*/
9+
readonly suffix?: string;
10+
}
11+
12+
export interface Paths {
13+
/**
14+
* Directory for data files.
15+
*/
16+
readonly data: string;
17+
18+
/**
19+
* Directory for data files.
20+
*/
21+
readonly config: string;
22+
23+
/**
24+
* Directory for non-essential data files.
25+
*/
26+
readonly cache: string;
27+
28+
/**
29+
* Directory for log files.
30+
*/
31+
readonly log: string;
32+
33+
/**
34+
* Directory for temporary files.
35+
*/
36+
readonly temp: string;
37+
}
38+
39+
/**
40+
* Get paths for storing things like data, config, cache, etc.
41+
*
42+
* @param name - Name of your project. Used to generate the paths.
43+
* @returns The paths to use for your project on current OS.
44+
*/
45+
export default function envPaths(name: string, options?: Options): Paths;

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const linux = name => {
4646
};
4747
};
4848

49-
module.exports = (name, options) => {
49+
const envPaths = (name, options) => {
5050
if (typeof name !== 'string') {
5151
throw new TypeError(`Expected string, got ${typeof name}`);
5252
}
@@ -68,3 +68,6 @@ module.exports = (name, options) => {
6868

6969
return linux(name);
7070
};
71+
72+
module.exports = envPaths;
73+
module.exports.default = envPaths;

index.test-d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {expectType} from 'tsd-check';
2+
import envPaths, {Paths} from '.';
3+
4+
expectType<Paths>(envPaths('MyApp'));
5+
expectType<Paths>(envPaths('MyApp', {suffix: 'test'}));
6+
7+
const paths = envPaths('MyApp');
8+
9+
expectType<string>(paths.cache);
10+
expectType<string>(paths.config);
11+
expectType<string>(paths.data);
12+
expectType<string>(paths.log);
13+
expectType<string>(paths.temp);

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
"node": ">=6"
1414
},
1515
"scripts": {
16-
"test": "xo && ava"
16+
"test": "xo && ava && tsd-check"
1717
},
1818
"files": [
19-
"index.js"
19+
"index.js",
20+
"index.d.ts"
2021
],
2122
"keywords": [
2223
"common",
@@ -37,7 +38,8 @@
3738
"unix"
3839
],
3940
"devDependencies": {
40-
"ava": "^0.25.0",
41-
"xo": "^0.23.0"
41+
"ava": "^1.2.1",
42+
"tsd-check": "^0.3.0",
43+
"xo": "^0.24.0"
4244
}
4345
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ $ npm install env-paths
1616

1717
```js
1818
const envPaths = require('env-paths');
19+
1920
const paths = envPaths('MyApp');
2021

2122
paths.data;

0 commit comments

Comments
 (0)