Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 2, 2021
1 parent 198c9fe commit aeafb93
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- 12
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
47 changes: 34 additions & 13 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
export interface Options {
/**
The directory to start searching from.
@default process.cwd()
*/
readonly cwd?: string;
}

/**
Find the root directory of a Node.js project or npm package.
@param cwd - Directory to start from. Default: `process.cwd()`.
@returns The project root path or `undefined` if it couldn't be found.
@returns The project root path or `undefined` if it could not be found.
@example
```
Expand All @@ -16,22 +24,35 @@ Find the root directory of a Node.js project or npm package.
// └── example.js
// example.js
import pkgDir = require('pkg-dir');
(async () => {
const rootDir = await pkgDir(__dirname);
import {packageDirectory} from 'pkg-dir';
console.log(rootDir);
//=> '/Users/sindresorhus/foo'
})();
console.log(await packageDirectory());
//=> '/Users/sindresorhus/foo'
```
*/
export function pkgDir(cwd: any): Promise<string>;
export function packageDirectory(options?: Options): Promise<string>;

/**
Synchronously find the root directory of a Node.js project or npm package.
@param cwd - Directory to start from. Default: `process.cwd()`.
@returns The project root path or `undefined` if it couldn't be found.
@returns The project root path or `undefined` if it could not be found.
@example
```
// /
// └── Users
// └── sindresorhus
// └── foo
// ├── package.json
// └── bar
// ├── baz
// └── example.js
// example.js
import {packageDirectorySync} from 'pkg-dir';
console.log(packageDirectorySync());
//=> '/Users/sindresorhus/foo'
```
*/
export function pkgDirSync(cwd: any): string;
export function packageDirectorySync(options?: Options): string;
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {dirname} from 'node:path';
import path from 'node:path';
import {findUp, findUpSync} from 'find-up';

export async function pkgDir(cwd) {
export async function packageDirectory({cwd}) {
const filePath = await findUp('package.json', {cwd});
return filePath && dirname(filePath);
return filePath && path.dirname(filePath);
}

export function pkgDirSync(cwd) {
export function packageDirectorySync({cwd}) {
const filePath = findUpSync('package.json', {cwd});
return filePath && dirname(filePath);
return filePath && path.dirname(filePath);
}
6 changes: 3 additions & 3 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import {pkgDir, pkgDirSync} from './index.js';
import {packageDirectory, packageDirectorySync} from './index.js';

expectType<Promise<string>>(pkgDir('/Users/project/pkg-dir'));
expectType<string>(pkgDirSync('/Users/project/pkg-dir'));
expectType<Promise<string>>(packageDirectory({cwd: '/Users/project/pkg-dir'}));
expectType<string>(packageDirectorySync({cwd: '/Users/project/pkg-dir'}));
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
"description": "Find the root directory of a Node.js project or npm package",
"license": "MIT",
"repository": "sindresorhus/pkg-dir",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=12"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -40,7 +42,6 @@
"parents",
"folder",
"directory",
"dir",
"walk",
"walking",
"path"
Expand All @@ -52,6 +53,7 @@
"ava": "^3.15.0",
"tempy": "^2.0.0",
"tsd": "^0.17.0",
"typescript": "^4.4.3",
"xo": "^0.44.0"
}
}
30 changes: 15 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
## Install

```
$ npm install pkg-dir
```sh
npm install pkg-dir
```

## Usage
Expand All @@ -23,32 +23,32 @@ $ npm install pkg-dir

```js
// example.js
import {pkgDir, pkgDirSync} from 'pkg-dir';

(async () => {
const rootDir = await pkgDir(__dirname);
import {packageDirectory} from 'pkg-dir';

console.log(rootDir);
//=> '/Users/sindresorhus/foo'
})();
console.log(await packageDirectory());
//=> '/Users/sindresorhus/foo'
```

## API

### pkgDir(cwd?)
### packageDirectory(option?)

Returns a `Promise` for either the project root path or `undefined` if it could not be found.

### packageDirectorySync(options?)

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

### pkgDirSync(cwd?)
#### options

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

#### cwd
##### cwd

Type: `string`\
Default: `process.cwd()`

Directory to start from.
The directory to start searching from.

## Related

Expand Down
10 changes: 5 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path';
import {fileURLToPath} from 'node:url';
import test from 'ava';
import tempy from 'tempy';
import {pkgDir, pkgDirSync} from './index.js';
import {packageDirectory, packageDirectorySync} from './index.js';

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

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

test('async', async t => {
t.is(await pkgDir(path.join(__dirname, 'fixture')), __dirname);
t.is(await pkgDir(t.context.disjoint), undefined);
t.is(await packageDirectory({cwd: path.join(__dirname, 'fixture')}), __dirname);
t.is(await packageDirectory({cwd: t.context.disjoint}), undefined);
});

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

0 comments on commit aeafb93

Please sign in to comment.