diff --git a/README.md b/README.md index 47f2c52..fcc7776 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,8 @@ -# nconf [![Build Status](https://travis-ci.org/indexzero/nconf.svg)](https://travis-ci.org/indexzero/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": { diff --git a/docs/nconf/provider.html b/docs/nconf/provider.html index 66c6718..b349626 100644 --- a/docs/nconf/provider.html +++ b/docs/nconf/provider.html @@ -13,7 +13,7 @@
Constructor function for the Provider object responsible
for exposing the pluggable storage features of nconf
.
var Provider = exports.Provider = function (options) {
- var self = this;
+ var self = this;
Setup default options for working with stores
,
overrides
, process.env
and process.argv
.
options = options || {};
this._overrides = options.overrides || null;
@@ -24,7 +24,7 @@ for exposing the pluggable storage features of nconf
.
Add the default system
store for working with
overrides
, process.env
, process.argv
and
a simple in-memory objects.
this.add('system', options);
-
+
if (options.type) {
this.add(options.type, options);
}
@@ -42,8 +42,8 @@ a simple in-memory objects.
Adds (or replaces) a new store with the specified name
-and options
. If options.type
is not set, then name
+
Adds (or replaces) a new store with the specified name
+and options
. If options.type
is not set, then name
will be used instead:
provider.use('file'); @@ -54,7 +54,7 @@ will be used instead:
else if (this._reserved.indexOf(name) !== -1) { throw new Error('Cannot use reserved name: ' + name); } - + options = options || {}; var type = options.type || name; @@ -63,18 +63,18 @@ will be used instead: return options[key] === store[key]; }); } - + var store = this[name], update = store && !sameOptions(store); - + if (!store || update) { if (update) { this.remove(name); } - + this.add(name, options); } - + return this; };name
will be used instead:
if (this._reserved.indexOf(name) !== -1) {
throw new Error('Cannot use reserved name: ' + name);
}
-
+
options = options || {};
var type = options.type || name;
-
+
if (Object.keys(stores).indexOf(common.capitalize(type)) === -1) {
throw new Error('Cannot add store with unknown type: ' + type);
}
-
+
this[name] = this.create(type, options);
this._stores.push(name);
-
+
if (this[name].loadSync) {
this[name].loadSync();
}
@@ -117,7 +117,7 @@ this was used in the call to .add()
. .add()
. Creates a store of the specified type
using the
+
Creates a store of the specified type
using the
specified options
.
Provider.prototype.create = function (type, options) {
return new stores[common.capitalize(type.toLowerCase())](options);
};
options
. if (!callback) {
return this._execute('get', 1, key, callback);
}
-
Otherwise the asynchronous, hierarchical get
is
+
Otherwise the asynchronous, hierarchical get
is
slightly more complicated because we do not need to traverse
the entire set of stores, but up until there is a defined value.
var current = 0,
self = this,
response;
-
+
async.whilst(function () {
return typeof response === 'undefined' && current < self._stores.length;
}, function (next) {
var store = self[self._stores[current]];
current++;
-
+
if (store.get.length >= 2) {
return store.get(key, function (err, value) {
if (err) {
return next(err);
}
-
+
response = value;
next();
});
}
-
+
response = store.get(key);
next();
}, function (err) {
@@ -182,7 +182,7 @@ the entire set of stores, but up until there is a defined value.
@callback {function} Optional Continuation to respond to when complete.
Clears all keys associated with this instance.
Provider.prototype.reset = function (callback) {
- return this._execute('reset', 0, callback);
+ return this._execute('reset', 0, callback);
};
Responds with an Object representing all keys associated in this instance.
Provider.prototype.load = function (callback) {
var self = this;
-
+
function loadStoreSync(name) {
var store = self[name];
-
+
if (!store.loadSync) {
throw new Error('nconf store ' + store.type + ' has no loadSync() method');
}
-
+
return store.loadSync();
}
-
+
function loadStore(name, next) {
var store = self[name];
-
+
if (!store.load && !store.loadSync) {
return next(new Error('nconf store ' + store.type + ' has no load() method'));
}
-
+
return store.loadSync
? next(null, store.loadSync())
: store.load(next);
}
-
If we don't have a callback and the current +
If we don't have a callback and the current store is capable of loading synchronously then do so.
if (!callback) {
return common.merge(this._stores.map(loadStoreSync));
}
-
+
async.map(this._stores, loadStore, function (err, objs) {
return err ? callback(err) : callback(null, common.merge(objs));
});
@@ -273,39 +273,39 @@ instance and then adds all key-value pairs in value
.
callback = value;
value = null;
}
-
+
var self = this;
-
+
function saveStoreSync(name) {
var store = self[name];
-
+
if (!store.saveSync) {
throw new Error('nconf store ' + store.type + ' has no saveSync() method');
}
-
+
return store.saveSync();
}
-
+
function saveStore(name, next) {
var store = self[name];
-
+
if (!store.save && !store.saveSync) {
return next(new Error('nconf store ' + store.type + ' has no save() method'));
}
-
+
return store.saveSync
? next(null, store.saveSync())
: store.save(next);
}
-
If we don't have a callback and the current +
If we don't have a callback and the current store is capable of saving synchronously then do so.
if (!callback) {
return common.merge(this._stores.map(saveStoreSync));
}
-
+
async.map(this._stores, saveStore, function (err, objs) {
return err ? callback(err) : callback();
- });
+ });
};
this.store
.Gets or sets a property representing overrides which supercede all +
Gets or sets a property representing overrides which supercede all other values for this instance.
Provider.prototype.__defineSetter__('overrides', function (val) { updateSystem.call(this, 'overrides', val) });
Provider.prototype.__defineGetter__('overrides', function () { return this._argv });
Gets or sets a property indicating if we should wrap calls to .get
-by checking optimist.argv
. Can be a boolean or the pass-thru
-options for optimist
.
Provider.prototype.__defineSetter__('argv', function (val) { updateSystem.call(this, 'argv', val) });
+Gets or sets a property indicating if we should wrap calls to .get
+by checking yargs.argv
. Can be a boolean or the pass-thru
+options for yargs
.
Provider.prototype.__defineSetter__('argv', function (val) { updateSystem.call(this, 'argv', val) });
Provider.prototype.__defineGetter__('argv', function () { return this._argv });
Gets or sets a property indicating if we should wrap calls to .get
-by checking process.env
. Can be a boolean or an Array of
+
Gets or sets a property indicating if we should wrap calls to .get
+by checking process.env
. Can be a boolean or an Array of
environment variables to extract.
Provider.prototype.__defineSetter__('env', function (val) { updateSystem.call(this, 'env', val) });
Provider.prototype.__defineGetter__('env', function () { return this._env });
Throw the err
if a callback is not supplied
function onError(err, callback) {
if (callback) {
return callback(err);
}
-
+
throw err;
}
Helper function for working with the
default system
store for providers.
function updateSystem(prop, value) {
var system = this['system'];
-
+
if (system[prop] === value) {
return;
}
-
+
value = value || false;
this['_' + prop] = value;
system[prop] = value;
system.loadSync();
}
-