Skip to content

Commit 0cd09a0

Browse files
committed
Remove the path arguments and drop BOM stripping
Since the name is always `package.json`, we can simplify the API by only accepting a `cwd` option instead.
1 parent c5f6837 commit 0cd09a0

File tree

5 files changed

+51
-63
lines changed

5 files changed

+51
-63
lines changed

index.js

+28-33
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,42 @@
11
'use strict';
2+
const fs = require('fs');
23
const path = require('path');
3-
const loadJsonFile = require('load-json-file');
4-
const pathType = require('path-type');
4+
const parseJson = require('parse-json');
5+
const promisify = require('util.promisify');
56

6-
module.exports = (fp, opts) => {
7-
if (typeof fp !== 'string') {
8-
opts = fp;
9-
fp = '.';
10-
}
7+
const readFileAsync = promisify(fs.readFile);
118

12-
opts = opts || {};
9+
module.exports = options => {
10+
options = Object.assign({
11+
cwd: process.cwd(),
12+
normalize: true
13+
}, options);
1314

14-
return pathType.dir(fp)
15-
.then(isDir => {
16-
if (isDir) {
17-
fp = path.join(fp, 'package.json');
18-
}
15+
const filePath = path.resolve(options.cwd, 'package.json');
1916

20-
return loadJsonFile(fp);
21-
})
22-
.then(x => {
23-
if (opts.normalize !== false) {
24-
require('normalize-package-data')(x);
25-
}
17+
return readFileAsync(filePath, 'utf8').then(file => {
18+
const json = parseJson(file);
2619

27-
return x;
28-
});
29-
};
20+
if (options.normalize) {
21+
require('normalize-package-data')(json);
22+
}
3023

31-
module.exports.sync = (fp, opts) => {
32-
if (typeof fp !== 'string') {
33-
opts = fp;
34-
fp = '.';
35-
}
24+
return json;
25+
});
26+
};
3627

37-
opts = opts || {};
38-
fp = pathType.dirSync(fp) ? path.join(fp, 'package.json') : fp;
28+
module.exports.sync = options => {
29+
options = Object.assign({
30+
cwd: process.cwd(),
31+
normalize: true
32+
}, options);
3933

40-
const x = loadJsonFile.sync(fp);
34+
const filePath = path.resolve(options.cwd, 'package.json');
35+
const json = parseJson(fs.readFileSync(filePath, 'utf8'));
4136

42-
if (opts.normalize !== false) {
43-
require('normalize-package-data')(x);
37+
if (options.normalize) {
38+
require('normalize-package-data')(json);
4439
}
4540

46-
return x;
41+
return json;
4742
};

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
"normalize"
3232
],
3333
"dependencies": {
34-
"load-json-file": "^5.0.0",
3534
"normalize-package-data": "^2.3.2",
36-
"path-type": "^3.0.0"
35+
"parse-json": "^4.0.0",
36+
"util.promisify": "^1.0.0"
3737
},
3838
"devDependencies": {
3939
"ava": "*",

readme.md

+12-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
## Why
77

88
- [Gracefully handles filesystem issues](https://github.com/isaacs/node-graceful-fs)
9-
- [Strips UTF-8 BOM](https://github.com/sindresorhus/strip-bom)
109
- [Throws more helpful JSON errors](https://github.com/sindresorhus/parse-json)
1110
- [Normalizes the data](https://github.com/npm/normalize-package-data#what-normalization-currently-entails)
1211

@@ -24,30 +23,35 @@ $ npm install read-pkg
2423
const readPkg = require('read-pkg');
2524

2625
(async () => {
27-
console.log( await readPkg());
26+
console.log(await readPkg());
2827
//=> {name: 'read-pkg', ...}
28+
29+
console.log(await readPkg({cwd: 'some-other-directory'});
30+
//=> {name: 'unicorn', ...}
2931
})();
3032
```
3133
3234
3335
## API
3436
35-
### readPkg([path], [options])
37+
### readPkg([options])
3638
3739
Returns a `Promise` for the parsed JSON.
3840
39-
### readPkg.sync([path], [options])
41+
### readPkg.sync([options])
4042
4143
Returns the parsed JSON.
4244
43-
#### path
45+
#### options
46+
47+
Type: `Object`
48+
49+
##### cwd
4450
4551
Type: `string`<br>
4652
Default: `process.cwd()`
4753
48-
Path to a `package.json` file or its directory.
49-
50-
#### options
54+
Current working directory.
5155
5256
##### normalize
5357
File renamed without changes.

test/test.js

+9-20
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,28 @@ import path from 'path';
33
import test from 'ava';
44
import m from '..';
55

6-
const pkg = path.join(__dirname, '..');
7-
const otherName = path.join(__dirname, 'pkg.json');
6+
process.chdir(__dirname);
7+
8+
const rootCwd = path.join(__dirname, '..');
89

910
test('async', async t => {
10-
const x = await m(otherName);
11+
const x = await m();
1112
t.is(x.name, 'unicorn');
1213
t.truthy(x._id);
1314
});
1415

15-
test('async - directory', async t => {
16-
const x = await m(pkg);
17-
t.is(x.name, 'read-pkg');
18-
t.truthy(x._id);
19-
});
20-
21-
test.serial('async - default filepath', async t => {
22-
const x = await m();
16+
test('async - cwd option', async t => {
17+
const x = await m({cwd: rootCwd});
2318
t.is(x.name, 'read-pkg');
2419
});
2520

2621
test('sync', t => {
27-
const x = m.sync(otherName);
22+
const x = m.sync();
2823
t.is(x.name, 'unicorn');
2924
t.truthy(x._id);
3025
});
3126

32-
test('sync - directory', t => {
33-
const x = m.sync(pkg);
34-
t.is(x.name, 'read-pkg');
35-
t.truthy(x._id);
36-
});
37-
38-
test.serial('sync - default filepath', t => {
39-
const x = m.sync();
27+
test('sync - cwd option', t => {
28+
const x = m.sync({cwd: rootCwd});
4029
t.is(x.name, 'read-pkg');
4130
});

0 commit comments

Comments
 (0)