Skip to content

Commit

Permalink
[breaking] ES2015ify and require Node.js 4
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 10, 2016
1 parent e1030ef commit d31b445
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* text=auto
*.js text eol=lf
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ language: node_js
node_js:
- '6'
- '4'
- '0.12'
- '0.10'
50 changes: 18 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,48 @@
'use strict';
var findUp = require('find-up');
var loadJsonFile = require('load-json-file');
var objectAssign = require('object-assign');
var Symbol = require('symbol');
var fpSymbol = Symbol('package.json filepath');
const findUp = require('find-up');
const loadJsonFile = require('load-json-file');

const fpSymbol = Symbol('package.json filepath');

function addFp(x, fp) {
x[fpSymbol] = fp;
return x;
}

module.exports = function (namespace, opts) {
// legacy
if (typeof opts === 'string') {
opts = {cwd: opts};
module.exports = (namespace, opts) => {
if (!namespace) {
return Promise.reject(new TypeError('Expected a namespace'));
}

opts = opts || {};

return findUp('package.json', {cwd: opts.cwd})
.then(function (fp) {
if (!namespace) {
throw new TypeError('Expected a namespace');
}

return findUp('package.json', opts.cwd ? {cwd: opts.cwd} : {})
.then(fp => {
if (!fp) {
return addFp(objectAssign({}, opts.defaults), fp);
return addFp(Object.assign({}, opts.defaults), fp);
}

return loadJsonFile(fp).then(function (pkg) {
return addFp(objectAssign({}, opts.defaults, pkg[namespace]), fp);
});
return loadJsonFile(fp).then(pkg =>
addFp(Object.assign({}, opts.defaults, pkg[namespace]), fp));
});
};

module.exports.sync = function (namespace, opts) {
module.exports.sync = (namespace, opts) => {
if (!namespace) {
throw new TypeError('Expected a namespace');
}

// legacy
if (typeof opts === 'string') {
opts = {cwd: opts};
}

opts = opts || {};

var fp = findUp.sync('package.json', {cwd: opts.cwd});
const fp = findUp.sync('package.json', opts.cwd ? {cwd: opts.cwd} : {});

if (!fp) {
return addFp(objectAssign({}, opts.defaults), fp);
return addFp(Object.assign({}, opts.defaults), fp);
}

var pkg = loadJsonFile.sync(fp);
const pkg = loadJsonFile.sync(fp);

return addFp(objectAssign({}, opts.defaults, pkg[namespace]), fp);
return addFp(Object.assign({}, opts.defaults, pkg[namespace]), fp);
};

module.exports.filepath = function (conf) {
return conf[fpSymbol];
};
module.exports.filepath = conf => conf[fpSymbol];
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -36,15 +36,16 @@
"namespaced"
],
"dependencies": {
"find-up": "^1.0.0",
"load-json-file": "^1.1.0",
"object-assign": "^4.0.1",
"symbol": "^0.2.1"
"find-up": "^2.0.0",
"load-json-file": "^2.0.0"
},
"devDependencies": {
"ava": "*",
"xo": "*"
},
"xo": {
"esnext": true
},
"fixture": {
"foo": true
}
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> Get namespaced config from the closest package.json
Having tool specific config in package.json reduces the amount of metafiles in your repo (there are usually a lot!) and makes the config obvious compared to hidden dotfiles like `.jshintrc`, which can end up causing confusion. [XO](https://github.com/sindresorhus/xo), for example, use the `xo` namespace in package.json, and [ESLint](http://eslint.org) uses `eslintConfig`. Many more tools supports this, like [AVA](https://ava.li), [Babel](https://babeljs.io), [nyc](https://github.com/bcoe/nyc), etc.
Having tool specific config in package.json reduces the amount of metafiles in your repo (there are usually a lot!) and makes the config obvious compared to hidden dotfiles like `.eslintrc`, which can end up causing confusion. [XO](https://github.com/sindresorhus/xo), for example, uses the `xo` namespace in package.json, and [ESLint](http://eslint.org) uses `eslintConfig`. Many more tools supports this, like [AVA](https://ava.li), [Babel](https://babeljs.io), [nyc](https://github.com/istanbuljs/nyc), etc.


## Install
Expand Down Expand Up @@ -40,7 +40,7 @@ It [walks up](https://github.com/sindresorhus/find-up) parent directories until

### pkgConf(namespace, [options])

Returns a promise that resolves to the config.
Returns a `Promise` for the config.

### pkgConf.sync(namespace, [options])

Expand Down Expand Up @@ -71,7 +71,7 @@ Default config.

Pass in the `config` returned from any of the above methods.

Returns the filepath to the package.json file or `null`.
Returns the filepath to the package.json file or `null` when not found.


## Related
Expand Down
18 changes: 9 additions & 9 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import path from 'path';
import test from 'ava';
import fn from './';
import m from './';

const cwd = path.join(__dirname, 'fixture');
const pkgPath = path.join(__dirname, 'package.json');

test('async', async t => {
const x = await fn('fixture', {cwd});
const x = await m('fixture', {cwd});
t.true(x.foo);
t.is(fn.filepath(x), pkgPath);
t.is(m.filepath(x), pkgPath);
});

test('async - non-existent namespace', async t => {
const x = await fn('noop', {cwd});
const x = await m('noop', {cwd});
t.is(typeof x, 'object');
});

test('sync', t => {
const x = fn.sync('fixture', {cwd});
const x = m.sync('fixture', {cwd});
t.true(x.foo);
t.is(fn.filepath(x), pkgPath);
t.is(m.filepath(x), pkgPath);
});

test('sync - non-existent namespace', t => {
const x = fn.sync('noop', {cwd});
const x = m.sync('noop', {cwd});
t.is(typeof x, 'object');
});

test('defaults option', t => {
const x = fn.sync('fixture', {cwd, defaults: {bar: false}});
const x = m.sync('fixture', {cwd, defaults: {bar: false}});
t.true(x.foo);
t.false(x.bar);

const x2 = fn.sync('noop', {defaults: {unicorn: true}});
const x2 = m.sync('noop', {defaults: {unicorn: true}});
t.true(x2.unicorn);
});

0 comments on commit d31b445

Please sign in to comment.