Skip to content

Commit 1c15896

Browse files
BendingBendersindresorhus
authored andcommittedMay 12, 2019
Rename pkg property to package, return undefined instead of empty object, add TypeScript definition (#10)
1 parent 2c1d52f commit 1c15896

File tree

6 files changed

+149
-29
lines changed

6 files changed

+149
-29
lines changed
 

‎index.d.ts

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import {Omit} from 'type-fest';
2+
import readPkg = require('read-pkg');
3+
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+
} & Omit<readPkg.Options, 'cwd'>;
13+
14+
type NormalizeOptions = {
15+
/**
16+
Directory to start looking for a package.json file.
17+
18+
@default process.cwd()
19+
*/
20+
cwd?: string;
21+
} & Omit<readPkg.NormalizeOptions, 'cwd'>;
22+
23+
type PackageJson = readPkg.PackageJson;
24+
type NormalizedPackageJson = readPkg.NormalizedPackageJson;
25+
26+
interface ReadResult {
27+
package: PackageJson;
28+
path: string;
29+
}
30+
31+
interface NormalizedReadResult {
32+
package: NormalizedPackageJson;
33+
path: string;
34+
}
35+
}
36+
37+
declare const readPkgUp: {
38+
/**
39+
Read the closest `package.json` file.
40+
41+
@example
42+
```
43+
import readPkgUp = require('read-pkg-up');
44+
45+
(async () => {
46+
console.log(await readPkgUp());
47+
// {
48+
// package: {
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>;
62+
63+
/**
64+
Synchronously read the closest `package.json` file.
65+
66+
@example
67+
```
68+
import readPkgUp = require('read-pkg-up');
69+
70+
console.log(readPkgUp.sync());
71+
// {
72+
// package: {
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+
};
86+
87+
export = readPkgUp;

‎index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ module.exports = async options => {
77
const filePath = await findUp('package.json', options);
88

99
if (!filePath) {
10-
return {};
10+
return;
1111
}
1212

1313
return {
14-
pkg: await readPkg({...options, cwd: path.dirname(filePath)}),
14+
package: await readPkg({...options, cwd: path.dirname(filePath)}),
1515
path: filePath
1616
};
1717
};
@@ -20,11 +20,11 @@ module.exports.sync = options => {
2020
const filePath = findUp.sync('package.json', options);
2121

2222
if (!filePath) {
23-
return {};
23+
return;
2424
}
2525

2626
return {
27-
pkg: readPkg.sync({...options, cwd: path.dirname(filePath)}),
27+
package: readPkg.sync({...options, cwd: path.dirname(filePath)}),
2828
path: filePath
2929
};
3030
};

‎index.test-d.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {expectType, expectError} from 'tsd';
2+
import readPkgUp = require('.');
3+
4+
expectType<Promise<readPkgUp.NormalizedReadResult | undefined>>(readPkgUp());
5+
expectType<Promise<readPkgUp.NormalizedReadResult | undefined>>(
6+
readPkgUp({cwd: '.'})
7+
);
8+
expectType<Promise<readPkgUp.NormalizedReadResult | undefined>>(
9+
readPkgUp({normalize: true})
10+
);
11+
expectType<Promise<readPkgUp.NormalizedReadResult | undefined>>(
12+
readPkgUp({cwd: '.', normalize: true})
13+
);
14+
expectType<Promise<readPkgUp.ReadResult | undefined>>(
15+
readPkgUp({normalize: false})
16+
);
17+
expectError<Promise<readPkgUp.NormalizedReadResult | undefined>>(
18+
readPkgUp({normalize: false})
19+
);
20+
21+
expectType<readPkgUp.NormalizedReadResult | undefined>(readPkgUp.sync());
22+
expectType<readPkgUp.NormalizedReadResult | undefined>(
23+
readPkgUp.sync({cwd: '.'})
24+
);
25+
expectType<readPkgUp.NormalizedReadResult | undefined>(
26+
readPkgUp.sync({normalize: true})
27+
);
28+
expectType<readPkgUp.NormalizedReadResult | undefined>(
29+
readPkgUp.sync({cwd: '.', normalize: true})
30+
);
31+
expectType<readPkgUp.ReadResult | undefined>(
32+
readPkgUp.sync({normalize: false})
33+
);
34+
expectError<readPkgUp.NormalizedReadResult | undefined>(
35+
readPkgUp.sync({normalize: false})
36+
);

‎package.json

+8-7
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
"node": ">=8"
1414
},
1515
"scripts": {
16-
"test": "xo && ava"
16+
"test": "xo && ava && tsd"
1717
},
1818
"files": [
19-
"index.js"
19+
"index.js",
20+
"index.d.ts"
2021
],
2122
"keywords": [
2223
"json",
@@ -26,7 +27,6 @@
2627
"fs",
2728
"graceful",
2829
"load",
29-
"pkg",
3030
"package",
3131
"find",
3232
"up",
@@ -41,17 +41,18 @@
4141
"parents",
4242
"folder",
4343
"directory",
44-
"dir",
4544
"walk",
4645
"walking",
4746
"path"
4847
],
4948
"dependencies": {
50-
"find-up": "^3.0.0",
51-
"read-pkg": "^5.0.0"
49+
"find-up": "^4.0.0",
50+
"read-pkg": "^5.1.1",
51+
"type-fest": "^0.5.0"
5252
},
5353
"devDependencies": {
54-
"ava": "^1.3.1",
54+
"ava": "^1.4.1",
55+
"tsd": "^0.7.3",
5556
"xo": "^0.24.0"
5657
}
5758
}

‎readme.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const readPkgUp = require('read-pkg-up');
2828
console.log(await readPkgUp());
2929
/*
3030
{
31-
pkg: {
31+
package: {
3232
name: 'awesome-package',
3333
version: '1.0.0',
3434
@@ -44,15 +44,15 @@ const readPkgUp = require('read-pkg-up');
4444

4545
### readPkgUp([options])
4646

47-
Returns a `Promise` for the result object.
47+
Returns a `Promise<object>` or `Promise<undefined>` if no `package.json` was found.
4848

4949
### readPkgUp.sync([options])
5050

51-
Returns the result object.
51+
Returns the result object or `undefined` if no `package.json` was found.
5252

5353
#### options
5454

55-
Type: `Object`
55+
Type: `object`
5656

5757
##### cwd
5858

‎test.js

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
import path from 'path';
22
import test from 'ava';
3-
import readPkgUp from '.';
3+
import readPackageUp from '.';
44

55
const cwd = 'fixture';
6-
const pkgPath = path.resolve('.', 'package.json');
6+
const packagePath = path.resolve('.', 'package.json');
77

88
test('async', async t => {
9-
const result = await readPkgUp({cwd});
10-
t.is(result.pkg.name, 'read-pkg-up');
11-
t.is(result.path, pkgPath);
9+
const result = await readPackageUp({cwd});
10+
t.is(result.package.name, 'read-pkg-up');
11+
t.is(result.path, packagePath);
1212

13-
const result2 = await readPkgUp({cwd: '/'});
14-
t.is(result2.pkg, undefined);
15-
t.is(result2.path, undefined);
13+
t.is(await readPackageUp({cwd: '/'}), undefined);
1614
});
1715

1816
test('sync', t => {
19-
const result = readPkgUp.sync({cwd});
20-
t.is(result.pkg.name, 'read-pkg-up');
21-
t.is(result.path, pkgPath);
17+
const result = readPackageUp.sync({cwd});
18+
t.is(result.package.name, 'read-pkg-up');
19+
t.is(result.path, packagePath);
2220

23-
const result2 = readPkgUp.sync({cwd: '/'});
24-
t.is(result2.pkg, undefined);
25-
t.is(result2.path, undefined);
21+
t.is(readPackageUp.sync({cwd: '/'}), undefined);
2622
});

0 commit comments

Comments
 (0)