cosmiconfig
Advanced tools
Comparing version 0.5.0 to 1.0.0
101
index.js
@@ -5,5 +5,5 @@ 'use strict'; | ||
var oshomedir = require('os-homedir'); | ||
var Promise = require('pinkie-promise'); | ||
var Promise = require('bluebird'); | ||
var minimist = require('minimist'); | ||
var defaults = require('defaults'); | ||
var assign = require('object-assign'); | ||
var loadPackageProp = require('./lib/loadPackageProp'); | ||
@@ -14,7 +14,6 @@ var loadRc = require('./lib/loadRc'); | ||
var DONE = 'done'; | ||
var parsedCliArgs = minimist(process.argv); | ||
module.exports = function(moduleName, options) { | ||
options = defaults(options, { | ||
options = assign({ | ||
packageProp: moduleName, | ||
@@ -25,3 +24,4 @@ rc: '.' + moduleName + 'rc', | ||
rcStrictJson: false, | ||
}); | ||
stopDir: oshomedir(), | ||
}, options); | ||
@@ -32,74 +32,33 @@ if (options.argv && parsedCliArgs[options.argv]) { | ||
var stopDir = options.stopDir || oshomedir(); | ||
var splitSearchPath = splitPath(options.cwd); | ||
return new Promise(function(resolve, reject) { | ||
if (options.configPath) { | ||
return loadDefinedFile(options.configPath, options.format); | ||
} | ||
find(); | ||
function find() { | ||
var currentSearchPath = joinPath(splitSearchPath); | ||
function find() { | ||
if (options.configPath) { | ||
loadDefinedFile(options.configPath, options.format).then(function(result) { | ||
return finishWith(result); | ||
}).catch(reject); | ||
return; | ||
return Promise.resolve().then(function() { | ||
if (!options.packageProp) return; | ||
return loadPackageProp(currentSearchPath, options.packageProp); | ||
}).then(function(result) { | ||
if (result || !options.rc) return result; | ||
return loadRc(path.join(currentSearchPath, options.rc), { | ||
strictJson: options.rcStrictJson, | ||
}); | ||
}).then(function(result) { | ||
if (result || !options.js) return result; | ||
return loadJs(path.join(currentSearchPath, options.js)); | ||
}).then(function(result) { | ||
if (result) return result; | ||
// Notice the mutation of splitSearchPath | ||
if (currentSearchPath === options.stopDir || !splitSearchPath.pop()) { | ||
return null; | ||
} | ||
return find(); | ||
}); | ||
} | ||
var currentSearchPath = joinPath(splitSearchPath); | ||
var search = Promise.resolve(null); | ||
if (options.packageProp) { | ||
search = search.then(function(result) { | ||
if (result) return finishWith(result); | ||
return loadPackageProp(currentSearchPath, options.packageProp); | ||
}); | ||
} | ||
if (options.rc) { | ||
search = search.then(function(result) { | ||
if (result) return finishWith(result); | ||
return loadRc(path.join(currentSearchPath, options.rc), { | ||
strictJson: options.rcStrictJson, | ||
}); | ||
}); | ||
} | ||
if (options.js) { | ||
search = search.then(function(result) { | ||
if (result) return finishWith(result); | ||
return loadJs(path.join(currentSearchPath, options.js)); | ||
}); | ||
} | ||
search.then(function(result) { | ||
if (result) return finishWith(result); | ||
return moveUpOrGiveUp(currentSearchPath); | ||
}) | ||
.then(function(result) { | ||
if (result === DONE) return resolve(null); | ||
find(); | ||
}) | ||
.catch(function(err) { | ||
if (err === DONE) return; | ||
reject(err); | ||
}); | ||
function moveUpOrGiveUp(searchPath) { | ||
return new Promise(function(resolve) { | ||
// Notice the mutation of splitSearchPath | ||
if (searchPath === stopDir || !splitSearchPath.pop()) { | ||
resolve(DONE); | ||
} else { | ||
resolve(null); | ||
} | ||
}); | ||
} | ||
function finishWith(result) { | ||
resolve(result); | ||
throw DONE; | ||
} | ||
} | ||
}); | ||
return find(); | ||
}; | ||
@@ -106,0 +65,0 @@ |
'use strict'; | ||
var fs = require('graceful-fs'); | ||
var Promise = require('pinkie-promise'); | ||
var Promise = require('bluebird'); | ||
var yaml = require('js-yaml'); | ||
@@ -19,9 +19,11 @@ var parseJson = require('parse-json'); | ||
case 'json': | ||
return parseJson(content); | ||
return parseJson(content, filepath); | ||
case 'yaml': | ||
return yaml.safeLoad(content); | ||
return yaml.safeLoad(content, { | ||
filename: filepath, | ||
}); | ||
case 'js': | ||
return requireFromString(content); | ||
return requireFromString(content, filepath); | ||
default: | ||
return tryAllParsing(content); | ||
return tryAllParsing(content, filepath); | ||
} | ||
@@ -28,0 +30,0 @@ })(); |
'use strict'; | ||
var fs = require('graceful-fs'); | ||
var Promise = require('pinkie-promise'); | ||
var Promise = require('bluebird'); | ||
var requireFromString = require('require-from-string'); | ||
@@ -6,0 +6,0 @@ |
@@ -5,3 +5,3 @@ 'use strict'; | ||
var fs = require('graceful-fs'); | ||
var Promise = require('pinkie-promise'); | ||
var Promise = require('bluebird'); | ||
var parseJson = require('parse-json'); | ||
@@ -21,3 +21,3 @@ | ||
if (!content) return null; | ||
var parsedContent = parseJson(content); | ||
var parsedContent = parseJson(content, packagePath); | ||
var packagePropValue = parsedContent[packageProp]; | ||
@@ -24,0 +24,0 @@ if (!packagePropValue) return null; |
'use strict'; | ||
var fs = require('graceful-fs'); | ||
var Promise = require('pinkie-promise'); | ||
var Promise = require('bluebird'); | ||
var yaml = require('js-yaml'); | ||
@@ -21,4 +21,6 @@ var parseJson = require('parse-json'); | ||
var parsedConfig = (options.strictJson) | ||
? parseJson(content) | ||
: yaml.safeLoad(content); | ||
? parseJson(content, filepath) | ||
: yaml.safeLoad(content, { | ||
filename: filepath, | ||
}); | ||
@@ -25,0 +27,0 @@ return { |
{ | ||
"name": "cosmiconfig", | ||
"version": "0.5.0", | ||
"version": "1.0.0", | ||
"description": "Find and load configuration from a package.json property, rc file, or CommonJS module", | ||
"main": "index.js", | ||
"files": [ | ||
"index.js", | ||
"lib" | ||
], | ||
"scripts": { | ||
"lint": "eslint .", | ||
"test": "npm run lint && tape test" | ||
"pretest": "npm run lint", | ||
"test": "ava", | ||
"prepublish": "npm test" | ||
}, | ||
@@ -19,3 +25,6 @@ "repository": { | ||
], | ||
"author": "David Clark", | ||
"author": "David Clark <david.dave.clark@gmail.com>", | ||
"contributors": [ | ||
"Bogdan Chadkin <trysound@yandex.ru>" | ||
], | ||
"license": "MIT", | ||
@@ -27,16 +36,16 @@ "bugs": { | ||
"dependencies": { | ||
"defaults": "^1.0.3", | ||
"bluebird": "^3.0.5", | ||
"graceful-fs": "^4.1.2", | ||
"js-yaml": "^3.4.3", | ||
"minimist": "^1.2.0", | ||
"object-assign": "^4.0.1", | ||
"os-homedir": "^1.0.1", | ||
"parse-json": "^2.2.0", | ||
"pinkie-promise": "^2.0.0", | ||
"require-from-string": "^1.1.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.5.0", | ||
"eslint": "1.9.0", | ||
"sinon": "1.17.2", | ||
"tape": "4.2.2" | ||
"sinon": "1.17.2" | ||
} | ||
} |
@@ -1,21 +0,19 @@ | ||
# cosmiconfig [![Build Status](https://img.shields.io/travis/davidtheclark/cosmiconfig/master.svg?label=unix%20build)](https://travis-ci.org/davidtheclark/cosmiconfig) [![Build status](https://img.shields.io/appveyor/ci/davidtheclark/cosmiconfig/master.svg?label=windows%20build)](https://ci.appveyor.com/project/davidtheclark/cosmiconfig/branch/master) | ||
# cosmiconfig | ||
[![Build Status](https://img.shields.io/travis/davidtheclark/cosmiconfig/master.svg?label=unix%20build)](https://travis-ci.org/davidtheclark/cosmiconfig) [![Build status](https://img.shields.io/appveyor/ci/davidtheclark/cosmiconfig/master.svg?label=windows%20build)](https://ci.appveyor.com/project/davidtheclark/cosmiconfig/branch/master) | ||
**STATUS: Under active development, so do not use unless you are helping develop.** | ||
Find and load a configuration object from | ||
- a CLI `--config` argument, | ||
- a `package.json` property (anywhere down file tree), | ||
- a JSON or YAML "rc file" (anywhere down file tree), or | ||
- a `.config.js` CommonJS module (anywhere down file tree). | ||
- a `package.json` property (anywhere down the file tree) | ||
- a JSON or YAML "rc file" (anywhere down the file tree) | ||
- a `.config.js` CommonJS module (anywhere down the file tree) | ||
- a CLI `--config` argument | ||
For example, if your module's name is "soursocks," cosmiconfig will search out configuration in the following places: | ||
- a `soursocks` property in `package.json`; | ||
- a `.soursocksrc` file in JSON or YAML format; | ||
- a `soursocks.config.js` file exporting a JS object. | ||
- a `soursocks` property in `package.json` (anywhere down the file tree) | ||
- a `.soursocksrc` file in JSON or YAML format (anywhere down the file tree) | ||
- a `soursocks.config.js` file exporting a JS object (anywhere down the file tree) | ||
- a CLI `--config` argument | ||
cosmiconfig continues to search in these places all the way down the file tree until it finds acceptable configuration or hits the home directory. And it does all this asynchronously, so it shouldn't get in your way. | ||
cosmiconfig continues to search in these places all the way down the file tree until it finds acceptable configuration (or hits the home directory). And it does all this asynchronously, so it shouldn't get in your way. | ||
If cosmiconfig finds a `--config` CLI argument, it will load that file, trying to parse it as either JSON, YAML, or JS. | ||
Additionally, all of these search locations are configurable: you can customize filenames or turn off any location. | ||
@@ -31,5 +29,4 @@ | ||
## API | ||
## Usage | ||
```js | ||
@@ -48,6 +45,6 @@ var cosmiconfig = require('cosmiconfig'); | ||
The function `cosmiconfig()` searches for configuration objects and returns a Promise; | ||
and that Promise resolves with an object containing the information you're looking for. | ||
The function `cosmiconfig()` searches for a configuration object and returns a Promise, | ||
which resolves with an object containing the information you're looking for. | ||
So let's say `yourModuleName = 'goldengrahams'` — here's how cosmiconfig will work: | ||
So let's say `var yourModuleName = 'goldengrahams'` — here's how cosmiconfig will work: | ||
@@ -59,3 +56,3 @@ - Starting from `process.cwd()` (or some other directory defined by `options.cwd`), it looks for configuration objects in three places, in this order: | ||
- If none of those searches reveal a configuration object, it moves down one directory and tries again. So the search continues in `./`, `../`, `../../`, `../../../`, etc., checking those three locations in each directory. | ||
- It continues searching until it arrives at your user directory (or some other directory defined by `options.stopDir`). | ||
- It continues searching until it arrives at your home directory (or some other directory defined by `options.stopDir`). | ||
- If at any point a parseable configuration is found, the `cosmiconfig()` Promise resolves with its result object. | ||
@@ -65,5 +62,7 @@ - If no configuration object is found, the `cosmiconfig()` Promise resolves with `null`. | ||
All this can be overridden by passing a `configPath` option or a `--config` CLI argument to specify a file. | ||
All this searching can be short-circuited by passing `options.configPath` or a `--config` CLI argument to specify a file. | ||
cosmiconfig will read that file and try parsing it as JSON, YAML, or JS. | ||
## API | ||
### cosmiconfig(moduleName[, options]) | ||
@@ -80,3 +79,3 @@ | ||
You module name. This is used to create the filenames that cosmiconfig will look for. | ||
You module name. This is used to create the default filenames that cosmiconfig will look for. | ||
@@ -87,3 +86,3 @@ #### Options | ||
Type: `string` or `boolean` | ||
Type: `string` or `false` | ||
Default: `'[moduleName]'` | ||
@@ -97,3 +96,3 @@ | ||
Type: `string` or `boolean` | ||
Type: `string` or `false` | ||
Default: `'.[moduleName]rc'` | ||
@@ -107,3 +106,3 @@ | ||
Type: `string` or `boolean` | ||
Type: `string` or `false` | ||
Default: `'[moduleName].config.js'` | ||
@@ -117,6 +116,8 @@ | ||
Type: `string` or `boolean` | ||
Type: `string` or `false` | ||
Default: `'config'` | ||
Name of a `process.argv` argument to look for. The default causes cosmiconfig to look for `--config`. | ||
Name of a `process.argv` argument to look for, whose value should be the path to a configuration file. | ||
cosmiconfig will read the file and try to parse it as JSON, YAML, or JS. | ||
By default, cosmiconfig looks for `--config`. | ||
@@ -155,3 +156,3 @@ If `false`, cosmiconfig will not look for any `process.argv` arguments. | ||
Path which the search will stop. | ||
Directory where the search will stop. | ||
@@ -165,3 +166,9 @@ ## Differences from [rc](https://github.com/dominictarr/rc) | ||
- Stops at the first configuration found, instead of finding all that can be found down the filetree and merging them automatically. | ||
- Provides a few configuration options (e.g. different file name expectations). | ||
- Options. | ||
- Asynchronicity. | ||
## Contributing & Development | ||
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms. | ||
And please do participate! |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
164
14245
9
184
1
+ Addedbluebird@^3.0.5
+ Addedobject-assign@^4.0.1
+ Addedbluebird@3.7.2(transitive)
+ Addedobject-assign@4.1.1(transitive)
- Removeddefaults@^1.0.3
- Removedpinkie-promise@^2.0.0
- Removedclone@1.0.4(transitive)
- Removeddefaults@1.0.4(transitive)
- Removedpinkie@2.0.4(transitive)
- Removedpinkie-promise@2.0.1(transitive)