[minor] Fix whitespaces
This commit is contained in:
parent
6ce0b7aef3
commit
29eb5f905d
9 changed files with 126 additions and 126 deletions
|
@ -22,7 +22,7 @@ require('pkginfo')(module, 'version');
|
||||||
fs.readdirSync(__dirname + '/nconf/stores').forEach(function (file) {
|
fs.readdirSync(__dirname + '/nconf/stores').forEach(function (file) {
|
||||||
var store = file.replace('.js', ''),
|
var store = file.replace('.js', ''),
|
||||||
name = common.capitalize(store);
|
name = common.capitalize(store);
|
||||||
|
|
||||||
nconf.__defineGetter__(name, function () {
|
nconf.__defineGetter__(name, function () {
|
||||||
return require('./nconf/stores/' + store)[name];
|
return require('./nconf/stores/' + store)[name];
|
||||||
});
|
});
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
* (C) 2011, Nodejitsu Inc.
|
* (C) 2011, Nodejitsu Inc.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var fs = require('fs'),
|
var fs = require('fs'),
|
||||||
async = require('async'),
|
async = require('async'),
|
||||||
formats = require('./formats'),
|
formats = require('./formats'),
|
||||||
|
@ -15,7 +15,7 @@ var common = exports;
|
||||||
//
|
//
|
||||||
// ### function path (key)
|
// ### function path (key)
|
||||||
// #### @key {string} The ':' delimited key to split
|
// #### @key {string} The ':' delimited key to split
|
||||||
// Returns a fully-qualified path to a nested nconf key.
|
// Returns a fully-qualified path to a nested nconf key.
|
||||||
//
|
//
|
||||||
common.path = function (key) {
|
common.path = function (key) {
|
||||||
return key.split(':');
|
return key.split(':');
|
||||||
|
@ -50,7 +50,7 @@ common.loadFiles = function (files, callback) {
|
||||||
|
|
||||||
function parseFile (file, next) {
|
function parseFile (file, next) {
|
||||||
fs.readFile(file, function (err, data) {
|
fs.readFile(file, function (err, data) {
|
||||||
return !err
|
return !err
|
||||||
? next(null, options.format.parse(data.toString()))
|
? next(null, options.format.parse(data.toString()))
|
||||||
: next(err);
|
: next(err);
|
||||||
});
|
});
|
||||||
|
@ -91,13 +91,13 @@ common.loadFilesSync = function (files) {
|
||||||
//
|
//
|
||||||
common.merge = function (objs) {
|
common.merge = function (objs) {
|
||||||
var store = new Memory();
|
var store = new Memory();
|
||||||
|
|
||||||
objs.forEach(function (obj) {
|
objs.forEach(function (obj) {
|
||||||
Object.keys(obj).forEach(function (key) {
|
Object.keys(obj).forEach(function (key) {
|
||||||
store.merge(key, obj[key]);
|
store.merge(key, obj[key]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return store.store;
|
return store.store;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ var Provider = exports.Provider = function (options) {
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
// Define wrapper functions for using basic stores
|
// Define wrapper functions for using basic stores
|
||||||
// in this instance
|
// in this instance
|
||||||
//
|
//
|
||||||
['argv', 'env', 'file'].forEach(function (type) {
|
['argv', 'env', 'file'].forEach(function (type) {
|
||||||
|
@ -36,7 +36,7 @@ var Provider = exports.Provider = function (options) {
|
||||||
});
|
});
|
||||||
|
|
||||||
//
|
//
|
||||||
// Define wrapper functions for using
|
// Define wrapper functions for using
|
||||||
// overrides and defaults
|
// overrides and defaults
|
||||||
//
|
//
|
||||||
['defaults', 'overrides'].forEach(function (type) {
|
['defaults', 'overrides'].forEach(function (type) {
|
||||||
|
@ -45,7 +45,7 @@ var Provider = exports.Provider = function (options) {
|
||||||
if (!options.type) {
|
if (!options.type) {
|
||||||
options.type = 'literal';
|
options.type = 'literal';
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.add(type, options);
|
return this.add(type, options);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
@ -54,8 +54,8 @@ var Provider = exports.Provider = function (options) {
|
||||||
// ### function use (name, options)
|
// ### function use (name, options)
|
||||||
// #### @type {string} Type of the nconf store to use.
|
// #### @type {string} Type of the nconf store to use.
|
||||||
// #### @options {Object} Options for the store instance.
|
// #### @options {Object} Options for the store instance.
|
||||||
// Adds (or replaces) a new store with the specified `name`
|
// Adds (or replaces) a new store with the specified `name`
|
||||||
// and `options`. If `options.type` is not set, then `name`
|
// and `options`. If `options.type` is not set, then `name`
|
||||||
// will be used instead:
|
// will be used instead:
|
||||||
//
|
//
|
||||||
// provider.use('file');
|
// provider.use('file');
|
||||||
|
@ -70,18 +70,18 @@ Provider.prototype.use = function (name, options) {
|
||||||
return options[key] === store[key];
|
return options[key] === store[key];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var store = this.stores[name],
|
var store = this.stores[name],
|
||||||
update = store && !sameOptions(store);
|
update = store && !sameOptions(store);
|
||||||
|
|
||||||
if (!store || update) {
|
if (!store || update) {
|
||||||
if (update) {
|
if (update) {
|
||||||
this.remove(name);
|
this.remove(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.add(name, options);
|
this.add(name, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -98,22 +98,22 @@ Provider.prototype.use = function (name, options) {
|
||||||
Provider.prototype.add = function (name, options) {
|
Provider.prototype.add = function (name, options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
var type = options.type || name;
|
var type = options.type || name;
|
||||||
|
|
||||||
if (!require('../nconf')[common.capitalize(type)]) {
|
if (!require('../nconf')[common.capitalize(type)]) {
|
||||||
throw new Error('Cannot add store with unknown type: ' + type);
|
throw new Error('Cannot add store with unknown type: ' + type);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.stores[name] = this.create(type, options);
|
this.stores[name] = this.create(type, options);
|
||||||
|
|
||||||
if (this.stores[name].loadSync) {
|
if (this.stores[name].loadSync) {
|
||||||
this.stores[name].loadSync();
|
this.stores[name].loadSync();
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
// ### function remove (name)
|
// ### function remove (name)
|
||||||
// #### @name {string} Name of the store to remove from this instance
|
// #### @name {string} Name of the store to remove from this instance
|
||||||
// Removes a store with the specified `name` from this instance. Users
|
// Removes a store with the specified `name` from this instance. Users
|
||||||
// are allowed to pass in a type argument (e.g. `memory`) as name if
|
// are allowed to pass in a type argument (e.g. `memory`) as name if
|
||||||
|
@ -128,7 +128,7 @@ Provider.prototype.remove = function (name) {
|
||||||
// ### function create (type, options)
|
// ### function create (type, options)
|
||||||
// #### @type {string} Type of the nconf store to use.
|
// #### @type {string} Type of the nconf store to use.
|
||||||
// #### @options {Object} Options for the store instance.
|
// #### @options {Object} Options for the store instance.
|
||||||
// Creates a store of the specified `type` using the
|
// Creates a store of the specified `type` using the
|
||||||
// specified `options`.
|
// specified `options`.
|
||||||
//
|
//
|
||||||
Provider.prototype.create = function (type, options) {
|
Provider.prototype.create = function (type, options) {
|
||||||
|
@ -139,11 +139,11 @@ Provider.prototype.create = function (type, options) {
|
||||||
// ### function init (options)
|
// ### function init (options)
|
||||||
// #### @options {Object} Options to initialize this instance with.
|
// #### @options {Object} Options to initialize this instance with.
|
||||||
// Initializes this instance with additional `stores` or `sources` in the
|
// Initializes this instance with additional `stores` or `sources` in the
|
||||||
// `options` supplied.
|
// `options` supplied.
|
||||||
//
|
//
|
||||||
Provider.prototype.init = function (options) {
|
Provider.prototype.init = function (options) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Add any stores passed in through the options
|
// Add any stores passed in through the options
|
||||||
// to this instance.
|
// to this instance.
|
||||||
|
@ -160,7 +160,7 @@ Provider.prototype.init = function (options) {
|
||||||
self.add(store.name || name || store.type, store);
|
self.add(store.name || name || store.type, store);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Add any read-only sources to this instance
|
// Add any read-only sources to this instance
|
||||||
//
|
//
|
||||||
|
@ -189,9 +189,9 @@ Provider.prototype.get = function (key, callback) {
|
||||||
if (!callback) {
|
if (!callback) {
|
||||||
return this._execute('get', 1, key, 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
|
// slightly more complicated because we do not need to traverse
|
||||||
// the entire set of stores, but up until there is a defined value.
|
// the entire set of stores, but up until there is a defined value.
|
||||||
//
|
//
|
||||||
|
@ -199,24 +199,24 @@ Provider.prototype.get = function (key, callback) {
|
||||||
names = Object.keys(this.stores),
|
names = Object.keys(this.stores),
|
||||||
self = this,
|
self = this,
|
||||||
response;
|
response;
|
||||||
|
|
||||||
async.whilst(function () {
|
async.whilst(function () {
|
||||||
return typeof response === 'undefined' && current < names.length;
|
return typeof response === 'undefined' && current < names.length;
|
||||||
}, function (next) {
|
}, function (next) {
|
||||||
var store = self.stores[names[current]];
|
var store = self.stores[names[current]];
|
||||||
current++;
|
current++;
|
||||||
|
|
||||||
if (store.get.length >= 2) {
|
if (store.get.length >= 2) {
|
||||||
return store.get(key, function (err, value) {
|
return store.get(key, function (err, value) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
response = value;
|
response = value;
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
response = store.get(key);
|
response = store.get(key);
|
||||||
next();
|
next();
|
||||||
}, function (err) {
|
}, function (err) {
|
||||||
|
@ -241,7 +241,7 @@ Provider.prototype.set = function (key, value, callback) {
|
||||||
// Clears all keys associated with this instance.
|
// Clears all keys associated with this instance.
|
||||||
//
|
//
|
||||||
Provider.prototype.reset = function (callback) {
|
Provider.prototype.reset = function (callback) {
|
||||||
return this._execute('reset', 0, callback);
|
return this._execute('reset', 0, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -259,7 +259,7 @@ Provider.prototype.clear = function (key, callback) {
|
||||||
// #### @key {string} Key to merge the value into
|
// #### @key {string} Key to merge the value into
|
||||||
// #### @value {literal|Object} Value to merge into the key
|
// #### @value {literal|Object} Value to merge into the key
|
||||||
// #### @callback {function} **Optional** Continuation to respond to when complete.
|
// #### @callback {function} **Optional** Continuation to respond to when complete.
|
||||||
// Merges the properties in `value` into the existing object value at `key`.
|
// Merges the properties in `value` into the existing object value at `key`.
|
||||||
//
|
//
|
||||||
// 1. If the existing value `key` is not an Object, it will be completely overwritten.
|
// 1. If the existing value `key` is not an Object, it will be completely overwritten.
|
||||||
// 2. If `key` is not supplied, then the `value` will be merged into the root.
|
// 2. If `key` is not supplied, then the `value` will be merged into the root.
|
||||||
|
@ -270,19 +270,19 @@ Provider.prototype.merge = function () {
|
||||||
callback = typeof args[args.length - 1] === 'function' && args.pop(),
|
callback = typeof args[args.length - 1] === 'function' && args.pop(),
|
||||||
value = args.pop(),
|
value = args.pop(),
|
||||||
key = args.pop();
|
key = args.pop();
|
||||||
|
|
||||||
function mergeProperty (prop, next) {
|
function mergeProperty (prop, next) {
|
||||||
return self._execute('merge', 2, prop, value[prop], next);
|
return self._execute('merge', 2, prop, value[prop], next);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!key) {
|
if (!key) {
|
||||||
if (Array.isArray(value) || typeof value !== 'object') {
|
if (Array.isArray(value) || typeof value !== 'object') {
|
||||||
return onError(new Error('Cannot merge non-Object into top-level.'), callback);
|
return onError(new Error('Cannot merge non-Object into top-level.'), callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
return async.forEach(Object.keys(value), mergeProperty, callback || function () { })
|
return async.forEach(Object.keys(value), mergeProperty, callback || function () { })
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._execute('merge', 2, key, value, callback);
|
return this._execute('merge', 2, key, value, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -293,33 +293,33 @@ Provider.prototype.merge = function () {
|
||||||
//
|
//
|
||||||
Provider.prototype.load = function (callback) {
|
Provider.prototype.load = function (callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
function getStores () {
|
function getStores () {
|
||||||
var stores = Object.keys(self.stores);
|
var stores = Object.keys(self.stores);
|
||||||
stores.reverse();
|
stores.reverse();
|
||||||
return stores.map(function (name) {
|
return stores.map(function (name) {
|
||||||
return self.stores[name];
|
return self.stores[name];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadStoreSync(store) {
|
function loadStoreSync(store) {
|
||||||
if (!store.loadSync) {
|
if (!store.loadSync) {
|
||||||
throw new Error('nconf store ' + store.type + ' has no loadSync() method');
|
throw new Error('nconf store ' + store.type + ' has no loadSync() method');
|
||||||
}
|
}
|
||||||
|
|
||||||
return store.loadSync();
|
return store.loadSync();
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadStore(store, next) {
|
function loadStore(store, next) {
|
||||||
if (!store.load && !store.loadSync) {
|
if (!store.load && !store.loadSync) {
|
||||||
return next(new Error('nconf store ' + store.type + ' has no load() method'));
|
return next(new Error('nconf store ' + store.type + ' has no load() method'));
|
||||||
}
|
}
|
||||||
|
|
||||||
return store.loadSync
|
return store.loadSync
|
||||||
? next(null, store.loadSync())
|
? next(null, store.loadSync())
|
||||||
: store.load(next);
|
: store.load(next);
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadBatch (targets, done) {
|
function loadBatch (targets, done) {
|
||||||
if (!done) {
|
if (!done) {
|
||||||
return common.merge(targets.map(loadStoreSync));
|
return common.merge(targets.map(loadStoreSync));
|
||||||
|
@ -329,26 +329,26 @@ Provider.prototype.load = function (callback) {
|
||||||
return err ? done(err) : done(null, common.merge(objs));
|
return err ? done(err) : done(null, common.merge(objs));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function mergeSources (data) {
|
function mergeSources (data) {
|
||||||
//
|
//
|
||||||
// If `data` was returned then merge it into
|
// If `data` was returned then merge it into
|
||||||
// the system store.
|
// the system store.
|
||||||
//
|
//
|
||||||
if (data && typeof data === 'object') {
|
if (data && typeof data === 'object') {
|
||||||
self.use('sources', {
|
self.use('sources', {
|
||||||
type: 'literal',
|
type: 'literal',
|
||||||
store: data
|
store: data
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadSources () {
|
function loadSources () {
|
||||||
var sourceHierarchy = self.sources.splice(0);
|
var sourceHierarchy = self.sources.splice(0);
|
||||||
sourceHierarchy.reverse();
|
sourceHierarchy.reverse();
|
||||||
|
|
||||||
//
|
//
|
||||||
// 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
|
// store is capable of loading synchronously
|
||||||
// then do so.
|
// then do so.
|
||||||
//
|
//
|
||||||
|
@ -356,78 +356,78 @@ Provider.prototype.load = function (callback) {
|
||||||
mergeSources(loadBatch(sourceHierarchy));
|
mergeSources(loadBatch(sourceHierarchy));
|
||||||
return loadBatch(getStores());
|
return loadBatch(getStores());
|
||||||
}
|
}
|
||||||
|
|
||||||
loadBatch(sourceHierarchy, function (err, data) {
|
loadBatch(sourceHierarchy, function (err, data) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
mergeSources(data);
|
mergeSources(data);
|
||||||
return loadBatch(getStores(), callback);
|
return loadBatch(getStores(), callback);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return self.sources.length
|
return self.sources.length
|
||||||
? loadSources()
|
? loadSources()
|
||||||
: loadBatch(getStores(), callback);
|
: loadBatch(getStores(), callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
// ### function save (value, callback)
|
// ### function save (value, callback)
|
||||||
// #### @value {Object} **Optional** Config object to set for this instance
|
// #### @value {Object} **Optional** Config object to set for this instance
|
||||||
// #### @callback {function} Continuation to respond to when complete.
|
// #### @callback {function} Continuation to respond to when complete.
|
||||||
// Removes any existing configuration settings that may exist in this
|
// Removes any existing configuration settings that may exist in this
|
||||||
// instance and then adds all key-value pairs in `value`.
|
// instance and then adds all key-value pairs in `value`.
|
||||||
//
|
//
|
||||||
Provider.prototype.save = function (value, callback) {
|
Provider.prototype.save = function (value, callback) {
|
||||||
if (!callback && typeof value === 'function') {
|
if (!callback && typeof value === 'function') {
|
||||||
callback = value;
|
callback = value;
|
||||||
value = null;
|
value = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var self = this,
|
var self = this,
|
||||||
names = Object.keys(this.stores);
|
names = Object.keys(this.stores);
|
||||||
|
|
||||||
function saveStoreSync(name) {
|
function saveStoreSync(name) {
|
||||||
var store = self.stores[name];
|
var store = self.stores[name];
|
||||||
|
|
||||||
//
|
//
|
||||||
// If the `store` doesn't have a `saveSync` method,
|
// If the `store` doesn't have a `saveSync` method,
|
||||||
// just ignore it and continue.
|
// just ignore it and continue.
|
||||||
//
|
//
|
||||||
return store.saveSync
|
return store.saveSync
|
||||||
? store.saveSync()
|
? store.saveSync()
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveStore(name, next) {
|
function saveStore(name, next) {
|
||||||
var store = self.stores[name];
|
var store = self.stores[name];
|
||||||
|
|
||||||
//
|
//
|
||||||
// If the `store` doesn't have a `save` or saveSync`
|
// If the `store` doesn't have a `save` or saveSync`
|
||||||
// method(s), just ignore it and continue.
|
// method(s), just ignore it and continue.
|
||||||
//
|
//
|
||||||
if (!store.save && !store.saveSync) {
|
if (!store.save && !store.saveSync) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
return store.saveSync
|
return store.saveSync
|
||||||
? next(null, store.saveSync())
|
? next(null, store.saveSync())
|
||||||
: store.save(next);
|
: 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
|
// store is capable of saving synchronously
|
||||||
// then do so.
|
// then do so.
|
||||||
//
|
//
|
||||||
if (!callback) {
|
if (!callback) {
|
||||||
return common.merge(names.map(saveStoreSync));
|
return common.merge(names.map(saveStoreSync));
|
||||||
}
|
}
|
||||||
|
|
||||||
async.map(names, saveStore, function (err, objs) {
|
async.map(names, saveStore, function (err, objs) {
|
||||||
return err ? callback(err) : callback();
|
return err ? callback(err) : callback();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -444,7 +444,7 @@ Provider.prototype._execute = function (action, syncLength /* [arguments] */) {
|
||||||
destructive = ['set', 'clear', 'merge'].indexOf(action) !== -1,
|
destructive = ['set', 'clear', 'merge'].indexOf(action) !== -1,
|
||||||
self = this,
|
self = this,
|
||||||
response;
|
response;
|
||||||
|
|
||||||
function runAction (name, next) {
|
function runAction (name, next) {
|
||||||
var store = self.stores[name];
|
var store = self.stores[name];
|
||||||
|
|
||||||
|
@ -456,7 +456,7 @@ Provider.prototype._execute = function (action, syncLength /* [arguments] */) {
|
||||||
? store[action].apply(store, args.concat(next))
|
? store[action].apply(store, args.concat(next))
|
||||||
: next(null, store[action].apply(store, args));
|
: next(null, store[action].apply(store, args));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (callback) {
|
if (callback) {
|
||||||
return async.forEach(Object.keys(this.stores), runAction, function (err) {
|
return async.forEach(Object.keys(this.stores), runAction, function (err) {
|
||||||
return err ? callback(err) : callback();
|
return err ? callback(err) : callback();
|
||||||
|
@ -475,7 +475,7 @@ Provider.prototype._execute = function (action, syncLength /* [arguments] */) {
|
||||||
response = store[action].apply(store, args);
|
response = store[action].apply(store, args);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -486,6 +486,6 @@ function onError(err, callback) {
|
||||||
if (callback) {
|
if (callback) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
|
@ -4,10 +4,10 @@
|
||||||
* (C) 2011, Nodejitsu Inc.
|
* (C) 2011, Nodejitsu Inc.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var util = require('util'),
|
var util = require('util'),
|
||||||
Memory = require('./memory').Memory;
|
Memory = require('./memory').Memory;
|
||||||
|
|
||||||
//
|
//
|
||||||
// ### function Argv (options)
|
// ### function Argv (options)
|
||||||
// #### @options {Object} Options for this instance.
|
// #### @options {Object} Options for this instance.
|
||||||
|
@ -36,26 +36,26 @@ Argv.prototype.loadSync = function () {
|
||||||
|
|
||||||
//
|
//
|
||||||
// ### function loadArgv ()
|
// ### function loadArgv ()
|
||||||
// Loads the data passed in from the command-line arguments
|
// Loads the data passed in from the command-line arguments
|
||||||
// into this instance.
|
// into this instance.
|
||||||
//
|
//
|
||||||
Argv.prototype.loadArgv = function () {
|
Argv.prototype.loadArgv = function () {
|
||||||
var self = this,
|
var self = this,
|
||||||
argv;
|
argv;
|
||||||
|
|
||||||
argv = typeof this.options === 'object'
|
argv = typeof this.options === 'object'
|
||||||
? require('optimist')(process.argv.slice(2)).options(this.options).argv
|
? require('optimist')(process.argv.slice(2)).options(this.options).argv
|
||||||
: require('optimist')(process.argv.slice(2)).argv;
|
: require('optimist')(process.argv.slice(2)).argv;
|
||||||
|
|
||||||
if (!argv) {
|
if (!argv) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.readOnly = false;
|
this.readOnly = false;
|
||||||
Object.keys(argv).forEach(function (key) {
|
Object.keys(argv).forEach(function (key) {
|
||||||
self.set(key, argv[key]);
|
self.set(key, argv[key]);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.readOnly = true;
|
this.readOnly = true;
|
||||||
return this.store;
|
return this.store;
|
||||||
};
|
};
|
|
@ -4,10 +4,10 @@
|
||||||
* (C) 2011, Nodejitsu Inc.
|
* (C) 2011, Nodejitsu Inc.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var util = require('util'),
|
var util = require('util'),
|
||||||
Memory = require('./memory').Memory;
|
Memory = require('./memory').Memory;
|
||||||
|
|
||||||
//
|
//
|
||||||
// ### function Env (options)
|
// ### function Env (options)
|
||||||
// #### @options {Object} Options for this instance.
|
// #### @options {Object} Options for this instance.
|
||||||
|
@ -40,14 +40,14 @@ Env.prototype.loadSync = function () {
|
||||||
//
|
//
|
||||||
Env.prototype.loadEnv = function () {
|
Env.prototype.loadEnv = function () {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this.readOnly = false;
|
this.readOnly = false;
|
||||||
Object.keys(process.env).filter(function (key) {
|
Object.keys(process.env).filter(function (key) {
|
||||||
return !self.options.length || self.options.indexOf(key) !== -1;
|
return !self.options.length || self.options.indexOf(key) !== -1;
|
||||||
}).forEach(function (key) {
|
}).forEach(function (key) {
|
||||||
self.set(key, process.env[key]);
|
self.set(key, process.env[key]);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.readOnly = true;
|
this.readOnly = true;
|
||||||
return this.store;
|
return this.store;
|
||||||
};
|
};
|
||||||
|
|
|
@ -11,7 +11,7 @@ var fs = require('fs'),
|
||||||
formats = require('../formats'),
|
formats = require('../formats'),
|
||||||
Memory = require('./memory').Memory,
|
Memory = require('./memory').Memory,
|
||||||
existsSync = fs.existsSync || path.existsSync;
|
existsSync = fs.existsSync || path.existsSync;
|
||||||
|
|
||||||
//
|
//
|
||||||
// ### function File (options)
|
// ### function File (options)
|
||||||
// #### @options {Object} Options for this instance
|
// #### @options {Object} Options for this instance
|
||||||
|
@ -30,7 +30,7 @@ var File = exports.File = function (options) {
|
||||||
this.dir = options.dir || process.cwd();
|
this.dir = options.dir || process.cwd();
|
||||||
this.format = options.format || formats.json;
|
this.format = options.format || formats.json;
|
||||||
this.json_spacing = options.json_spacing || 2;
|
this.json_spacing = options.json_spacing || 2;
|
||||||
|
|
||||||
if (options.search) {
|
if (options.search) {
|
||||||
this.search(this.dir);
|
this.search(this.dir);
|
||||||
}
|
}
|
||||||
|
@ -40,10 +40,10 @@ var File = exports.File = function (options) {
|
||||||
util.inherits(File, Memory);
|
util.inherits(File, Memory);
|
||||||
|
|
||||||
//
|
//
|
||||||
// ### function save (value, callback)
|
// ### function save (value, callback)
|
||||||
// #### @value {Object} _Ignored_ Left here for consistency
|
// #### @value {Object} _Ignored_ Left here for consistency
|
||||||
// #### @callback {function} Continuation to respond to when complete.
|
// #### @callback {function} Continuation to respond to when complete.
|
||||||
// Saves the current configuration object to disk at `this.file`
|
// Saves the current configuration object to disk at `this.file`
|
||||||
// using the format specified by `this.format`.
|
// using the format specified by `this.format`.
|
||||||
//
|
//
|
||||||
File.prototype.save = function (value, callback) {
|
File.prototype.save = function (value, callback) {
|
||||||
|
@ -51,17 +51,17 @@ File.prototype.save = function (value, callback) {
|
||||||
callback = value;
|
callback = value;
|
||||||
value = null;
|
value = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.writeFile(this.file, this.format.stringify(this.store, null, this.json_spacing), function (err) {
|
fs.writeFile(this.file, this.format.stringify(this.store, null, this.json_spacing), function (err) {
|
||||||
return err ? callback(err) : callback();
|
return err ? callback(err) : callback();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
// ### function saveSync (value, callback)
|
// ### function saveSync (value, callback)
|
||||||
// #### @value {Object} _Ignored_ Left here for consistency
|
// #### @value {Object} _Ignored_ Left here for consistency
|
||||||
// #### @callback {function} **Optional** Continuation to respond to when complete.
|
// #### @callback {function} **Optional** Continuation to respond to when complete.
|
||||||
// Saves the current configuration object to disk at `this.file`
|
// Saves the current configuration object to disk at `this.file`
|
||||||
// using the format specified by `this.format` synchronously.
|
// using the format specified by `this.format` synchronously.
|
||||||
//
|
//
|
||||||
File.prototype.saveSync = function (value) {
|
File.prototype.saveSync = function (value) {
|
||||||
|
@ -94,14 +94,14 @@ File.prototype.load = function (callback) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
self.store = self.format.parse(data.toString());
|
self.store = self.format.parse(data.toString());
|
||||||
}
|
}
|
||||||
catch (ex) {
|
catch (ex) {
|
||||||
return callback(new Error("Error parsing your JSON configuration file."));
|
return callback(new Error("Error parsing your JSON configuration file."));
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(null, self.store);
|
callback(null, self.store);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -138,8 +138,8 @@ File.prototype.loadSync = function () {
|
||||||
//
|
//
|
||||||
// ### function search (base)
|
// ### function search (base)
|
||||||
// #### @base {string} Base directory (or file) to begin searching for the target file.
|
// #### @base {string} Base directory (or file) to begin searching for the target file.
|
||||||
// Attempts to find `this.file` by iteratively searching up the
|
// Attempts to find `this.file` by iteratively searching up the
|
||||||
// directory structure
|
// directory structure
|
||||||
//
|
//
|
||||||
File.prototype.search = function (base) {
|
File.prototype.search = function (base) {
|
||||||
var looking = true,
|
var looking = true,
|
||||||
|
@ -181,7 +181,7 @@ File.prototype.search = function (base) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (looking) {
|
while (looking) {
|
||||||
//
|
//
|
||||||
// Iteratively look up the directory structure from `base`
|
// Iteratively look up the directory structure from `base`
|
||||||
|
@ -210,7 +210,7 @@ File.prototype.search = function (base) {
|
||||||
// Ignore errors
|
// Ignore errors
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
|
||||||
looking = false;
|
looking = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -222,6 +222,6 @@ File.prototype.search = function (base) {
|
||||||
// the search was unsuccessful use the original value for `this.file`.
|
// the search was unsuccessful use the original value for `this.file`.
|
||||||
//
|
//
|
||||||
this.file = fullpath || this.file;
|
this.file = fullpath || this.file;
|
||||||
|
|
||||||
return fullpath;
|
return fullpath;
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,12 +5,12 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var util = require('util'),
|
var util = require('util'),
|
||||||
Memory = require('./memory').Memory
|
Memory = require('./memory').Memory
|
||||||
|
|
||||||
var Literal = exports.Literal = function Literal (options) {
|
var Literal = exports.Literal = function Literal (options) {
|
||||||
Memory.call(this, options);
|
Memory.call(this, options);
|
||||||
|
|
||||||
options = options || {}
|
options = options || {}
|
||||||
this.type = 'literal';
|
this.type = 'literal';
|
||||||
this.readOnly = true;
|
this.readOnly = true;
|
||||||
|
|
|
@ -13,7 +13,7 @@ var common = require('../common');
|
||||||
// Constructor function for the Memory nconf store which maintains
|
// Constructor function for the Memory nconf store which maintains
|
||||||
// a nested json structure based on key delimiters `:`.
|
// a nested json structure based on key delimiters `:`.
|
||||||
//
|
//
|
||||||
// e.g. `my:nested:key` ==> `{ my: { nested: { key: } } }`
|
// e.g. `my:nested:key` ==> `{ my: { nested: { key: } } }`
|
||||||
//
|
//
|
||||||
var Memory = exports.Memory = function (options) {
|
var Memory = exports.Memory = function (options) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
|
@ -22,7 +22,7 @@ var Memory = exports.Memory = function (options) {
|
||||||
this.mtimes = {};
|
this.mtimes = {};
|
||||||
this.readOnly = false;
|
this.readOnly = false;
|
||||||
this.loadFrom = options.loadFrom || null;
|
this.loadFrom = options.loadFrom || null;
|
||||||
|
|
||||||
if (this.loadFrom) {
|
if (this.loadFrom) {
|
||||||
this.store = common.loadFilesSync(this.loadFrom);
|
this.store = common.loadFilesSync(this.loadFrom);
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ var Memory = exports.Memory = function (options) {
|
||||||
// Retrieves the value for the specified key (if any).
|
// Retrieves the value for the specified key (if any).
|
||||||
//
|
//
|
||||||
Memory.prototype.get = function (key) {
|
Memory.prototype.get = function (key) {
|
||||||
var target = this.store,
|
var target = this.store,
|
||||||
path = common.path(key);
|
path = common.path(key);
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -45,7 +45,7 @@ Memory.prototype.get = function (key) {
|
||||||
if (!(target && key in target)) {
|
if (!(target && key in target)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
target = target[key];
|
target = target[key];
|
||||||
if (path.length === 0) {
|
if (path.length === 0) {
|
||||||
return target;
|
return target;
|
||||||
|
@ -63,15 +63,15 @@ Memory.prototype.set = function (key, value) {
|
||||||
if (this.readOnly) {
|
if (this.readOnly) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var target = this.store,
|
var target = this.store,
|
||||||
path = common.path(key);
|
path = common.path(key);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Update the `mtime` (modified time) of the key
|
// Update the `mtime` (modified time) of the key
|
||||||
//
|
//
|
||||||
this.mtimes[key] = Date.now();
|
this.mtimes[key] = Date.now();
|
||||||
|
|
||||||
//
|
//
|
||||||
// Scope into the object to get the appropriate nested context
|
// Scope into the object to get the appropriate nested context
|
||||||
//
|
//
|
||||||
|
@ -80,10 +80,10 @@ Memory.prototype.set = function (key, value) {
|
||||||
if (!target[key] || typeof target[key] !== 'object') {
|
if (!target[key] || typeof target[key] !== 'object') {
|
||||||
target[key] = {};
|
target[key] = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
target = target[key];
|
target = target[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the specified value in the nested JSON structure
|
// Set the specified value in the nested JSON structure
|
||||||
key = path.shift();
|
key = path.shift();
|
||||||
target[key] = value;
|
target[key] = value;
|
||||||
|
@ -99,15 +99,15 @@ Memory.prototype.clear = function (key) {
|
||||||
if (this.readOnly) {
|
if (this.readOnly) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
var target = this.store,
|
var target = this.store,
|
||||||
path = common.path(key);
|
path = common.path(key);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Remove the key from the set of `mtimes` (modified times)
|
// Remove the key from the set of `mtimes` (modified times)
|
||||||
//
|
//
|
||||||
delete this.mtimes[key];
|
delete this.mtimes[key];
|
||||||
|
|
||||||
//
|
//
|
||||||
// Scope into the object to get the appropriate nested context
|
// Scope into the object to get the appropriate nested context
|
||||||
//
|
//
|
||||||
|
@ -116,10 +116,10 @@ Memory.prototype.clear = function (key) {
|
||||||
if (!target[key]) {
|
if (!target[key]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
target = target[key];
|
target = target[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete the key from the nested JSON structure
|
// Delete the key from the nested JSON structure
|
||||||
key = path.shift();
|
key = path.shift();
|
||||||
delete target[key];
|
delete target[key];
|
||||||
|
@ -138,7 +138,7 @@ Memory.prototype.merge = function (key, value) {
|
||||||
if (this.readOnly) {
|
if (this.readOnly) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// If the key is not an `Object` or is an `Array`,
|
// If the key is not an `Object` or is an `Array`,
|
||||||
// then simply set it. Merging is for Objects.
|
// then simply set it. Merging is for Objects.
|
||||||
|
@ -146,17 +146,17 @@ Memory.prototype.merge = function (key, value) {
|
||||||
if (typeof value !== 'object' || Array.isArray(value)) {
|
if (typeof value !== 'object' || Array.isArray(value)) {
|
||||||
return this.set(key, value);
|
return this.set(key, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
var self = this,
|
var self = this,
|
||||||
target = this.store,
|
target = this.store,
|
||||||
path = common.path(key),
|
path = common.path(key),
|
||||||
fullKey = key;
|
fullKey = key;
|
||||||
|
|
||||||
//
|
//
|
||||||
// Update the `mtime` (modified time) of the key
|
// Update the `mtime` (modified time) of the key
|
||||||
//
|
//
|
||||||
this.mtimes[key] = Date.now();
|
this.mtimes[key] = Date.now();
|
||||||
|
|
||||||
//
|
//
|
||||||
// Scope into the object to get the appropriate nested context
|
// Scope into the object to get the appropriate nested context
|
||||||
//
|
//
|
||||||
|
@ -165,13 +165,13 @@ Memory.prototype.merge = function (key, value) {
|
||||||
if (!target[key]) {
|
if (!target[key]) {
|
||||||
target[key] = {};
|
target[key] = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
target = target[key];
|
target = target[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the specified value in the nested JSON structure
|
// Set the specified value in the nested JSON structure
|
||||||
key = path.shift();
|
key = path.shift();
|
||||||
|
|
||||||
//
|
//
|
||||||
// If the current value at the key target is not an `Object`,
|
// If the current value at the key target is not an `Object`,
|
||||||
// or is an `Array` then simply override it because the new value
|
// or is an `Array` then simply override it because the new value
|
||||||
|
@ -181,7 +181,7 @@ Memory.prototype.merge = function (key, value) {
|
||||||
target[key] = value;
|
target[key] = value;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Object.keys(value).every(function (nested) {
|
return Object.keys(value).every(function (nested) {
|
||||||
return self.merge(fullKey + ':' + nested, value[nested]);
|
return self.merge(fullKey + ':' + nested, value[nested]);
|
||||||
});
|
});
|
||||||
|
@ -195,7 +195,7 @@ Memory.prototype.reset = function () {
|
||||||
if (this.readOnly) {
|
if (this.readOnly) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.mtimes = {};
|
this.mtimes = {};
|
||||||
this.store = {};
|
this.store = {};
|
||||||
return true;
|
return true;
|
||||||
|
|
2
usage.js
2
usage.js
|
@ -18,7 +18,7 @@ var single = new nconf.Provider({
|
||||||
|
|
||||||
//
|
//
|
||||||
// Configure the provider with multiple hierarchical stores
|
// Configure the provider with multiple hierarchical stores
|
||||||
// representing `user` and `global` configuration values.
|
// representing `user` and `global` configuration values.
|
||||||
//
|
//
|
||||||
var multiple = new nconf.Provider({
|
var multiple = new nconf.Provider({
|
||||||
stores: [
|
stores: [
|
||||||
|
|
Loading…
Reference in a new issue