Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sindresorhus/pkg-dir
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v5.0.0
Choose a base ref
...
head repository: sindresorhus/pkg-dir
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v6.0.0
Choose a head ref
  • 4 commits
  • 8 files changed
  • 3 contributors

Commits on Jan 1, 2021

  1. Copy the full SHA
    26f7493 View commit details

Commits on Oct 2, 2021

  1. Copy the full SHA
    198c9fe View commit details
  2. Cleanup

    sindresorhus committed Oct 2, 2021
    Copy the full SHA
    aeafb93 View commit details
  3. 6.0.0

    sindresorhus committed Oct 2, 2021
    Copy the full SHA
    a1006db View commit details
Showing with 124 additions and 87 deletions.
  1. +22 −0 .github/workflows/main.yml
  2. +0 −5 .travis.yml
  3. +55 −38 index.d.ts
  4. +7 −10 index.js
  5. +3 −3 index.test-d.ts
  6. +11 −8 package.json
  7. +16 −16 readme.md
  8. +10 −7 test.js
22 changes: 22 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

93 changes: 55 additions & 38 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,58 @@
declare const pkgDir: {
export interface Options {
/**
Synchronously find the root directory of a Node.js project or npm package.
The directory to start searching from.
@param cwd - Directory to start from. Default: `process.cwd()`.
@returns The project root path or `undefined` if it couldn't be found.
@default process.cwd()
*/
sync: (cwd?: string) => string | undefined;

/**
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.
@example
```
// /
// └── Users
// └── sindresorhus
// └── foo
// ├── package.json
// └── bar
// ├── baz
// └── example.js
// example.js
import pkgDir = require('pkg-dir');
(async () => {
const rootDir = await pkgDir(__dirname);
console.log(rootDir);
//=> '/Users/sindresorhus/foo'
})();
```
*/
(cwd?: string): Promise<string | undefined>;
};

export = pkgDir;
readonly cwd?: string;
}

/**
Find the root directory of a Node.js project or npm package.
@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 {packageDirectory} from 'pkg-dir';
console.log(await packageDirectory());
//=> '/Users/sindresorhus/foo'
```
*/
export function packageDirectory(options?: Options): Promise<string>;

/**
Synchronously find the root directory of a Node.js project or npm package.
@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 packageDirectorySync(options?: Options): string;
17 changes: 7 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
'use strict';
const path = require('path');
const findUp = require('find-up');
import path from 'node:path';
import {findUp, findUpSync} from 'find-up';

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

module.exports = pkgDir;

module.exports.sync = cwd => {
const filePath = findUp.sync('package.json', {cwd});
export function packageDirectorySync({cwd}) {
const filePath = findUpSync('package.json', {cwd});
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 = require('.');
import {packageDirectory, packageDirectorySync} from './index.js';

expectType<Promise<string | undefined>>(pkgDir('/Users/project/pkg-dir'));
expectType<string | undefined>(pkgDir.sync('/Users/project/pkg-dir'));
expectType<Promise<string>>(packageDirectory({cwd: '/Users/project/pkg-dir'}));
expectType<string>(packageDirectorySync({cwd: '/Users/project/pkg-dir'}));
19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"name": "pkg-dir",
"version": "5.0.0",
"version": "6.0.0",
"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": ">=10"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
@@ -39,18 +42,18 @@
"parents",
"folder",
"directory",
"dir",
"walk",
"walking",
"path"
],
"dependencies": {
"find-up": "^5.0.0"
"find-up": "^6.1.0"
},
"devDependencies": {
"ava": "^2.4.0",
"tempy": "^1.0.0",
"tsd": "^0.13.1",
"xo": "^0.33.1"
"ava": "^3.15.0",
"tempy": "^2.0.0",
"tsd": "^0.17.0",
"typescript": "^4.4.3",
"xo": "^0.44.0"
}
}
32 changes: 16 additions & 16 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# pkg-dir [![Build Status](https://travis-ci.com/sindresorhus/pkg-dir.svg?branch=master)](https://travis-ci.com/github/sindresorhus/pkg-dir)
# pkg-dir

> Find the root directory of a Node.js project or npm package
## Install

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

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

```js
// example.js
const 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'
```

## 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.

### pkgDir.sync(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

17 changes: 10 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import fs from 'fs';
import path from 'path';
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import test from 'ava';
import tempy from 'tempy';
import pkgDir from '.';
import {packageDirectory, packageDirectorySync} from './index.js';

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

// Create a disjoint directory, used for the not-found tests
test.beforeEach(t => {
@@ -14,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(pkgDir.sync(path.join(__dirname, 'fixture')), __dirname);
t.is(pkgDir.sync(t.context.disjoint), undefined);
t.is(packageDirectorySync({cwd: path.join(__dirname, 'fixture')}), __dirname);
t.is(packageDirectorySync({cwd: t.context.disjoint}), undefined);
});