Skip to content

Commit e6fbe7f

Browse files
committedMar 28, 2021
Require Node.js 12 and move to ESM
1 parent 021b1ec commit e6fbe7f

File tree

8 files changed

+125
-141
lines changed

8 files changed

+125
-141
lines changed
 

‎.github/workflows/main.yml

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ jobs:
1212
node-version:
1313
- 14
1414
- 12
15-
- 10
16-
- 8
1715
steps:
1816
- uses: actions/checkout@v2
1917
- uses: actions/setup-node@v1

‎index.d.ts

+62-72
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,77 @@
11
import {Except} from 'type-fest';
2-
import readPkg = require('read-pkg');
2+
import {readPackageAsync, readPackageSync, Options as ReadPackageOptions, NormalizeOptions as ReadPackageNormalizeOptions, PackageJson, NormalizedPackageJson} from 'read-pkg';
33

4-
declare namespace readPkgUp {
5-
type Options = {
6-
/**
7-
Directory to start looking for a package.json file.
8-
9-
@default process.cwd()
10-
*/
11-
cwd?: string;
12-
} & Except<readPkg.Options, 'cwd'>;
4+
export type Options = {
5+
/**
6+
Directory to start looking for a package.json file.
137
14-
type NormalizeOptions = {
15-
/**
16-
Directory to start looking for a package.json file.
8+
@default process.cwd()
9+
*/
10+
cwd?: string;
11+
} & Except<ReadPackageOptions, 'cwd'>;
1712

18-
@default process.cwd()
19-
*/
20-
cwd?: string;
21-
} & Except<readPkg.NormalizeOptions, 'cwd'>;
13+
export type NormalizeOptions = {
14+
/**
15+
Directory to start looking for a package.json file.
2216
23-
type PackageJson = readPkg.PackageJson;
24-
type NormalizedPackageJson = readPkg.NormalizedPackageJson;
17+
@default process.cwd()
18+
*/
19+
cwd?: string;
20+
} & Except<ReadPackageNormalizeOptions, 'cwd'>;
2521

26-
interface ReadResult {
27-
packageJson: PackageJson;
28-
path: string;
29-
}
22+
export interface ReadResult {
23+
packageJson: PackageJson;
24+
path: string;
25+
}
3026

31-
interface NormalizedReadResult {
32-
packageJson: NormalizedPackageJson;
33-
path: string;
34-
}
27+
export interface NormalizedReadResult {
28+
packageJson: NormalizedPackageJson;
29+
path: string;
3530
}
3631

37-
declare const readPkgUp: {
38-
/**
39-
Read the closest `package.json` file.
32+
export {
33+
PackageJson,
34+
NormalizedPackageJson
35+
};
4036

41-
@example
42-
```
43-
import readPkgUp = require('read-pkg-up');
37+
/**
38+
Read the closest `package.json` file.
4439
45-
(async () => {
46-
console.log(await readPkgUp());
47-
// {
48-
// packageJson: {
49-
// name: 'awesome-package',
50-
// version: '1.0.0',
51-
// …
52-
// },
53-
// path: '/Users/sindresorhus/dev/awesome-package/package.json'
54-
// }
55-
})();
56-
```
57-
*/
58-
(options?: readPkgUp.NormalizeOptions): Promise<
59-
readPkgUp.NormalizedReadResult | undefined
60-
>;
61-
(options: readPkgUp.Options): Promise<readPkgUp.ReadResult | undefined>;
40+
@example
41+
```
42+
import {readPackageUpAsync} from 'read-pkg-up';
6243
63-
/**
64-
Synchronously read the closest `package.json` file.
44+
console.log(await readPackageUpAsync());
45+
// {
46+
// packageJson: {
47+
// name: 'awesome-package',
48+
// version: '1.0.0',
49+
// …
50+
// },
51+
// path: '/Users/sindresorhus/dev/awesome-package/package.json'
52+
// }
53+
```
54+
*/
55+
export function readPackageUpAsync(options?: NormalizeOptions): Promise<NormalizedReadResult | undefined>;
56+
export function readPackageUpAsync(options: Options): Promise<ReadResult | undefined>;
6557

66-
@example
67-
```
68-
import readPkgUp = require('read-pkg-up');
58+
/**
59+
Synchronously read the closest `package.json` file.
6960
70-
console.log(readPkgUp.sync());
71-
// {
72-
// packageJson: {
73-
// name: 'awesome-package',
74-
// version: '1.0.0',
75-
// …
76-
// },
77-
// path: '/Users/sindresorhus/dev/awesome-package/package.json'
78-
// }
79-
```
80-
*/
81-
sync(
82-
options?: readPkgUp.NormalizeOptions
83-
): readPkgUp.NormalizedReadResult | undefined;
84-
sync(options: readPkgUp.Options): readPkgUp.ReadResult | undefined;
85-
};
61+
@example
62+
```
63+
import {readPackageUpSync} from 'read-pkg-up';
8664
87-
export = readPkgUp;
65+
console.log(readPackageUpSync());
66+
// {
67+
// packageJson: {
68+
// name: 'awesome-package',
69+
// version: '1.0.0',
70+
// …
71+
// },
72+
// path: '/Users/sindresorhus/dev/awesome-package/package.json'
73+
// }
74+
```
75+
*/
76+
export function readPackageUpSync(options?: NormalizeOptions): NormalizedReadResult | undefined;
77+
export function readPackageUpSync(options: Options): ReadResult | undefined;

