Skip to content

Commit

Permalink
[api] Added nconf.loadFiles() method
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Aug 23, 2011
1 parent a6533aa commit e8904e9
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion lib/nconf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

//
Expand Down Expand Up @@ -35,6 +37,38 @@ nconf.key = function () {
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
//
Expand Down

0 comments on commit e8904e9

Please sign in to comment.