2011-03-31 06:32:47 +00:00
|
|
|
/*
|
|
|
|
* nconf.js: Top-level include for the nconf module
|
|
|
|
*
|
|
|
|
* (C) 2011, Charlie Robbins
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-08-23 10:53:05 +00:00
|
|
|
var fs = require('fs'),
|
|
|
|
async = require('async'),
|
|
|
|
Provider = require('./nconf/provider').Provider,
|
2011-05-14 05:47:26 +00:00
|
|
|
nconf = module.exports = Object.create(Provider.prototype);
|
2011-03-31 06:32:47 +00:00
|
|
|
|
2011-04-19 21:34:42 +00:00
|
|
|
//
|
2011-05-17 02:44:46 +00:00
|
|
|
// Use the memory engine by default.
|
2011-04-19 21:34:42 +00:00
|
|
|
//
|
2011-05-17 02:44:46 +00:00
|
|
|
nconf.use('memory');
|
|
|
|
|
|
|
|
//
|
2011-06-05 05:39:39 +00:00
|
|
|
// Expose the version from the package.json using `pkginfo`.
|
2011-05-17 02:44:46 +00:00
|
|
|
//
|
2011-06-08 03:45:01 +00:00
|
|
|
require('pkginfo')(module, 'version');
|
2011-04-19 21:34:42 +00:00
|
|
|
|
|
|
|
//
|
2011-05-14 05:47:26 +00:00
|
|
|
// ### function path (key)
|
|
|
|
// #### @key {string} The ':' delimited key to split
|
|
|
|
// Returns a fully-qualified path to a nested nconf key.
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
2011-05-14 05:47:26 +00:00
|
|
|
nconf.path = function (key) {
|
|
|
|
return key.split(':');
|
2011-04-02 07:03:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// ### function key (arguments)
|
|
|
|
// Returns a `:` joined string from the `arguments`.
|
|
|
|
//
|
|
|
|
nconf.key = function () {
|
|
|
|
return Array.prototype.slice.call(arguments).join(':');
|
|
|
|
};
|
|
|
|
|
2011-08-23 10:53:05 +00:00
|
|
|
//
|
|
|
|
// ### function loadFiles (files)
|
|
|
|
// #### @files {Array} List of files to load.
|
|
|
|
// Loads all the data in the specified `files`.
|
|
|
|
//
|
|
|
|
nconf.loadFiles = function (files, callback) {
|
|
|
|
if (!files) {
|
|
|
|
return callback(null, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
var allData = {};
|
|
|
|
|
|
|
|
function loadFile (file, next) {
|
|
|
|
fs.readFile(file, function (err, data) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
data = JSON.parse(data.toString());
|
|
|
|
Object.keys(data).forEach(function (key) {
|
|
|
|
allData[key] = data[key];
|
|
|
|
});
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async.forEach(files, loadFile, function (err) {
|
|
|
|
return err ? callback(err) : callback(null, allData);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-04-02 07:03:16 +00:00
|
|
|
//
|
2011-05-14 05:47:26 +00:00
|
|
|
// Expose the various components included with nconf
|
2011-04-02 23:17:04 +00:00
|
|
|
//
|
2011-05-14 05:47:26 +00:00
|
|
|
nconf.stores = require('./nconf/stores');
|
|
|
|
nconf.Provider = Provider;
|