diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 48143fc..3b8aa86 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/index.d.ts b/index.d.ts index 66c5555..47f2e61 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 ``` @@ -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; +export function packageDirectory(options?: Options): Promise; /** 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; diff --git a/index.js b/index.js index ed76ef7..787bf8c 100644 --- a/index.js +++ b/index.js @@ -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); } diff --git a/index.test-d.ts b/index.test-d.ts index 6c51206..7d4e968 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,5 @@ import {expectType} from 'tsd'; -import {pkgDir, pkgDirSync} from './index.js'; +import {packageDirectory, packageDirectorySync} from './index.js'; -expectType>(pkgDir('/Users/project/pkg-dir')); -expectType(pkgDirSync('/Users/project/pkg-dir')); +expectType>(packageDirectory({cwd: '/Users/project/pkg-dir'})); +expectType(packageDirectorySync({cwd: '/Users/project/pkg-dir'})); diff --git a/package.json b/package.json index c1cc1ca..16ee578 100644 --- a/package.json +++ b/package.json @@ -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": "sindresorhus@gmail.com", "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" @@ -40,7 +42,6 @@ "parents", "folder", "directory", - "dir", "walk", "walking", "path" @@ -52,6 +53,7 @@ "ava": "^3.15.0", "tempy": "^2.0.0", "tsd": "^0.17.0", + "typescript": "^4.4.3", "xo": "^0.44.0" } } diff --git a/readme.md b/readme.md index dd1aab2..8898c24 100644 --- a/readme.md +++ b/readme.md @@ -4,8 +4,8 @@ ## Install -``` -$ npm install pkg-dir +```sh +npm install pkg-dir ``` ## Usage @@ -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 diff --git a/test.js b/test.js index d447103..04db4b2 100644 --- a/test.js +++ b/test.js @@ -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)); @@ -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); });