[api] Added nconf.loadFiles() method

This commit is contained in:
indexzero 2011-08-23 06:53:05 -04:00
parent a6533aa7bf
commit e8904e95c6

View file

@ -5,7 +5,9 @@
* *
*/ */
var Provider = require('./nconf/provider').Provider, var fs = require('fs'),
async = require('async'),
Provider = require('./nconf/provider').Provider,
nconf = module.exports = Object.create(Provider.prototype); nconf = module.exports = Object.create(Provider.prototype);
// //
@ -35,6 +37,38 @@ nconf.key = function () {
return Array.prototype.slice.call(arguments).join(':'); return Array.prototype.slice.call(arguments).join(':');
}; };
//
// ### 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);
});
};
// //
// Expose the various components included with nconf // Expose the various components included with nconf
// //