+62
| export type Config = {[key: string]: unknown}; | ||
| export interface Options<ConfigType extends Config> { | ||
| /** | ||
| * Directory to start looking up for a `package.json` file. | ||
| * | ||
| * @default process.cwd() | ||
| */ | ||
| cwd?: string; | ||
| /** | ||
| * Default config. | ||
| * | ||
| * @default {} | ||
| */ | ||
| defaults?: ConfigType; | ||
| /** | ||
| * Skip `package.json` files that have the namespaced config explicitly set to `false`. | ||
| * | ||
| * Continues searching upwards until the next `package.json` file is reached. This can be useful when you need to support the ability for users to have nested `package.json` files, but only read from the root one, like in the case of [`electron-builder`](https://github.com/electron-userland/electron-builder/wiki/Options#AppMetadata) where you have one `package.json` file for the app and one top-level for development. | ||
| * | ||
| * @default false | ||
| * | ||
| * @example | ||
| * | ||
| * { | ||
| * "name": "some-package", | ||
| * "version": "1.0.0", | ||
| * "unicorn": false | ||
| * } | ||
| */ | ||
| skipOnFalse?: boolean; | ||
| } | ||
| /** | ||
| * It [walks up](https://github.com/sindresorhus/find-up) parent directories until a `package.json` can be found, reads it, and returns the user specified namespace or an empty object if not found. | ||
| * | ||
| * @param namespace - The `package.json` namespace you want. | ||
| * @returns A `Promise` for the config. | ||
| */ | ||
| export default function pkgConf<ConfigType extends Config = Config>( | ||
| namespace: string, | ||
| options?: Options<ConfigType> | ||
| ): Promise<ConfigType & Config>; | ||
| /** | ||
| * Same as `pkgConf()`, but runs synchronously. | ||
| * | ||
| * @param namespace - The `package.json` namespace you want. | ||
| * @returns Returns the config. | ||
| */ | ||
| export function sync<ConfigType extends Config = Config>( | ||
| namespace: string, | ||
| options?: Options<ConfigType> | ||
| ): ConfigType & Config; | ||
| /** | ||
| * @param config - The `config` returned from any of the above methods. | ||
| * @returns The filepath to the `package.json` file or `null` when not found. | ||
| */ | ||
| export function filepath(config: Config): string | null; |
+23
-26
@@ -10,8 +10,8 @@ 'use strict'; | ||
| const addFp = (obj, fp) => { | ||
| filepaths.set(obj, fp); | ||
| return obj; | ||
| const addFilePath = (object, filePath) => { | ||
| filepaths.set(object, filePath); | ||
| return object; | ||
| }; | ||
| const pkgConf = (namespace, opts) => { | ||
| const pkgConf = (namespace, options = {}) => { | ||
| if (!namespace) { | ||
@@ -21,17 +21,15 @@ return Promise.reject(new TypeError('Expected a namespace')); | ||
| opts = opts || {}; | ||
| return findUp('package.json', opts.cwd ? {cwd: opts.cwd} : {}) | ||
| .then(fp => { | ||
| if (!fp) { | ||
| return addFp(Object.assign({}, opts.defaults), fp); | ||
| return findUp('package.json', options.cwd ? {cwd: options.cwd} : {}) | ||
| .then(filePath => { | ||
| if (!filePath) { | ||
| return addFilePath(Object.assign({}, options.defaults), filePath); | ||
| } | ||
| return loadJsonFile(fp).then(pkg => { | ||
| if (opts.skipOnFalse && pkg[namespace] === false) { | ||
| const newOpts = Object.assign({}, opts, {cwd: findNextCwd(fp)}); | ||
| return pkgConf(namespace, newOpts); | ||
| return loadJsonFile(filePath).then(package_ => { | ||
| if (options.skipOnFalse && package_[namespace] === false) { | ||
| const newOptions = Object.assign({}, options, {cwd: findNextCwd(filePath)}); | ||
| return pkgConf(namespace, newOptions); | ||
| } | ||
| return addFp(Object.assign({}, opts.defaults, pkg[namespace]), fp); | ||
| return addFilePath(Object.assign({}, options.defaults, package_[namespace]), filePath); | ||
| }); | ||
@@ -41,3 +39,3 @@ }); | ||
| const sync = (namespace, opts) => { | ||
| const sync = (namespace, options = {}) => { | ||
| if (!namespace) { | ||
@@ -47,22 +45,21 @@ throw new TypeError('Expected a namespace'); | ||
| opts = opts || {}; | ||
| const filePath = findUp.sync('package.json', options.cwd ? {cwd: options.cwd} : {}); | ||
| const fp = findUp.sync('package.json', opts.cwd ? {cwd: opts.cwd} : {}); | ||
| if (!fp) { | ||
| return addFp(Object.assign({}, opts.defaults), fp); | ||
| if (!filePath) { | ||
| return addFilePath(Object.assign({}, options.defaults), filePath); | ||
| } | ||
| const pkg = loadJsonFile.sync(fp); | ||
| const package_ = loadJsonFile.sync(filePath); | ||
| if (opts.skipOnFalse && pkg[namespace] === false) { | ||
| const newOpts = Object.assign({}, opts, {cwd: findNextCwd(fp)}); | ||
| return sync(namespace, newOpts); | ||
| if (options.skipOnFalse && package_[namespace] === false) { | ||
| const newOptions = Object.assign({}, options, {cwd: findNextCwd(filePath)}); | ||
| return sync(namespace, newOptions); | ||
| } | ||
| return addFp(Object.assign({}, opts.defaults, pkg[namespace]), fp); | ||
| return addFilePath(Object.assign({}, options.defaults, package_[namespace]), filePath); | ||
| }; | ||
| module.exports = pkgConf; | ||
| module.exports.default = pkgConf; | ||
| module.exports.filepath = filepath; | ||
| module.exports.sync = sync; |
+49
-47
| { | ||
| "name": "pkg-conf", | ||
| "version": "2.1.0", | ||
| "description": "Get namespaced config from the closest package.json", | ||
| "license": "MIT", | ||
| "repository": "sindresorhus/pkg-conf", | ||
| "author": { | ||
| "name": "Sindre Sorhus", | ||
| "email": "sindresorhus@gmail.com", | ||
| "url": "sindresorhus.com" | ||
| }, | ||
| "engines": { | ||
| "node": ">=4" | ||
| }, | ||
| "scripts": { | ||
| "test": "xo && ava" | ||
| }, | ||
| "files": [ | ||
| "index.js" | ||
| ], | ||
| "keywords": [ | ||
| "json", | ||
| "read", | ||
| "parse", | ||
| "file", | ||
| "fs", | ||
| "graceful", | ||
| "load", | ||
| "pkg", | ||
| "package", | ||
| "config", | ||
| "conf", | ||
| "configuration", | ||
| "object", | ||
| "namespace", | ||
| "namespaced" | ||
| ], | ||
| "dependencies": { | ||
| "find-up": "^2.0.0", | ||
| "load-json-file": "^4.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "ava": "*", | ||
| "xo": "*" | ||
| }, | ||
| "fixture": { | ||
| "foo": true | ||
| } | ||
| "name": "pkg-conf", | ||
| "version": "3.0.0", | ||
| "description": "Get namespaced config from the closest package.json", | ||
| "license": "MIT", | ||
| "repository": "sindresorhus/pkg-conf", | ||
| "author": { | ||
| "name": "Sindre Sorhus", | ||
| "email": "sindresorhus@gmail.com", | ||
| "url": "sindresorhus.com" | ||
| }, | ||
| "engines": { | ||
| "node": ">=6" | ||
| }, | ||
| "scripts": { | ||
| "test": "xo && ava && tsd-check" | ||
| }, | ||
| "files": [ | ||
| "index.js", | ||
| "index.d.ts" | ||
| ], | ||
| "keywords": [ | ||
| "json", | ||
| "read", | ||
| "parse", | ||
| "file", | ||
| "fs", | ||
| "graceful", | ||
| "load", | ||
| "pkg", | ||
| "package", | ||
| "config", | ||
| "conf", | ||
| "configuration", | ||
| "object", | ||
| "namespace", | ||
| "namespaced" | ||
| ], | ||
| "dependencies": { | ||
| "find-up": "^3.0.0", | ||
| "load-json-file": "^5.2.0" | ||
| }, | ||
| "devDependencies": { | ||
| "ava": "^1.2.1", | ||
| "tsd-check": "^0.3.0", | ||
| "xo": "^0.24.0" | ||
| }, | ||
| "fixture": { | ||
| "foo": true | ||
| } | ||
| } |
+4
-1
@@ -5,3 +5,3 @@ # pkg-conf [](https://travis-ci.org/sindresorhus/pkg-conf) | ||
| 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. | ||
| 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/xojs/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. | ||
@@ -60,2 +60,4 @@ | ||
| Type: `Object` | ||
| ##### cwd | ||
@@ -84,2 +86,3 @@ | ||
| Example usage for the user: | ||
| ```json | ||
@@ -86,0 +89,0 @@ { |
8462
34.15%5
25%105
114.29%110
2.8%3
50%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated
Updated