Skip to content

Commit

Permalink
[wip] refactoring to simplify code base
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeck committed Sep 26, 2012
1 parent da39d3c commit 4cc1357
Show file tree
Hide file tree
Showing 15 changed files with 360 additions and 422 deletions.
28 changes: 13 additions & 15 deletions lib/nconf.js
Expand Up @@ -6,34 +6,32 @@
*/

var fs = require('fs'),
async = require('async'),
common = require('./nconf/common'),
Provider = require('./nconf/provider').Provider,
nconf = module.exports = new Provider();

//
// Expose the version from the package.json using `pkginfo`.
//
require('pkginfo')(module, 'version');

//
// Setup all stores as lazy-loaded getters.
//
nconf.engines = {};
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];
Object.defineProperty(nconf.engines, file.replace('.js', ''), {
get: function () {
return require('./nconf/stores/' + file);
},
enumerable: true
});
Object.defineProperty(nconf, file.replace('.js', '').replace(/./, function (c) {return c.toUpperCase()}), {
get: function () {
return require('./nconf/stores/' + file);
},
enumerable: true
});
});

//
// Expose the various components included with nconf
//
nconf.key = common.key;
nconf.path = common.path;
nconf.loadFiles = common.loadFiles;
nconf.loadFilesSync = common.loadFilesSync;
nconf.common = common;
nconf.formats = require('./nconf/formats');
nconf.Provider = Provider;
53 changes: 37 additions & 16 deletions lib/nconf/common.js
Expand Up @@ -7,8 +7,7 @@

var fs = require('fs'),
async = require('async'),
formats = require('./formats'),
Memory = require('./stores/memory').Memory;
Memory = require('./stores/memory');

var common = exports;

Expand All @@ -20,9 +19,39 @@ var common = exports;
// '' should still be respected as a path.
//
common.path = function (key) {
return key == null ? [] : key.split(':');
return key == null ? [] : ('' + key).split(':');
};

common.scope = function (path, target) {
//
// Scope into the object to get the appropriate nested context
//
for (var i = 0; i < path.length; i++) {
key = path[i];
if (typeof target === 'object' && key in target) {
target = target[key];
continue;
}
return void 0;
}
return target;
}

common.ensure = function (path, target) {
//
// Scope into the object to get the appropriate nested context
//
for (var i = 0; i < path.length - 1; i++) {
key = path[i];
if (target && !(key in target)) {
target[key] = {};
}
target = target[key];
}

return target;
}

//
// ### function key (arguments)
// Returns a `:` joined string from the `arguments`.
Expand All @@ -39,7 +68,8 @@ common.key = function () {
//
common.loadFiles = function (files, callback) {
if (!files) {
return callback(null, {});
callback(null, {});
return;
}

var options = Array.isArray(files) ? { files: files } : files;
Expand All @@ -48,7 +78,7 @@ common.loadFiles = function (files, callback) {
// Set the default JSON format if not already
// specified
//
options.format = options.format || formats.json;
options.format = options.format || require('../nconf').formats.json;

function parseFile (file, next) {
fs.readFile(file, function (err, data) {
Expand All @@ -70,15 +100,15 @@ common.loadFiles = function (files, callback) {
//
common.loadFilesSync = function (files) {
if (!files) {
return;
return void 0;
}

//
// Set the default JSON format if not already
// specified
//
var options = Array.isArray(files) ? { files: files } : files;
options.format = options.format || formats.json;
options.format = options.format || require('../nconf').formats.json;

return common.merge(files.map(function (file) {
return options.format.parse(fs.readFileSync(file, 'utf8'));
Expand All @@ -102,12 +132,3 @@ common.merge = function (objs) {

return store.store;
};

//
// ### function capitalize (str)
// #### @str {string} String to capitalize
// Capitalizes the specified `str`.
//
common.capitalize = function (str) {
return str && str[0].toUpperCase() + str.slice(1);
};
6 changes: 2 additions & 4 deletions lib/nconf/formats.js
Expand Up @@ -7,13 +7,11 @@

var ini = require('ini');

var formats = exports;

//
// ### @json
// Standard JSON format which pretty prints `.stringify()`.
//
formats.json = {
exports.json = {
stringify: function (obj, replacer, spacing) {
return JSON.stringify(obj, replacer || null, spacing || 2)
},
Expand All @@ -25,4 +23,4 @@ formats.json = {
// Standard INI format supplied from the `ini` module
// http://en.wikipedia.org/wiki/INI_file
//
formats.ini = ini;
exports.ini = ini;

0 comments on commit 4cc1357

Please sign in to comment.