diff --git a/lib/nconf.js b/lib/nconf.js index 503b52d..be81056 100644 --- a/lib/nconf.js +++ b/lib/nconf.js @@ -22,7 +22,7 @@ require('pkginfo')(module, 'version'); fs.readdirSync(__dirname + '/nconf/stores').forEach(function (file) { var store = file.replace('.js', ''), name = common.capitalize(store); - + nconf.__defineGetter__(name, function () { return require('./nconf/stores/' + store)[name]; }); diff --git a/lib/nconf/common.js b/lib/nconf/common.js index 50efb3a..730de70 100644 --- a/lib/nconf/common.js +++ b/lib/nconf/common.js @@ -4,7 +4,7 @@ * (C) 2011, Nodejitsu Inc. * */ - + var fs = require('fs'), async = require('async'), formats = require('./formats'), @@ -15,7 +15,7 @@ var common = exports; // // ### function path (key) // #### @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) { return key.split(':'); @@ -50,7 +50,7 @@ common.loadFiles = function (files, callback) { function parseFile (file, next) { fs.readFile(file, function (err, data) { - return !err + return !err ? next(null, options.format.parse(data.toString())) : next(err); }); @@ -91,13 +91,13 @@ common.loadFilesSync = function (files) { // common.merge = function (objs) { var store = new Memory(); - + objs.forEach(function (obj) { Object.keys(obj).forEach(function (key) { store.merge(key, obj[key]); }); }); - + return store.store; }; diff --git a/lib/nconf/provider.js b/lib/nconf/provider.js index fdb0d7b..c1a5de7 100644 --- a/lib/nconf/provider.js +++ b/lib/nconf/provider.js @@ -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 // ['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 // ['defaults', 'overrides'].forEach(function (type) { @@ -45,7 +45,7 @@ var Provider = exports.Provider = function (options) { if (!options.type) { options.type = 'literal'; } - + return this.add(type, options); }; }); @@ -54,8 +54,8 @@ var Provider = exports.Provider = function (options) { // ### function use (name, options) // #### @type {string} Type of the nconf store to use. // #### @options {Object} Options for the store instance. -// 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'); @@ -70,18 +70,18 @@ Provider.prototype.use = function (name, options) { return options[key] === store[key]; }); } - + var store = this.stores[name], update = store && !sameOptions(store); - + if (!store || update) { if (update) { this.remove(name); } - + this.add(name, options); } - + return this; }; @@ -98,22 +98,22 @@ Provider.prototype.use = function (name, options) { Provider.prototype.add = function (name, options) { options = options || {}; var type = options.type || name; - + if (!require('../nconf')[common.capitalize(type)]) { throw new Error('Cannot add store with unknown type: ' + type); } - + this.stores[name] = this.create(type, options); - + if (this.stores[name].loadSync) { this.stores[name].loadSync(); } - + return this; }; // -// ### function remove (name) +// ### function remove (name) // #### @name {string} Name of the store to remove from this instance // 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 @@ -128,7 +128,7 @@ Provider.prototype.remove = function (name) { // ### function create (type, options) // #### @type {string} Type of the nconf store to use. // #### @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`. // Provider.prototype.create = function (type, options) { @@ -139,11 +139,11 @@ Provider.prototype.create = function (type, options) { // ### function init (options) // #### @options {Object} Options to initialize this instance with. // Initializes this instance with additional `stores` or `sources` in the -// `options` supplied. +// `options` supplied. // Provider.prototype.init = function (options) { var self = this; - + // // Add any stores passed in through the options // to this instance. @@ -160,7 +160,7 @@ Provider.prototype.init = function (options) { self.add(store.name || name || store.type, store); }); } - + // // Add any read-only sources to this instance // @@ -189,9 +189,9 @@ Provider.prototype.get = function (key, callback) { 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. // @@ -199,24 +199,24 @@ Provider.prototype.get = function (key, callback) { names = Object.keys(this.stores), self = this, response; - + async.whilst(function () { return typeof response === 'undefined' && current < names.length; }, function (next) { var store = self.stores[names[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) { @@ -241,7 +241,7 @@ Provider.prototype.set = function (key, value, callback) { // Clears all keys associated with this instance. // 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 // #### @value {literal|Object} Value to merge into the key // #### @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. // 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(), value = args.pop(), key = args.pop(); - + function mergeProperty (prop, next) { return self._execute('merge', 2, prop, value[prop], next); } - + if (!key) { if (Array.isArray(value) || typeof value !== 'object') { return onError(new Error('Cannot merge non-Object into top-level.'), callback); } - + return async.forEach(Object.keys(value), mergeProperty, callback || function () { }) } - + return this._execute('merge', 2, key, value, callback); }; @@ -293,33 +293,33 @@ Provider.prototype.merge = function () { // Provider.prototype.load = function (callback) { var self = this; - + function getStores () { var stores = Object.keys(self.stores); stores.reverse(); - return stores.map(function (name) { + return stores.map(function (name) { return self.stores[name]; }); } - + function loadStoreSync(store) { if (!store.loadSync) { throw new Error('nconf store ' + store.type + ' has no loadSync() method'); } - + return store.loadSync(); } - + function loadStore(store, next) { 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); } - + function loadBatch (targets, done) { if (!done) { return common.merge(targets.map(loadStoreSync)); @@ -329,26 +329,26 @@ Provider.prototype.load = function (callback) { return err ? done(err) : done(null, common.merge(objs)); }); } - + function mergeSources (data) { // - // If `data` was returned then merge it into + // If `data` was returned then merge it into // the system store. // if (data && typeof data === 'object') { self.use('sources', { - type: 'literal', + type: 'literal', store: data }); } } - + function loadSources () { var sourceHierarchy = self.sources.splice(0); 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 // then do so. // @@ -356,78 +356,78 @@ Provider.prototype.load = function (callback) { mergeSources(loadBatch(sourceHierarchy)); return loadBatch(getStores()); } - + loadBatch(sourceHierarchy, function (err, data) { if (err) { return callback(err); } - + mergeSources(data); return loadBatch(getStores(), callback); }); } - - return self.sources.length + + return self.sources.length ? loadSources() : loadBatch(getStores(), callback); }; // -// ### function save (value, callback) +// ### function save (value, callback) // #### @value {Object} **Optional** Config object to set for this instance // #### @callback {function} Continuation to respond to when complete. // 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) { if (!callback && typeof value === 'function') { callback = value; value = null; } - + var self = this, names = Object.keys(this.stores); - + function saveStoreSync(name) { var store = self.stores[name]; - + // - // If the `store` doesn't have a `saveSync` method, - // just ignore it and continue. + // If the `store` doesn't have a `saveSync` method, + // just ignore it and continue. // return store.saveSync ? store.saveSync() : null; } - + function saveStore(name, next) { var store = self.stores[name]; - + // - // If the `store` doesn't have a `save` or saveSync` - // method(s), just ignore it and continue. + // If the `store` doesn't have a `save` or saveSync` + // method(s), just ignore it and continue. // if (!store.save && !store.saveSync) { return next(); } - + 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(names.map(saveStoreSync)); } - + async.map(names, saveStore, function (err, objs) { return err ? callback(err) : callback(); - }); + }); }; // @@ -444,7 +444,7 @@ Provider.prototype._execute = function (action, syncLength /* [arguments] */) { destructive = ['set', 'clear', 'merge'].indexOf(action) !== -1, self = this, response; - + function runAction (name, next) { var store = self.stores[name]; @@ -456,7 +456,7 @@ Provider.prototype._execute = function (action, syncLength /* [arguments] */) { ? store[action].apply(store, args.concat(next)) : next(null, store[action].apply(store, args)); } - + if (callback) { return async.forEach(Object.keys(this.stores), runAction, function (err) { return err ? callback(err) : callback(); @@ -475,7 +475,7 @@ Provider.prototype._execute = function (action, syncLength /* [arguments] */) { response = store[action].apply(store, args); } }); - + return response; } @@ -486,6 +486,6 @@ function onError(err, callback) { if (callback) { return callback(err); } - + throw err; } \ No newline at end of file diff --git a/lib/nconf/stores/argv.js b/lib/nconf/stores/argv.js index befad1d..3841f7d 100644 --- a/lib/nconf/stores/argv.js +++ b/lib/nconf/stores/argv.js @@ -4,10 +4,10 @@ * (C) 2011, Nodejitsu Inc. * */ - + var util = require('util'), Memory = require('./memory').Memory; - + // // ### function Argv (options) // #### @options {Object} Options for this instance. @@ -36,26 +36,26 @@ Argv.prototype.loadSync = function () { // // ### 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. // Argv.prototype.loadArgv = function () { - var self = this, + var self = this, argv; - + argv = typeof this.options === 'object' ? require('optimist')(process.argv.slice(2)).options(this.options).argv : require('optimist')(process.argv.slice(2)).argv; - + if (!argv) { return; } - + this.readOnly = false; Object.keys(argv).forEach(function (key) { self.set(key, argv[key]); }); - + this.readOnly = true; return this.store; }; \ No newline at end of file diff --git a/lib/nconf/stores/env.js b/lib/nconf/stores/env.js index cae922b..936d45a 100644 --- a/lib/nconf/stores/env.js +++ b/lib/nconf/stores/env.js @@ -4,10 +4,10 @@ * (C) 2011, Nodejitsu Inc. * */ - + var util = require('util'), Memory = require('./memory').Memory; - + // // ### function Env (options) // #### @options {Object} Options for this instance. @@ -40,14 +40,14 @@ Env.prototype.loadSync = function () { // Env.prototype.loadEnv = function () { var self = this; - + this.readOnly = false; Object.keys(process.env).filter(function (key) { return !self.options.length || self.options.indexOf(key) !== -1; }).forEach(function (key) { self.set(key, process.env[key]); }); - + this.readOnly = true; return this.store; }; diff --git a/lib/nconf/stores/file.js b/lib/nconf/stores/file.js index 5d9de29..5620ee1 100644 --- a/lib/nconf/stores/file.js +++ b/lib/nconf/stores/file.js @@ -11,7 +11,7 @@ var fs = require('fs'), formats = require('../formats'), Memory = require('./memory').Memory, existsSync = fs.existsSync || path.existsSync; - + // // ### function File (options) // #### @options {Object} Options for this instance @@ -30,7 +30,7 @@ var File = exports.File = function (options) { this.dir = options.dir || process.cwd(); this.format = options.format || formats.json; this.json_spacing = options.json_spacing || 2; - + if (options.search) { this.search(this.dir); } @@ -40,10 +40,10 @@ var File = exports.File = function (options) { util.inherits(File, Memory); // -// ### function save (value, callback) +// ### function save (value, callback) // #### @value {Object} _Ignored_ Left here for consistency // #### @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`. // File.prototype.save = function (value, callback) { @@ -51,17 +51,17 @@ File.prototype.save = function (value, callback) { callback = value; value = null; } - + fs.writeFile(this.file, this.format.stringify(this.store, null, this.json_spacing), function (err) { return err ? callback(err) : callback(); }); }; // -// ### function saveSync (value, callback) +// ### function saveSync (value, callback) // #### @value {Object} _Ignored_ Left here for consistency // #### @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. // File.prototype.saveSync = function (value) { @@ -94,14 +94,14 @@ File.prototype.load = function (callback) { if (err) { return callback(err); } - + try { self.store = self.format.parse(data.toString()); } catch (ex) { return callback(new Error("Error parsing your JSON configuration file.")); } - + callback(null, self.store); }); }); @@ -138,8 +138,8 @@ File.prototype.loadSync = function () { // // ### function search (base) // #### @base {string} Base directory (or file) to begin searching for the target file. -// Attempts to find `this.file` by iteratively searching up the -// directory structure +// Attempts to find `this.file` by iteratively searching up the +// directory structure // File.prototype.search = function (base) { var looking = true, @@ -181,7 +181,7 @@ File.prototype.search = function (base) { return false; } } - + while (looking) { // // Iteratively look up the directory structure from `base` @@ -210,7 +210,7 @@ File.prototype.search = function (base) { // Ignore errors // } - + looking = false; } } @@ -222,6 +222,6 @@ File.prototype.search = function (base) { // the search was unsuccessful use the original value for `this.file`. // this.file = fullpath || this.file; - + return fullpath; }; diff --git a/lib/nconf/stores/literal.js b/lib/nconf/stores/literal.js index 55c64cc..c7c1752 100644 --- a/lib/nconf/stores/literal.js +++ b/lib/nconf/stores/literal.js @@ -5,12 +5,12 @@ * */ -var util = require('util'), +var util = require('util'), Memory = require('./memory').Memory var Literal = exports.Literal = function Literal (options) { Memory.call(this, options); - + options = options || {} this.type = 'literal'; this.readOnly = true; diff --git a/lib/nconf/stores/memory.js b/lib/nconf/stores/memory.js index 1d25cd3..e075d7f 100644 --- a/lib/nconf/stores/memory.js +++ b/lib/nconf/stores/memory.js @@ -13,7 +13,7 @@ var common = require('../common'); // Constructor function for the Memory nconf store which maintains // 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) { options = options || {}; @@ -22,7 +22,7 @@ var Memory = exports.Memory = function (options) { this.mtimes = {}; this.readOnly = false; this.loadFrom = options.loadFrom || null; - + if (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). // Memory.prototype.get = function (key) { - var target = this.store, + var target = this.store, path = common.path(key); // @@ -45,7 +45,7 @@ Memory.prototype.get = function (key) { if (!(target && key in target)) { return; } - + target = target[key]; if (path.length === 0) { return target; @@ -63,15 +63,15 @@ Memory.prototype.set = function (key, value) { if (this.readOnly) { return false; } - - var target = this.store, + + var target = this.store, path = common.path(key); - + // // Update the `mtime` (modified time) of the key // this.mtimes[key] = Date.now(); - + // // 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') { target[key] = {}; } - + target = target[key]; } - + // Set the specified value in the nested JSON structure key = path.shift(); target[key] = value; @@ -99,15 +99,15 @@ Memory.prototype.clear = function (key) { if (this.readOnly) { return false; } - - var target = this.store, + + var target = this.store, path = common.path(key); - + // // Remove the key from the set of `mtimes` (modified times) // delete this.mtimes[key]; - + // // Scope into the object to get the appropriate nested context // @@ -116,10 +116,10 @@ Memory.prototype.clear = function (key) { if (!target[key]) { return; } - + target = target[key]; } - + // Delete the key from the nested JSON structure key = path.shift(); delete target[key]; @@ -138,7 +138,7 @@ Memory.prototype.merge = function (key, value) { if (this.readOnly) { return false; } - + // // If the key is not an `Object` or is an `Array`, // 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)) { return this.set(key, value); } - + var self = this, - target = this.store, + target = this.store, path = common.path(key), fullKey = key; - + // // Update the `mtime` (modified time) of the key // this.mtimes[key] = Date.now(); - + // // Scope into the object to get the appropriate nested context // @@ -165,13 +165,13 @@ Memory.prototype.merge = function (key, value) { if (!target[key]) { target[key] = {}; } - + target = target[key]; } // Set the specified value in the nested JSON structure key = path.shift(); - + // // If the current value at the key target is not an `Object`, // 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; return true; } - + return Object.keys(value).every(function (nested) { return self.merge(fullKey + ':' + nested, value[nested]); }); @@ -195,7 +195,7 @@ Memory.prototype.reset = function () { if (this.readOnly) { return false; } - + this.mtimes = {}; this.store = {}; return true; diff --git a/usage.js b/usage.js index b7c30ca..ebd5f28 100644 --- a/usage.js +++ b/usage.js @@ -18,7 +18,7 @@ var single = new nconf.Provider({ // // Configure the provider with multiple hierarchical stores -// representing `user` and `global` configuration values. +// representing `user` and `global` configuration values. // var multiple = new nconf.Provider({ stores: [