Skip to content

Commit

Permalink
Require Node.js 8
Browse files Browse the repository at this point in the history
Closes #9
  • Loading branch information
sindresorhus committed Mar 10, 2019
1 parent 03b601f commit 267b050
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 70 deletions.
33 changes: 16 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
'use strict';
const {promisify} = require('util');
const fs = require('fs');
const path = require('path');
const parseJson = require('parse-json');
const pify = require('pify');

const readFileAsync = pify(fs.readFile);
const readFileAsync = promisify(fs.readFile);

module.exports = options => {
options = Object.assign({
module.exports = async options => {
options = {
cwd: process.cwd(),
normalize: true
}, options);
normalize: true,
...options
};

const filePath = path.resolve(options.cwd, 'package.json');
const json = parseJson(await readFileAsync(filePath, 'utf8'));

return readFileAsync(filePath, 'utf8').then(file => {
const json = parseJson(file);

if (options.normalize) {
require('normalize-package-data')(json);
}
if (options.normalize) {
require('normalize-package-data')(json);
}

return json;
});
return json;
};

module.exports.sync = options => {
options = Object.assign({
options = {
cwd: process.cwd(),
normalize: true
}, options);
normalize: true,
...options
};

const filePath = path.resolve(options.cwd, 'package.json');
const json = parseJson(fs.readFileSync(filePath, 'utf8'));
Expand Down
84 changes: 44 additions & 40 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
{
"name": "read-pkg",
"version": "4.0.1",
"description": "Read a package.json file",
"license": "MIT",
"repository": "sindresorhus/read-pkg",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"json",
"read",
"parse",
"file",
"fs",
"graceful",
"load",
"pkg",
"package",
"normalize"
],
"dependencies": {
"normalize-package-data": "^2.3.2",
"parse-json": "^4.0.0",
"pify": "^3.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
}
"name": "read-pkg",
"version": "4.0.1",
"description": "Read a package.json file",
"license": "MIT",
"repository": "sindresorhus/read-pkg",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"json",
"read",
"parse",
"file",
"fs",
"graceful",
"load",
"pkg",
"package",
"normalize"
],
"dependencies": {
"normalize-package-data": "^2.3.2",
"parse-json": "^4.0.0"
},
"devDependencies": {
"ava": "^1.3.1",
"xo": "^0.24.0"
},
"xo": {
"ignores": [
"test/test.js"
]
}
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ const readPkg = require('read-pkg');

(async () => {
console.log(await readPkg());
//=> {name: 'read-pkg', ...}
//=> {name: 'read-pkg', }

console.log(await readPkg({cwd: 'some-other-directory'});
//=> {name: 'unicorn', ...}
//=> {name: 'unicorn', }
})();
```
Expand Down
22 changes: 11 additions & 11 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
'use strict';
import path from 'path';
import test from 'ava';
import m from '..';
import readPackage from '..';

process.chdir(__dirname);

const rootCwd = path.join(__dirname, '..');

test('async', async t => {
const x = await m();
t.is(x.name, 'unicorn');
t.truthy(x._id);
const package_ = await readPackage();
t.is(package_.name, 'unicorn');
t.truthy(package_._id);
});

test('async - cwd option', async t => {
const x = await m({cwd: rootCwd});
t.is(x.name, 'read-pkg');
const package_ = await readPackage({cwd: rootCwd});
t.is(package_.name, 'read-pkg');
});

test('sync', t => {
const x = m.sync();
t.is(x.name, 'unicorn');
t.truthy(x._id);
const package_ = readPackage.sync();
t.is(package_.name, 'unicorn');
t.truthy(package_._id);
});

test('sync - cwd option', t => {
const x = m.sync({cwd: rootCwd});
t.is(x.name, 'read-pkg');
const package_ = readPackage.sync({cwd: rootCwd});
t.is(package_.name, 'read-pkg');
});

0 comments on commit 267b050

Please sign in to comment.