‎index.js

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
1-
'use strict';
2-
const path = require('path');
3-
const findUp = require('find-up');
4-
const readPkg = require('read-pkg');
1+
import path from 'path';
2+
import findUp from 'find-up';
3+
import {readPackageAsync, readPackageSync} from 'read-pkg';
54

6-
module.exports = async options => {
5+
export async function readPackageUpAsync(options) {
76
const filePath = await findUp('package.json', options);
8-
97
if (!filePath) {
108
return;
119
}
1210

1311
return {
14-
packageJson: await readPkg({...options, cwd: path.dirname(filePath)}),
12+
packageJson: await readPackageAsync({...options, cwd: path.dirname(filePath)}),
1513
path: filePath
1614
};
17-
};
15+
}
1816

19-
module.exports.sync = options => {
17+
export function readPackageUpSync(options) {
2018
const filePath = findUp.sync('package.json', options);
21-
2219
if (!filePath) {
2320
return;
2421
}
2522

2623
return {
27-
packageJson: readPkg.sync({...options, cwd: path.dirname(filePath)}),
24+
packageJson: readPackageSync({...options, cwd: path.dirname(filePath)}),
2825
path: filePath
2926
};
30-
};
27+
}

‎index.test-d.ts

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
import {expectType, expectError} from 'tsd';
2-
import readPkgUp = require('.');
2+
import {readPackageUpAsync, readPackageUpSync, ReadResult, NormalizedReadResult} from './index.js';
33

