Skip to content

Commit 198c9fe

Browse files
authoredOct 2, 2021
Require Node.js 12 and move to ESM (#14)
1 parent 26f7493 commit 198c9fe

File tree

7 files changed

+67
-70
lines changed

7 files changed

+67
-70
lines changed
 

‎.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13+
- 16
1314
- 14
1415
- 12
15-
- 10
1616
steps:
1717
- uses: actions/checkout@v2
1818
- uses: actions/setup-node@v1

‎index.d.ts

+37-41
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
1-
declare const pkgDir: {
2-
/**
3-
Synchronously find the root directory of a Node.js project or npm package.
4-
5-
@param cwd - Directory to start from. Default: `process.cwd()`.
6-
@returns The project root path or `undefined` if it couldn't be found.
7-
*/
8-
sync: (cwd?: string) => string | undefined;
9-
10-
/**
11-
Find the root directory of a Node.js project or npm package.
12-
13-
@param cwd - Directory to start from. Default: `process.cwd()`.
14-
@returns The project root path or `undefined` if it couldn't be found.
15-
16-
@example
17-
```
18-
// /
19-
// └── Users
20-
// └── sindresorhus
21-
// └── foo
22-
// ├── package.json
23-
// └── bar
24-
// ├── baz
25-
// └── example.js
26-
27-
// example.js
28-
import pkgDir = require('pkg-dir');
29-
30-
(async () => {
31-
const rootDir = await pkgDir(__dirname);
32-
33-
console.log(rootDir);
34-
//=> '/Users/sindresorhus/foo'
35-
})();
36-
```
37-
*/
38-
(cwd?: string): Promise<string | undefined>;
39-
};
40-
41-
export = pkgDir;
1+
/**
2+
Find the root directory of a Node.js project or npm package.
3+
4+
@param cwd - Directory to start from. Default: `process.cwd()`.
5+
@returns The project root path or `undefined` if it couldn't be found.
6+
7+
@example
8+
```
9+
// /
10+
// └── Users
11+
// └── sindresorhus
12+
// └── foo
13+
// ├── package.json
14+
// └── bar
15+
// ├── baz
16+
// └── example.js
17+
18+
// example.js
19+
import pkgDir = require('pkg-dir');
20+
21+
(async () => {
22+
const rootDir = await pkgDir(__dirname);
23+
24+
console.log(rootDir);
25+
//=> '/Users/sindresorhus/foo'
26+
})();
27+
```
28+
*/
29+
export function pkgDir(cwd: any): Promise<string>;
30+
31+
/**
32+
Synchronously find the root directory of a Node.js project or npm package.
33+
34+
@param cwd - Directory to start from. Default: `process.cwd()`.
35+
@returns The project root path or `undefined` if it couldn't be found.
36+
*/
37+
export function pkgDirSync(cwd: any): string;

‎index.js

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
'use strict';
2-
const path = require('path');
3-
const findUp = require('find-up');
1+
import {dirname} from 'node:path';
2+
import {findUp, findUpSync} from 'find-up';
43

5-
const pkgDir = async cwd => {
4+
export async function pkgDir(cwd) {
65
const filePath = await findUp('package.json', {cwd});
7-
return filePath && path.dirname(filePath);
8-
};
6+
return filePath && dirname(filePath);
7+
}
98

10-
module.exports = pkgDir;
11-
12-
module.exports.sync = cwd => {
13-
const filePath = findUp.sync('package.json', {cwd});
14-
return filePath && path.dirname(filePath);
15-
};
9+
export function pkgDirSync(cwd) {
10+
const filePath = findUpSync('package.json', {cwd});
11+
return filePath && dirname(filePath);
12+
}

‎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 = require('.');
2+
import {pkgDir, pkgDirSync} from './index.js';
33

4-
expectType<Promise<string | undefined>>(pkgDir('/Users/project/pkg-dir'));
5-
expectType<string | undefined>(pkgDir.sync('/Users/project/pkg-dir'));
4+
expectType<Promise<string>>(pkgDir('/Users/project/pkg-dir'));
5+
expectType<string>(pkgDirSync('/Users/project/pkg-dir'));

‎package.json

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
"email": "sindresorhus@gmail.com",
1010
"url": "https://sindresorhus.com"
1111
},
12+
"type": "module",
1213
"engines": {
13-
"node": ">=10"
14+
"node": ">=12"
1415
},
1516
"scripts": {
1617
"test": "xo && ava && tsd"
@@ -45,12 +46,12 @@
4546
"path"
4647
],
4748
"dependencies": {
48-
"find-up": "^5.0.0"
49+
"find-up": "^6.1.0"
4950
},
5051
"devDependencies": {
51-
"ava": "^2.4.0",
52-
"tempy": "^1.0.0",
53-
"tsd": "^0.13.1",
54-
"xo": "^0.33.1"
52+
"ava": "^3.15.0",
53+
"tempy": "^2.0.0",
54+
"tsd": "^0.17.0",
55+
"xo": "^0.44.0"
5556
}
5657
}

‎readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ $ npm install pkg-dir
2323

2424
```js
2525
// example.js
26-
const pkgDir = require('pkg-dir');
26+
import {pkgDir, pkgDirSync} from 'pkg-dir';
2727

2828
(async () => {
2929
const rootDir = await pkgDir(__dirname);
@@ -39,7 +39,7 @@ const pkgDir = require('pkg-dir');
3939

4040
Returns a `Promise` for either the project root path or `undefined` if it couldn't be found.
4141

42-
### pkgDir.sync(cwd?)
42+
### pkgDirSync(cwd?)
4343

4444
Returns the project root path or `undefined` if it couldn't be found.
4545

‎test.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import fs from 'fs';
2-
import path from 'path';
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import {fileURLToPath} from 'node:url';
34
import test from 'ava';
45
import tempy from 'tempy';
5-
import pkgDir from '.';
6+
import {pkgDir, pkgDirSync} from './index.js';
7+
8+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
69

710
// Create a disjoint directory, used for the not-found tests
811
test.beforeEach(t => {
@@ -19,6 +22,6 @@ test('async', async t => {
1922
});
2023

2124
test('sync', t => {
22-
t.is(pkgDir.sync(path.join(__dirname, 'fixture')), __dirname);
23-
t.is(pkgDir.sync(t.context.disjoint), undefined);
25+
t.is(pkgDirSync(path.join(__dirname, 'fixture')), __dirname);
26+
t.is(pkgDirSync(t.context.disjoint), undefined);
2427
});

0 commit comments

Comments
 (0)
Please sign in to comment.