Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 4, 2021
1 parent aa84553 commit 1bca773
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 94 deletions.
3 changes: 0 additions & 3 deletions .github/funding.yml

This file was deleted.

5 changes: 2 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
- 8
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
99 changes: 55 additions & 44 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,59 @@
declare namespace pkgUp {
interface Options {
/**
Directory to start from.
@default process.cwd()
*/
readonly cwd?: string;
}
}

declare const pkgUp: {
export interface Options {
/**
Find the closest `package.json` file.
@returns The filepath, or `null` if it couldn't be found.
@example
```
// /
// └── Users
// └── sindresorhus
// └── foo
// ├── package.json
// └── bar
// ├── baz
// └── example.js
// example.js
import pkgUp = require('pkg-up');
(async () => {
console.log(await pkgUp());
//=> '/Users/sindresorhus/foo/package.json'
})();
```
*/
(options?: pkgUp.Options): Promise<string | null>;
The directory to start from.
/**
Synchronously find the closest `package.json` file.
@returns The filepath, or `null` if it couldn't be found.
@default process.cwd()
*/
sync(options?: pkgUp.Options): string | null;
};
readonly cwd?: string;
}

/**
Find the closest `package.json` file.
@returns The file path, or `undefined` if it could not be found.
@example
```
// /
// └── Users
// └── sindresorhus
// └── foo
// ├── package.json
// └── bar
// ├── baz
// └── example.js
// example.js
import {pkgUp} from 'pkg-up';
console.log(await pkgUp());
//=> '/Users/sindresorhus/foo/package.json'
```
*/
export function pkgUp(options?: Options): Promise<string | undefined>;

/**
Synchronously find the closest `package.json` file.
@returns The file path, or `undefined` if it could not be found.
@example
```
// /
// └── Users
// └── sindresorhus
// └── foo
// ├── package.json
// └── bar
// ├── baz
// └── example.js
// example.js
import {pkgUpSync} from 'pkg-up';
console.log(pkgUpSync());
//=> '/Users/sindresorhus/foo/package.json'
```
*/
export function pkgUpSync(options?: Options): string | undefined;

export = pkgUp;
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'use strict';
const findUp = require('find-up');
import {findUp, findUpSync} from 'find-up';

module.exports = async ({cwd} = {}) => findUp('package.json', {cwd});
module.exports.sync = ({cwd} = {}) => findUp.sync('package.json', {cwd});
export async function pkgUp({cwd} = {}) {
return findUp('package.json', {cwd});
}

export function pkgUpSync({cwd} = {}) {
return findUpSync('package.json', {cwd});
}
10 changes: 5 additions & 5 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {expectType} from 'tsd';
import pkgUp = require('.');
import {pkgUp, pkgUpSync} from './index.js';

expectType<Promise<string | null>>(pkgUp());
expectType<Promise<string | null>>(pkgUp({cwd: '.'}));
expectType<string | null>(pkgUp.sync());
expectType<string | null>(pkgUp.sync({cwd: '.'}));
expectType<Promise<string | undefined>>(pkgUp());
expectType<Promise<string | undefined>>(pkgUp({cwd: '.'}));
expectType<string | undefined>(pkgUpSync());
expectType<string | undefined>(pkgUpSync({cwd: '.'}));
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "Find the closest package.json file",
"license": "MIT",
"repository": "sindresorhus/pkg-up",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -36,17 +39,16 @@
"parents",
"folder",
"directory",
"dir",
"walk",
"walking",
"path"
],
"dependencies": {
"find-up": "^3.0.0"
"find-up": "^6.2.0"
},
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"ava": "^3.15.0",
"tsd": "^0.18.0",
"xo": "^0.46.4"
}
}
35 changes: 12 additions & 23 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

> Find the closest package.json file

## Install

```
$ npm install pkg-up
```sh
npm install pkg-up
```


## Usage

```
Expand All @@ -25,44 +23,35 @@ $ npm install pkg-up

```js
// example.js
const pkgUp = require('pkg-up');
import {pkgUp} from 'pkg-up';

(async () => {
console.log(await pkgUp());
//=> '/Users/sindresorhus/foo/package.json'
})();
console.log(await pkgUp());
//=> '/Users/sindresorhus/foo/package.json'
```


## API

### pkgUp([options])
### pkgUp(options?)

Returns a `Promise<string>` for the filepath, or `Promise<null>` if it couldn't be found.
Returns a `Promise<string>` for the file path, or `Promise<undefined>` if it could not be found.

### pkgUp.sync([options])
### pkgUpSync(options?)

Returns the filepath, or `null` if it couldn't be found.
Returns the file path, or `undefined` if it could not be found.

#### options

Type: `Object`
Type: `object`

#### cwd

Type: `string`<br>
Type: `string`\
Default: `process.cwd()`

Directory to start from.

The directory to start from.

## Related

- [read-pkg-up](https://github.com/sindresorhus/read-pkg-up) - Read the closest package.json file
- [pkg-dir](https://github.com/sindresorhus/pkg-dir) - Find the root directory of an npm package
- [find-up](https://github.com/sindresorhus/find-up) - Find a file by walking up parent directories


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
11 changes: 7 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import path from 'path';
import path from 'node:path';
import test from 'ava';
import pkgUp from '.';
import {fileURLToPath} from 'node:url';
import {pkgUp, pkgUpSync} from './index.js';

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

const cwd = path.join(__dirname, 'fixture');
const pkgPath = path.join(__dirname, 'package.json');
Expand All @@ -11,6 +14,6 @@ test('async', async t => {
});

test('sync', t => {
t.is(pkgUp.sync({cwd}), pkgPath);
t.is(path.dirname(pkgUp.sync()), __dirname);
t.is(pkgUpSync({cwd}), pkgPath);
t.is(path.dirname(pkgUpSync()), __dirname);
});

0 comments on commit 1bca773

Please sign in to comment.