diff --git a/lib/nconf.js b/lib/nconf.js index e9b7eb0..34daed5 100644 --- a/lib/nconf.js +++ b/lib/nconf.js @@ -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); // @@ -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 //