-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[breaking] ES2015ify and require Node.js 4
- Loading branch information
1 parent
e1030ef
commit d31b445
Showing
6 changed files
with
37 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
* text=auto | ||
*.js text eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,3 @@ language: node_js | |
node_js: | ||
- '6' | ||
- '4' | ||
- '0.12' | ||
- '0.10' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |