Merge branch 'master' of https://github.com/indexzero/nconf into update-async
This commit is contained in:
commit
be0ee00b2a
11 changed files with 117 additions and 102 deletions
12
README.md
12
README.md
|
@ -1,4 +1,8 @@
|
|||
# nconf [![Build Status](https://secure.travis-ci.org/flatiron/nconf.png)](http://travis-ci.org/flatiron/nconf)
|
||||
# nconf
|
||||
|
||||
[![Version npm](https://img.shields.io/npm/v/nconf.svg?style=flat-square)](https://www.npmjs.com/package/nconf)[![npm Downloads](https://img.shields.io/npm/dm/nconf.svg?style=flat-square)](https://www.npmjs.com/package/nconf)[![Build Status](https://img.shields.io/travis/indexzero/nconf/master.svg?style=flat-square)](https://travis-ci.org/indexzero/nconf)[![Dependencies](https://img.shields.io/david/indexzero/nconf.svg?style=flat-square)](https://david-dm.org/indexzero/nconf)
|
||||
|
||||
[![NPM](https://nodei.co/npm/nconf.png?downloads=true&downloadRank=true)](https://nodei.co/npm/nconf/)
|
||||
|
||||
Hierarchical node.js configuration with files, environment variables, command-line arguments, and atomic object merging.
|
||||
|
||||
|
@ -61,7 +65,7 @@ The output will be:
|
|||
|
||||
Configuration management can get complicated very quickly for even trivial applications running in production. `nconf` addresses this problem by enabling you to setup a hierarchy for different sources of configuration with no defaults. **The order in which you attach these configuration sources determines their priority in the hierarchy.** Lets take a look at the options available to you
|
||||
|
||||
1. **nconf.argv(options)** Loads `process.argv` using optimist. If `options` is supplied it is passed along to optimist.
|
||||
1. **nconf.argv(options)** Loads `process.argv` using yargs. If `options` is supplied it is passed along to yargs.
|
||||
2. **nconf.env(options)** Loads `process.env` into the hierarchy.
|
||||
3. **nconf.file(options)** Loads the configuration data at options.file into the hierarchy.
|
||||
4. **nconf.defaults(options)** Loads the data in options.store into the hierarchy.
|
||||
|
@ -159,11 +163,11 @@ A simple in-memory storage engine that stores a nested JSON representation of th
|
|||
```
|
||||
|
||||
### Argv
|
||||
Responsible for loading the values parsed from `process.argv` by `optimist` into the configuration hierarchy. See the [optimist option docs](https://github.com/substack/node-optimist/#optionskey-opt) for more on the option format.
|
||||
Responsible for loading the values parsed from `process.argv` by `yargs` into the configuration hierarchy. See the [yargs option docs](https://github.com/bcoe/yargs#optionskey-opt) for more on the option format.
|
||||
|
||||
``` js
|
||||
//
|
||||
// Can optionally also be an object literal to pass to `optimist`.
|
||||
// Can optionally also be an object literal to pass to `yargs`.
|
||||
//
|
||||
nconf.argv({
|
||||
"x": {
|
||||
|
|
|
@ -348,8 +348,8 @@ other values for this instance.</p> </td> <td class="cod
|
|||
<span class="nx">Provider</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">__defineGetter__</span><span class="p">(</span><span class="s1">'overrides'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_argv</span> <span class="p">});</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">¶</a> </div> <h3>@argv {boolean}</h3>
|
||||
|
||||
<p>Gets or sets a property indicating if we should wrap calls to <code>.get</code>
|
||||
by checking <code>optimist.argv</code>. Can be a boolean or the pass-thru
|
||||
options for <code>optimist</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Provider</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">__defineSetter__</span><span class="p">(</span><span class="s1">'argv'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">val</span><span class="p">)</span> <span class="p">{</span> <span class="nx">updateSystem</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="s1">'argv'</span><span class="p">,</span> <span class="nx">val</span><span class="p">)</span> <span class="p">});</span>
|
||||
by checking <code>yargs.argv</code>. Can be a boolean or the pass-thru
|
||||
options for <code>yargs</code>.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Provider</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">__defineSetter__</span><span class="p">(</span><span class="s1">'argv'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">val</span><span class="p">)</span> <span class="p">{</span> <span class="nx">updateSystem</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">,</span> <span class="s1">'argv'</span><span class="p">,</span> <span class="nx">val</span><span class="p">)</span> <span class="p">});</span>
|
||||
<span class="nx">Provider</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">__defineGetter__</span><span class="p">(</span><span class="s1">'argv'</span><span class="p">,</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">_argv</span> <span class="p">});</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">¶</a> </div> <h3>@env {boolean}</h3>
|
||||
|
||||
<p>Gets or sets a property indicating if we should wrap calls to <code>.get</code>
|
||||
|
|
|
@ -62,10 +62,10 @@ into this instance.</p> </td> <td class="code">
|
|||
<span class="nx">argv</span><span class="p">;</span>
|
||||
|
||||
<span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="k">this</span><span class="p">.</span><span class="nx">argv</span> <span class="o">===</span> <span class="s1">'object'</span><span class="p">)</span> <span class="p">{</span>
|
||||
<span class="nx">argv</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'optimist'</span><span class="p">).</span><span class="nx">options</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">argv</span><span class="p">).</span><span class="nx">argv</span><span class="p">;</span>
|
||||
<span class="nx">argv</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'yargs'</span><span class="p">).</span><span class="nx">options</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">argv</span><span class="p">).</span><span class="nx">argv</span><span class="p">;</span>
|
||||
<span class="p">}</span>
|
||||
<span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">argv</span><span class="p">)</span> <span class="p">{</span>
|
||||
<span class="nx">argv</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'optimist'</span><span class="p">).</span><span class="nx">argv</span><span class="p">;</span>
|
||||
<span class="nx">argv</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'yargs'</span><span class="p">).</span><span class="nx">argv</span><span class="p">;</span>
|
||||
<span class="p">}</span>
|
||||
|
||||
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">argv</span><span class="p">)</span> <span class="p">{</span>
|
||||
|
|
|
@ -42,15 +42,15 @@ Argv.prototype.loadSync = function () {
|
|||
//
|
||||
Argv.prototype.loadArgv = function () {
|
||||
var self = this,
|
||||
optimist, argv;
|
||||
yargs, argv;
|
||||
|
||||
optimist = typeof this.options === 'object'
|
||||
? require('optimist')(process.argv.slice(2)).options(this.options)
|
||||
: require('optimist')(process.argv.slice(2));
|
||||
yargs = typeof this.options === 'object'
|
||||
? require('yargs')(process.argv.slice(2)).options(this.options)
|
||||
: require('yargs')(process.argv.slice(2));
|
||||
|
||||
if (typeof this.usage === 'string') { optimist.usage(this.usage) }
|
||||
if (typeof this.usage === 'string') { yargs.usage(this.usage) }
|
||||
|
||||
argv = optimist.argv
|
||||
argv = yargs.argv
|
||||
|
||||
if (!argv) {
|
||||
return;
|
||||
|
@ -61,8 +61,8 @@ Argv.prototype.loadArgv = function () {
|
|||
self.set(key, argv[key]);
|
||||
});
|
||||
|
||||
this.showHelp = optimist.showHelp
|
||||
this.help = optimist.help
|
||||
this.showHelp = yargs.showHelp
|
||||
this.help = yargs.help
|
||||
|
||||
this.readOnly = true;
|
||||
return this.store;
|
||||
|
|
|
@ -24,7 +24,7 @@ var Env = exports.Env = function (options) {
|
|||
this.whitelist = options.whitelist || [];
|
||||
this.separator = options.separator || '';
|
||||
|
||||
if (typeof options.match === 'function'
|
||||
if (({}).toString.call(options.match) === '[object RegExp]'
|
||||
&& typeof options !== 'string') {
|
||||
this.match = options.match;
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "nconf",
|
||||
"description": "Hierarchical node.js configuration with files, environment variables, command-line arguments, and atomic object merging.",
|
||||
"version": "0.7.1",
|
||||
"version": "0.7.2",
|
||||
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
@ -15,7 +15,7 @@
|
|||
"dependencies": {
|
||||
"async": "^1.4.0",
|
||||
"ini": "^1.3.0",
|
||||
"optimist": "~0.6.0"
|
||||
"yargs": "^3.19.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"vows": "0.8.x"
|
||||
|
|
|
@ -16,12 +16,23 @@ var fs = require('fs'),
|
|||
var completeTest = helpers.fixture('complete-test.json'),
|
||||
complete = helpers.fixture('complete.json');
|
||||
|
||||
// prime the process.env
|
||||
process.env['NCONF_foo'] = 'bar';
|
||||
process.env.FOO = 'bar';
|
||||
process.env.BAR = 'zalgo';
|
||||
process.env.NODE_ENV = 'debug';
|
||||
process.env.FOOBAR = 'should not load';
|
||||
|
||||
vows.describe('nconf/multiple-stores').addBatch({
|
||||
"When using the nconf with multiple providers": {
|
||||
topic: function () {
|
||||
var that = this;
|
||||
helpers.cp(complete, completeTest, function () {
|
||||
nconf.env();
|
||||
nconf.env({
|
||||
// separator: '__',
|
||||
match: /^NCONF_/,
|
||||
whitelist: ['NODE_ENV', 'FOO', 'BAR']
|
||||
});
|
||||
nconf.file({ file: completeTest });
|
||||
nconf.use('argv', { type: 'literal', store: data });
|
||||
that.callback();
|
||||
|
@ -34,7 +45,7 @@ vows.describe('nconf/multiple-stores').addBatch({
|
|||
},
|
||||
"env vars": {
|
||||
"are present": function () {
|
||||
Object.keys(process.env).forEach(function (key) {
|
||||
['NODE_ENV', 'FOO', 'BAR', 'NCONF_foo'].forEach(function (key) {
|
||||
assert.equal(nconf.get(key), process.env[key]);
|
||||
});
|
||||
}
|
||||
|
|
2
test/fixtures/scripts/nconf-argv.js
vendored
2
test/fixtures/scripts/nconf-argv.js
vendored
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* default-argv.js: Test fixture for using optimist defaults with nconf.
|
||||
* default-argv.js: Test fixture for using yargs defaults with nconf.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins and the Contributors.
|
||||
*
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* nconf-hierarchical-file-argv.js: Test fixture for using optimist defaults and a file store with nconf.
|
||||
* nconf-hierarchical-file-argv.js: Test fixture for using yargs defaults and a file store with nconf.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins and the Contributors.
|
||||
* (C) 2011, Sander Tolsma
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* nconf-hierarchical-load-save.js: Test fixture for using optimist, envvars and a file store with nconf.
|
||||
* nconf-hierarchical-load-save.js: Test fixture for using yargs, envvars and a file store with nconf.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins and the Contributors.
|
||||
*
|
||||
|
|
2
test/fixtures/scripts/provider-argv.js
vendored
2
test/fixtures/scripts/provider-argv.js
vendored
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* provider-argv.js: Test fixture for using optimist defaults with nconf.
|
||||
* provider-argv.js: Test fixture for using yargs defaults with nconf.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins and the Contributors.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue