Skip to content

Commit aeafb93

Browse files
committedOct 2, 2021
Cleanup
1 parent 198c9fe commit aeafb93

File tree

7 files changed

+67
-44
lines changed

7 files changed

+67
-44
lines changed
 

‎.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- 12
1616
steps:
1717
- uses: actions/checkout@v2
18-
- uses: actions/setup-node@v1
18+
- uses: actions/setup-node@v2
1919
with:
2020
node-version: ${{ matrix.node-version }}
2121
- run: npm install

‎index.d.ts

+34-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
export interface Options {
2+
/**
3+
The directory to start searching from.
4+
5+
@default process.cwd()
6+
*/
7+
readonly cwd?: string;
8+
}
9+
110
/**
211
Find the root directory of a Node.js project or npm package.
312
4-
@param cwd - Directory to start from. Default: `process.cwd()`.
5-
@returns The project root path or `undefined` if it couldn't be found.
13+
@returns The project root path or `undefined` if it could not be found.
614
715
@example
816
```
@@ -16,22 +24,35 @@ Find the root directory of a Node.js project or npm package.
1624
// └── example.js
1725
1826
// example.js
19-
import pkgDir = require('pkg-dir');
20-
21-
(async () => {
22-
const rootDir = await pkgDir(__dirname);
27+
import {packageDirectory} from 'pkg-dir';
2328
24-
console.log(rootDir);
25-
//=> '/Users/sindresorhus/foo'
26-
})();
29+
console.log(await packageDirectory());
30+
//=> '/Users/sindresorhus/foo'
2731
```
2832
*/
29-
export function pkgDir(cwd: any): Promise<string>;
33+
export function packageDirectory(options?: Options): Promise<string>;
3034

3135
/**
3236
Synchronously find the root directory of a Node.js project or npm package.
3337
34-
@param cwd - Directory to start from. Default: `process.cwd()`.
35-
@returns The project root path or `undefined` if it couldn't be found.
38+
@returns The project root path or `undefined` if it could not be found.
39+
40+
@example
41+
```
42+
// /
43+
// └── Users
44+
// └── sindresorhus
45+
// └── foo
46+
// ├── package.json
47+
// └── bar
48+
// ├── baz
49+
// └── example.js
50+
51+
// example.js
52+
import {packageDirectorySync} from 'pkg-dir';
53+
54+
console.log(packageDirectorySync());
55+
//=> '/Users/sindresorhus/foo'
56+
```
3657
*/
37-
export function pkgDirSync(cwd: any): string;
58+
export function packageDirectorySync(options?: Options): string;

‎index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import {dirname} from 'node:path';
1+
import path from 'node:path';
22
import {findUp, findUpSync} from 'find-up';
33

4-
export async function pkgDir(cwd) {
4+
export async function packageDirectory({cwd}) {
55
const filePath = await findUp('package.json', {cwd});
6-
return filePath && dirname(filePath);
6+
return filePath && path.dirname(filePath);
77
}
88

9-
export function pkgDirSync(cwd) {
9+
export function packageDirectorySync({cwd}) {
1010
const filePath = findUpSync('package.json', {cwd});
11-
return filePath && dirname(filePath);
11+
return filePath && path.dirname(filePath);
1212
}

‎index.test-d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {expectType} from 'tsd';
2-
import {pkgDir, pkgDirSync} from './index.js';
2+
import {packageDirectory, packageDirectorySync} from './index.js';
33

4-
expectType<Promise<string>>(pkgDir('/Users/project/pkg-dir'));
5-
expectType<string>(pkgDirSync('/Users/project/pkg-dir'));
4+
expectType<Promise<string>>(packageDirectory({cwd: '/Users/project/pkg-dir'}));
5+
expectType<string>(packageDirectorySync({cwd: '/Users/project/pkg-dir'}));

‎package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
"description": "Find the root directory of a Node.js project or npm package",
55
"license": "MIT",
66
"repository": "sindresorhus/pkg-dir",
7+
"funding": "https://github.com/sponsors/sindresorhus",
78
"author": {
89
"name": "Sindre Sorhus",
910
"email": "sindresorhus@gmail.com",
1011
"url": "https://sindresorhus.com"
1112
},
1213
"type": "module",
14+
"exports": "./index.js",
1315
"engines": {
14-
"node": ">=12"
16+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
1517
},
1618
"scripts": {
1719
"test": "xo && ava && tsd"
@@ -40,7 +42,6 @@
4042
"parents",
4143
"folder",
4244
"directory",
43-
"dir",
4445
"walk",
4546
"walking",
4647
"path"
@@ -52,6 +53,7 @@
5253
"ava": "^3.15.0",
5354
"tempy": "^2.0.0",
5455
"tsd": "^0.17.0",
56+
"typescript": "^4.4.3",
5557
"xo": "^0.44.0"
5658
}
5759
}

‎readme.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
55
## Install
66

7-
```
8-
$ npm install pkg-dir
7+
```sh
8+
npm install pkg-dir
99
```
1010

1111
## Usage
@@ -23,32 +23,32 @@ $ npm install pkg-dir
2323

2424
```js
2525
// example.js
26-
import {pkgDir, pkgDirSync} from 'pkg-dir';
27-
28-
(async () => {
29-
const rootDir = await pkgDir(__dirname);
26+
import {packageDirectory} from 'pkg-dir';
3027

31-
console.log(rootDir);
32-
//=> '/Users/sindresorhus/foo'
33-
})();
28+
console.log(await packageDirectory());
29+
//=> '/Users/sindresorhus/foo'
3430
```
3531

3632
## API
3733

38-
### pkgDir(cwd?)
34+
### packageDirectory(option?)
35+
36+
Returns a `Promise` for either the project root path or `undefined` if it could not be found.
37+
38+
### packageDirectorySync(options?)
3939

40-
Returns a `Promise` for either the project root path or `undefined` if it couldn't be found.
40+
Returns the project root path or `undefined` if it could not be found.
4141

42-
### pkgDirSync(cwd?)
42+
#### options
4343

44-
Returns the project root path or `undefined` if it couldn't be found.
44+
Type: `object`
4545

46-
#### cwd
46+
##### cwd
4747

4848
Type: `string`\
4949
Default: `process.cwd()`
5050

51-
Directory to start from.
51+
The directory to start searching from.
5252

5353
## Related
5454

‎test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'node:path';
33
import {fileURLToPath} from 'node:url';
44
import test from 'ava';
55
import tempy from 'tempy';
6-
import {pkgDir, pkgDirSync} from './index.js';
6+
import {packageDirectory, packageDirectorySync} from './index.js';
77

88
const __dirname = path.dirname(fileURLToPath(import.meta.url));
99

@@ -17,11 +17,11 @@ test.afterEach(t => {
1717
});
1818

1919
test('async', async t => {
20-
t.is(await pkgDir(path.join(__dirname, 'fixture')), __dirname);
21-
t.is(await pkgDir(t.context.disjoint), undefined);
20+
t.is(await packageDirectory({cwd: path.join(__dirname, 'fixture')}), __dirname);
21+
t.is(await packageDirectory({cwd: t.context.disjoint}), undefined);
2222
});
2323

2424
test('sync', t => {
25-
t.is(pkgDirSync(path.join(__dirname, 'fixture')), __dirname);
26-
t.is(pkgDirSync(t.context.disjoint), undefined);
25+
t.is(packageDirectorySync({cwd: path.join(__dirname, 'fixture')}), __dirname);
26+
t.is(packageDirectorySync({cwd: t.context.disjoint}), undefined);
2727
});

0 commit comments

Comments
 (0)
Please sign in to comment.