Skip to content

Commit 0aae4d6

Browse files
BendingBendersindresorhus
authored andcommitted
Add TypeScript definition (#9)
1 parent ddee7ea commit 0aae4d6

File tree

5 files changed

+88
-17
lines changed

5 files changed

+88
-17
lines changed

index.d.ts

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
declare namespace globalDirectories {
2+
interface GlobalDirectories {
3+
/**
4+
Directory with globally installed packages.
5+
6+
Equivalent to `npm root --global`.
7+
*/
8+
readonly packages: string;
9+
10+
/**
11+
Directory with globally installed binaries.
12+
13+
Equivalent to `npm bin --global`.
14+
*/
15+
readonly binaries: string;
16+
17+
/**
18+
Directory with directories for packages and binaries. You probably want either of the above.
19+
20+
Equivalent to `npm prefix --global`.
21+
*/
22+
readonly prefix: string;
23+
}
24+
}
25+
26+
declare const globalDirectories: {
27+
/**
28+
Get the directory of globally installed packages and binaries.
29+
30+
@example
31+
```
32+
import * as globalDirectories from 'global-dirs';
33+
34+
console.log(globalDirectories.npm.prefix);
35+
//=> '/usr/local'
36+
37+
console.log(globalDirectories.npm.packages);
38+
//=> '/usr/local/lib/node_modules'
39+
```
40+
*/
41+
readonly npm: globalDirectories.GlobalDirectories;
42+
43+
/**
44+
Get the directory of globally installed packages and binaries.
45+
46+
@example
47+
```
48+
import * as globalDirectories from 'global-dirs';
49+
50+
console.log(globalDirectories.npm.binaries);
51+
//=> '/usr/local/bin'
52+
53+
console.log(globalDirectories.yarn.packages);
54+
//=> '/Users/sindresorhus/.config/yarn/global/node_modules'
55+
```
56+
*/
57+
readonly yarn: globalDirectories.GlobalDirectories;
58+
}
59+
60+
export = globalDirectories;

index.test-d.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {expectType} from 'tsd';
2+
import globalDirectories = require('.');
3+
4+
expectType<string>(globalDirectories.npm.prefix);
5+
expectType<string>(globalDirectories.npm.packages);
6+
expectType<string>(globalDirectories.npm.binaries);
7+
expectType<string>(globalDirectories.yarn.prefix);
8+
expectType<string>(globalDirectories.yarn.packages);
9+
expectType<string>(globalDirectories.yarn.binaries);

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
"node": ">=8"
1414
},
1515
"scripts": {
16-
"test": "xo && ava"
16+
"test": "xo && ava && tsd"
1717
},
1818
"files": [
19-
"index.js"
19+
"index.js",
20+
"index.d.ts"
2021
],
2122
"keywords": [
2223
"global",
@@ -47,6 +48,7 @@
4748
"devDependencies": {
4849
"ava": "^1.4.1",
4950
"execa": "^1.0.0",
51+
"tsd": "^0.7.2",
5052
"xo": "^0.24.0"
5153
}
5254
}

readme.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,25 @@ $ npm install global-dirs
1515
## Usage
1616

1717
```js
18-
const globalDirs = require('global-dirs');
18+
const globalDirectories = require('global-dirs');
1919

20-
console.log(globalDirs.npm.prefix);
20+
console.log(globalDirectories.npm.prefix);
2121
//=> '/usr/local'
2222

23-
console.log(globalDirs.npm.packages);
23+
console.log(globalDirectories.npm.packages);
2424
//=> '/usr/local/lib/node_modules'
2525

26-
console.log(globalDirs.npm.binaries);
26+
console.log(globalDirectories.npm.binaries);
2727
//=> '/usr/local/bin'
2828

29-
console.log(globalDirs.yarn.packages);
29+
console.log(globalDirectories.yarn.packages);
3030
//=> '/Users/sindresorhus/.config/yarn/global/node_modules'
3131
```
3232

3333

3434
## API
3535

36-
### globalDirs
36+
### globalDirectories
3737

3838
#### npm
3939
#### yarn

test.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import test from 'ava';
22
import execa from 'execa';
3-
import globalDirs from '.';
3+
import globalDirectories from '.';
44

5-
console.log(globalDirs);
5+
console.log(globalDirectories);
66

77
const npm = arguments_ => execa.stdout('npm', arguments_);
88

99
test('npm.prefix', async t => {
10-
t.is(globalDirs.npm.prefix, await npm(['prefix', '--global']));
10+
t.is(globalDirectories.npm.prefix, await npm(['prefix', '--global']));
1111
});
1212

1313
test('npm.packages', async t => {
14-
t.is(globalDirs.npm.packages, await npm(['root', '--global']));
14+
t.is(globalDirectories.npm.packages, await npm(['root', '--global']));
1515
});
1616

1717
test('npm.binaries', async t => {
18-
t.is(globalDirs.npm.binaries, await npm(['bin', '--global']));
18+
t.is(globalDirectories.npm.binaries, await npm(['bin', '--global']));
1919
});
2020

2121
test('yarn', async t => {
2222
await npm(['install', '--global', 'yarn']);
23-
t.truthy(globalDirs.yarn);
24-
t.truthy(globalDirs.yarn.prefix);
25-
t.truthy(globalDirs.yarn.packages);
26-
t.truthy(globalDirs.yarn.binaries);
23+
t.truthy(globalDirectories.yarn);
24+
t.truthy(globalDirectories.yarn.prefix);
25+
t.truthy(globalDirectories.yarn.packages);
26+
t.truthy(globalDirectories.yarn.binaries);
2727
});

0 commit comments

Comments
 (0)