4-
expectType<Promise<readPkgUp.NormalizedReadResult | undefined>>(readPkgUp());
5-
expectType<Promise<readPkgUp.NormalizedReadResult | undefined>>(
6-
readPkgUp({cwd: '.'})
4+
expectType<Promise<NormalizedReadResult | undefined>>(readPackageUpAsync());
5+
expectType<Promise<NormalizedReadResult | undefined>>(
6+
readPackageUpAsync({cwd: '.'})
77
);
8-
expectType<Promise<readPkgUp.NormalizedReadResult | undefined>>(
9-
readPkgUp({normalize: true})
8+
expectType<Promise<NormalizedReadResult | undefined>>(
9+
readPackageUpAsync({normalize: true})
1010
);
11-
expectType<Promise<readPkgUp.NormalizedReadResult | undefined>>(
12-
readPkgUp({cwd: '.', normalize: true})
11+
expectType<Promise<NormalizedReadResult | undefined>>(
12+
readPackageUpAsync({cwd: '.', normalize: true})
1313
);
14-
expectType<Promise<readPkgUp.ReadResult | undefined>>(
15-
readPkgUp({normalize: false})
14+
expectType<Promise<ReadResult | undefined>>(
15+
readPackageUpAsync({normalize: false})
1616
);
17-
expectError<Promise<readPkgUp.NormalizedReadResult | undefined>>(
18-
readPkgUp({normalize: false})
17+
expectError<Promise<NormalizedReadResult | undefined>>(
18+
readPackageUpAsync({normalize: false})
1919
);
2020

21-
expectType<readPkgUp.NormalizedReadResult | undefined>(readPkgUp.sync());
22-
expectType<readPkgUp.NormalizedReadResult | undefined>(
23-
readPkgUp.sync({cwd: '.'})
21+
expectType<NormalizedReadResult | undefined>(readPackageUpSync());
22+
expectType<NormalizedReadResult | undefined>(
23+
readPackageUpSync({cwd: '.'})
2424
);
25-
expectType<readPkgUp.NormalizedReadResult | undefined>(
26-
readPkgUp.sync({normalize: true})
25+
expectType<NormalizedReadResult | undefined>(
26+
readPackageUpSync({normalize: true})
2727
);
28-
expectType<readPkgUp.NormalizedReadResult | undefined>(
29-
readPkgUp.sync({cwd: '.', normalize: true})
28+
expectType<NormalizedReadResult | undefined>(
29+
readPackageUpSync({cwd: '.', normalize: true})
3030
);
31-
expectType<readPkgUp.ReadResult | undefined>(
32-
readPkgUp.sync({normalize: false})
31+
expectType<ReadResult | undefined>(
32+
readPackageUpSync({normalize: false})
3333
);
34-
expectError<readPkgUp.NormalizedReadResult | undefined>(
35-
readPkgUp.sync({normalize: false})
34+
expectError<NormalizedReadResult | undefined>(
35+
readPackageUpSync({normalize: false})
3636
);

‎license

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

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

55
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:
66

‎package.json

+10-8
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
"author": {
99
"name": "Sindre Sorhus",
1010
"email": "sindresorhus@gmail.com",
11-
"url": "sindresorhus.com"
11+
"url": "https://sindresorhus.com"
1212
},
13+
"type": "module",
14+
"exports": "./index.js",
1315
"engines": {
14-
"node": ">=8"
16+
"node": ">=12"
1517
},
1618
"scripts": {
1719
"test": "xo && ava && tsd"
@@ -47,13 +49,13 @@
4749
"path"
4850
],
4951
"dependencies": {
50-
"find-up": "^4.1.0",
51-
"read-pkg": "^5.2.0",
52-
"type-fest": "^0.8.1"
52+
"find-up": "^5.0.0",
53+
"read-pkg": "^6.0.0",
54+
"type-fest": "^1.0.1"
5355
},
5456
"devDependencies": {
55-
"ava": "^2.4.0",
56-
"tsd": "^0.9.0",
57-
"xo": "^0.25.3"
57+
"ava": "^3.15.0",
58+
"tsd": "^0.14.0",
59+
"xo": "^0.38.2"
5860
}
5961
}

‎readme.md

+15-18
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
## Why
66

77
- [Finds the closest package.json](https://github.com/sindresorhus/find-up)
8-
- [Gracefully handles filesystem issues](https://github.com/isaacs/node-graceful-fs)
98
- [Throws more helpful JSON errors](https://github.com/sindresorhus/parse-json)
109
- [Normalizes the data](https://github.com/npm/normalize-package-data#what-normalization-currently-entails)
1110

@@ -18,30 +17,28 @@ $ npm install read-pkg-up
1817
## Usage
1918

2019
```js
21-
const readPkgUp = require('read-pkg-up');
22-
23-
(async () => {
24-
console.log(await readPkgUp());
25-
/*
26-
{
27-
packageJson: {
28-
name: 'awesome-package',
29-
version: '1.0.0',
30-
31-
},
32-
path: '/Users/sindresorhus/dev/awesome-package/package.json'
33-
}
34-
*/
35-
})();
20+
import {readPackageUpAsync} from 'read-pkg-up';
21+
22+
console.log(await readPackageUpAsync());
23+
/*
24+
{
25+
packageJson: {
26+
name: 'awesome-package',
27+
version: '1.0.0',
28+
29+
},
30+
path: '/Users/sindresorhus/dev/awesome-package/package.json'
31+
}
32+
*/
3633
```
3734

3835
## API
3936

40-
### readPkgUp(options?)
37+
### readPackageUpAsync(options?)
4138

4239
Returns a `Promise<object>` or `Promise<undefined>` if no `package.json` was found.
4340

44-
### readPkgUp.sync(options?)
41+
### readPackageUpSync(options?)
4542

4643
Returns the result object or `undefined` if no `package.json` was found.
4744

‎test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import path from 'path';
22
import test from 'ava';
3-
import readPackageUp from '.';
3+
import {readPackageUpAsync, readPackageUpSync} from './index.js';
44

55
const cwd = 'fixture';
66
const packagePath = path.resolve('.', 'package.json');
77

88
test('async', async t => {
9-
const result = await readPackageUp({cwd});
9+
const result = await readPackageUpAsync({cwd});
1010
t.is(result.packageJson.name, 'read-pkg-up');
1111
t.is(result.path, packagePath);
1212

13-
t.is(await readPackageUp({cwd: '/'}), undefined);
13+
t.is(await readPackageUpAsync({cwd: '/'}), undefined);
1414
});
1515

1616
test('sync', t => {
17-
const result = readPackageUp.sync({cwd});
17+
const result = readPackageUpSync({cwd});
1818
t.is(result.packageJson.name, 'read-pkg-up');
1919
t.is(result.path, packagePath);
2020

21-
t.is(readPackageUp.sync({cwd: '/'}), undefined);
21+
t.is(readPackageUpSync({cwd: '/'}), undefined);
2222
});

0 commit comments

Comments
 (0)
Please sign in to comment.