pkg-conf
Advanced tools
Comparing version 3.1.0 to 4.0.0
149
index.d.ts
@@ -1,92 +0,99 @@ | ||
declare namespace pkgConf { | ||
type Config = {[key: string]: unknown}; | ||
export type Config = Record<string, unknown>; | ||
interface Options<ConfigType extends Config> { | ||
/** | ||
Directory to start looking up for a `package.json` file. | ||
export interface Options<ConfigType extends Config> { | ||
/** | ||
The directory to start looking up for a `package.json` file. | ||
@default process.cwd() | ||
*/ | ||
cwd?: string; | ||
@default process.cwd() | ||
*/ | ||
readonly cwd?: string; | ||
/** | ||
Default config. | ||
/** | ||
The default config. | ||
@default {} | ||
*/ | ||
defaults?: ConfigType; | ||
@default {} | ||
*/ | ||
readonly defaults?: ConfigType; | ||
/** | ||
Skip `package.json` files that have the namespaced config explicitly set to `false`. | ||
/** | ||
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. | ||
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 | ||
@default false | ||
@example | ||
``` | ||
{ | ||
"name": "some-package", | ||
"version": "1.0.0", | ||
"unicorn": false | ||
} | ||
``` | ||
*/ | ||
skipOnFalse?: boolean; | ||
@example | ||
``` | ||
{ | ||
"name": "some-package", | ||
"version": "1.0.0", | ||
"unicorn": false | ||
} | ||
``` | ||
*/ | ||
readonly skipOnFalse?: boolean; | ||
} | ||
declare const pkgConf: { | ||
/** | ||
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. | ||
/** | ||
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. | ||
@param namespace - The `package.json` namespace you want. | ||
@returns A `Promise` for the config. | ||
@example | ||
``` | ||
// { | ||
// "name": "some-package", | ||
// "version": "1.0.0", | ||
// "unicorn": { | ||
// "rainbow": true | ||
// } | ||
// } | ||
@example | ||
``` | ||
// { | ||
// "name": "some-package", | ||
// "version": "1.0.0", | ||
// "unicorn": { | ||
// "rainbow": true | ||
// } | ||
// } | ||
import pkgConf = require('pkg-conf'); | ||
import {packageConfig} from 'pkg-conf'; | ||
(async () => { | ||
const config = await pkgConf('unicorn'); | ||
const config = await packageConfig('unicorn'); | ||
console.log(config.rainbow); | ||
//=> true | ||
})(); | ||
``` | ||
*/ | ||
<ConfigType extends pkgConf.Config = pkgConf.Config>( | ||
namespace: string, | ||
options?: pkgConf.Options<ConfigType> | ||
): Promise<ConfigType & pkgConf.Config>; | ||
console.log(config.rainbow); | ||
//=> true | ||
``` | ||
*/ | ||
export function packageConfig<ConfigType extends Config = Config>( | ||
namespace: string, | ||
options?: Options<ConfigType> | ||
): Promise<ConfigType & Config>; | ||
/** | ||
Same as `pkgConf()`, but runs synchronously. | ||
/** | ||
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 Returns the config. | ||
*/ | ||
sync<ConfigType extends pkgConf.Config = pkgConf.Config>( | ||
namespace: string, | ||
options?: pkgConf.Options<ConfigType> | ||
): ConfigType & pkgConf.Config; | ||
@param namespace - The `package.json` namespace you want. | ||
@returns Returns the 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. | ||
*/ | ||
filepath(config: pkgConf.Config): string | null; | ||
@example | ||
``` | ||
// { | ||
// "name": "some-package", | ||
// "version": "1.0.0", | ||
// "unicorn": { | ||
// "rainbow": true | ||
// } | ||
// } | ||
// TODO: Remove this for the next major release | ||
default: typeof pkgConf; | ||
}; | ||
import {packageConfigSync} from 'pkg-conf'; | ||
export = pkgConf; | ||
const config = packageConfigSync('unicorn'); | ||
console.log(config.rainbow); | ||
//=> true | ||
``` | ||
*/ | ||
export function packageConfigSync<ConfigType extends Config = Config>( | ||
namespace: string, | ||
options?: Options<ConfigType> | ||
): ConfigType & Config; | ||
/** | ||
@param config - The config returned from any of the above methods. | ||
@returns The file path to the `package.json` file or `undefined` if not found. | ||
*/ | ||
export function packageJsonPath(config: Config): string | undefined; |
67
index.js
@@ -1,38 +0,34 @@ | ||
'use strict'; | ||
const path = require('path'); | ||
const findUp = require('find-up'); | ||
const loadJsonFile = require('load-json-file'); | ||
import path from 'node:path'; | ||
import {findUp, findUpSync} from 'find-up'; | ||
import {loadJsonFile, loadJsonFileSync} from 'load-json-file'; | ||
const filepaths = new WeakMap(); | ||
const filepath = conf => filepaths.get(conf); | ||
const filePaths = new WeakMap(); | ||
const findNextCwd = pkgPath => path.resolve(path.dirname(pkgPath), '..'); | ||
const addFilePath = (object, filePath) => { | ||
filepaths.set(object, filePath); | ||
filePaths.set(object, filePath); | ||
return object; | ||
}; | ||
const pkgConf = (namespace, options = {}) => { | ||
export async function packageConfig(namespace, options = {}) { | ||
if (!namespace) { | ||
return Promise.reject(new TypeError('Expected a namespace')); | ||
throw new TypeError('Expected a namespace'); | ||
} | ||
return findUp('package.json', options.cwd ? {cwd: options.cwd} : {}) | ||
.then(filePath => { | ||
if (!filePath) { | ||
return addFilePath(Object.assign({}, options.defaults), filePath); | ||
} | ||
const filePath = await findUp('package.json', options.cwd ? {cwd: options.cwd} : {}); | ||
return loadJsonFile(filePath).then(package_ => { | ||
if (options.skipOnFalse && package_[namespace] === false) { | ||
const newOptions = Object.assign({}, options, {cwd: findNextCwd(filePath)}); | ||
return pkgConf(namespace, newOptions); | ||
} | ||
if (!filePath) { | ||
return addFilePath({...options.defaults}, filePath); | ||
} | ||
return addFilePath(Object.assign({}, options.defaults, package_[namespace]), filePath); | ||
}); | ||
}); | ||
}; | ||
const packageJson = await loadJsonFile(filePath); | ||
const sync = (namespace, options = {}) => { | ||
if (options.skipOnFalse && packageJson[namespace] === false) { | ||
return packageConfig(namespace, {...options, cwd: findNextCwd(filePath)}); | ||
} | ||
return addFilePath({...options.defaults, ...packageJson[namespace]}, filePath); | ||
} | ||
export function packageConfigSync(namespace, options = {}) { | ||
if (!namespace) { | ||
@@ -42,22 +38,19 @@ throw new TypeError('Expected a namespace'); | ||
const filePath = findUp.sync('package.json', options.cwd ? {cwd: options.cwd} : {}); | ||
const filePath = findUpSync('package.json', options.cwd ? {cwd: options.cwd} : {}); | ||
if (!filePath) { | ||
return addFilePath(Object.assign({}, options.defaults), filePath); | ||
return addFilePath({...options.defaults}, filePath); | ||
} | ||
const package_ = loadJsonFile.sync(filePath); | ||
const packageJson = loadJsonFileSync(filePath); | ||
if (options.skipOnFalse && package_[namespace] === false) { | ||
const newOptions = Object.assign({}, options, {cwd: findNextCwd(filePath)}); | ||
return sync(namespace, newOptions); | ||
if (options.skipOnFalse && packageJson[namespace] === false) { | ||
return packageConfigSync(namespace, {...options, cwd: findNextCwd(filePath)}); | ||
} | ||
return addFilePath(Object.assign({}, options.defaults, package_[namespace]), filePath); | ||
}; | ||
return addFilePath({...options.defaults, ...packageJson[namespace]}, filePath); | ||
} | ||
module.exports = pkgConf; | ||
// TODO: Remove this for the next major release | ||
module.exports.default = pkgConf; | ||
module.exports.filepath = filepath; | ||
module.exports.sync = sync; | ||
export function packageJsonPath(config) { | ||
return filePaths.get(config); | ||
} |
{ | ||
"name": "pkg-conf", | ||
"version": "3.1.0", | ||
"version": "4.0.0", | ||
"description": "Get namespaced config from the closest package.json", | ||
"license": "MIT", | ||
"repository": "sindresorhus/pkg-conf", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=6" | ||
"node": "^12.20.0 || ^14.13.1 || >=16.0.0" | ||
}, | ||
@@ -30,6 +33,4 @@ "scripts": { | ||
"load", | ||
"pkg", | ||
"package", | ||
"config", | ||
"conf", | ||
"configuration", | ||
@@ -41,9 +42,9 @@ "object", | ||
"dependencies": { | ||
"find-up": "^3.0.0", | ||
"load-json-file": "^5.2.0" | ||
"find-up": "^6.0.0", | ||
"load-json-file": "^7.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.2", | ||
"xo": "^0.24.0" | ||
"ava": "^3.15.0", | ||
"tsd": "^0.17.0", | ||
"xo": "^0.44.0" | ||
}, | ||
@@ -50,0 +51,0 @@ "fixture": { |
@@ -1,8 +0,7 @@ | ||
# pkg-conf [![Build Status](https://travis-ci.org/sindresorhus/pkg-conf.svg?branch=master)](https://travis-ci.org/sindresorhus/pkg-conf) | ||
# pkg-conf | ||
> 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 `.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. | ||
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://avajs.dev), [Babel](https://babeljs.io), [nyc](https://github.com/istanbuljs/nyc), etc. | ||
## Install | ||
@@ -14,3 +13,2 @@ | ||
## Usage | ||
@@ -29,13 +27,10 @@ | ||
```js | ||
const pkgConf = require('pkg-conf'); | ||
import {packageConfig} from 'pkg-conf'; | ||
(async () => { | ||
const config = await pkgConf('unicorn'); | ||
const config = await packageConfig('unicorn'); | ||
console.log(config.rainbow); | ||
//=> true | ||
})(); | ||
console.log(config.rainbow); | ||
//=> true | ||
``` | ||
## API | ||
@@ -45,7 +40,7 @@ | ||
### pkgConf(namespace, [options]) | ||
### packageConfig(namespace, options?) | ||
Returns a `Promise` for the config. | ||
### pkgConf.sync(namespace, [options]) | ||
### packageConfigSync(namespace, options?) | ||
@@ -62,20 +57,20 @@ Returns the config. | ||
Type: `Object` | ||
Type: `object` | ||
##### cwd | ||
Type: `string`<br> | ||
Type: `string`\ | ||
Default: `process.cwd()` | ||
Directory to start looking up for a package.json file. | ||
The directory to start looking up for a package.json file. | ||
##### defaults | ||
Type: `Object`<br> | ||
Type: `object` | ||
Default config. | ||
The default config. | ||
##### skipOnFalse | ||
Type: `boolean`<br> | ||
Type: `boolean`\ | ||
Default: `false` | ||
@@ -97,9 +92,8 @@ | ||
### pkgConf.filepath(config) | ||
### packageJsonPath(config) | ||
Pass in the `config` returned from any of the above methods. | ||
Pass in the config returned from any of the above methods. | ||
Returns the filepath to the package.json file or `null` when not found. | ||
Returns the file path to the package.json file or `undefined` if not found. | ||
## Related | ||
@@ -110,6 +104,1 @@ | ||
- [find-up](https://github.com/sindresorhus/find-up) - Find a file by walking up parent directories | ||
## License | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) |
Sorry, the diff of this file is not supported yet
Yes
8792
119
99
+ Addedfind-up@6.3.0(transitive)
+ Addedload-json-file@7.0.1(transitive)
+ Addedlocate-path@7.2.0(transitive)
+ Addedp-limit@4.0.0(transitive)
+ Addedp-locate@6.0.0(transitive)
+ Addedpath-exists@5.0.0(transitive)
+ Addedyocto-queue@1.0.0(transitive)
- Removederror-ex@1.3.2(transitive)
- Removedfind-up@3.0.0(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removedis-arrayish@0.2.1(transitive)
- Removedjson-parse-better-errors@1.0.2(transitive)
- Removedload-json-file@5.3.0(transitive)
- Removedlocate-path@3.0.0(transitive)
- Removedp-limit@2.3.0(transitive)
- Removedp-locate@3.0.0(transitive)
- Removedp-try@2.2.0(transitive)
- Removedparse-json@4.0.0(transitive)
- Removedpath-exists@3.0.0(transitive)
- Removedpify@4.0.1(transitive)
- Removedstrip-bom@3.0.0(transitive)
- Removedtype-fest@0.3.1(transitive)
Updatedfind-up@^6.0.0
Updatedload-json-file@^7.0.0