pkg-conf
Advanced tools
Comparing version 1.0.1 to 1.1.0
27
index.js
'use strict'; | ||
var findUp = require('find-up'); | ||
var readPkg = require('read-pkg'); | ||
var objectAssign = require('object-assign'); | ||
var Symbol = require('symbol'); | ||
@@ -12,4 +13,11 @@ var fpSymbol = Symbol('package.json filepath'); | ||
module.exports = function (namespace, cwd) { | ||
return findUp('package.json', {cwd: cwd}) | ||
module.exports = function (namespace, opts) { | ||
// legacy | ||
if (typeof opts === 'string') { | ||
opts = {cwd: opts}; | ||
} | ||
opts = opts || {}; | ||
return findUp('package.json', {cwd: opts.cwd}) | ||
.then(function (fp) { | ||
@@ -25,3 +33,3 @@ if (!namespace) { | ||
return readPkg(fp, {normalize: false}).then(function (pkg) { | ||
return addFp(pkg[namespace] || {}, fp); | ||
return addFp(objectAssign({}, opts.defaults, pkg[namespace]), fp); | ||
}); | ||
@@ -31,3 +39,3 @@ }); | ||
module.exports.sync = function (namespace, cwd) { | ||
module.exports.sync = function (namespace, opts) { | ||
if (!namespace) { | ||
@@ -37,4 +45,11 @@ throw new TypeError('Expected a namespace'); | ||
var fp = findUp.sync('package.json', {cwd: cwd}); | ||
// legacy | ||
if (typeof opts === 'string') { | ||
opts = {cwd: opts}; | ||
} | ||
opts = opts || {}; | ||
var fp = findUp.sync('package.json', {cwd: opts.cwd}); | ||
if (!fp) { | ||
@@ -46,3 +61,3 @@ return addFp({}, fp); | ||
return addFp(pkg[namespace] || {}, fp); | ||
return addFp(objectAssign({}, opts.defaults, pkg[namespace]), fp); | ||
}; | ||
@@ -49,0 +64,0 @@ |
{ | ||
"name": "pkg-conf", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Get namespaced config from the closest package.json", | ||
@@ -40,2 +40,3 @@ "license": "MIT", | ||
"find-up": "^1.0.0", | ||
"object-assign": "^4.0.1", | ||
"read-pkg": "^1.0.0", | ||
@@ -42,0 +43,0 @@ "symbol": "^0.2.1" |
@@ -28,5 +28,5 @@ # pkg-conf [![Build Status](https://travis-ci.org/sindresorhus/pkg-conf.svg?branch=master)](https://travis-ci.org/sindresorhus/pkg-conf) | ||
```js | ||
var pkgConf = require('pkg-conf'); | ||
const pkgConf = require('pkg-conf'); | ||
pkgConf('unicorn').then(function (config) { | ||
pkgConf('unicorn').then(config => { | ||
console.log(config.rainbow); | ||
@@ -42,7 +42,7 @@ //=> true | ||
### pkgConf(namespace, [cwd]) | ||
### pkgConf(namespace, [options]) | ||
Returns a promise that resolves to the config. | ||
### pkgConf.sync(namespace, [cwd]) | ||
### pkgConf.sync(namespace, [options]) | ||
@@ -57,9 +57,17 @@ Returns the config. | ||
#### cwd | ||
#### options | ||
Type: `string` | ||
Default: `.` | ||
##### cwd | ||
Directory to start looking for a package.json file. | ||
Type: `string`<br> | ||
Default: `process.cwd()` | ||
Directory to start looking up for a package.json file. | ||
##### defaults | ||
Type: `object`<br> | ||
Default config. | ||
### pkgConf.filepath(config) | ||
@@ -74,2 +82,3 @@ | ||
- [read-pkg-up](https://github.com/sindresorhus/read-pkg-up) - Read the closest package.json file | ||
- [read-pkg](https://github.com/sindresorhus/read-pkg) - Read a package.json file | ||
@@ -76,0 +85,0 @@ - [find-up](https://github.com/sindresorhus/find-up) - Find a file by walking up parent directories |
4974
48
87
4
+ Addedobject-assign@^4.0.1
+ Addedobject-assign@4.1.1(transitive)