[api] Added nconf.loadFiles()
method
This commit is contained in:
parent
a6533aa7bf
commit
e8904e95c6
1 changed files with 35 additions and 1 deletions
36
lib/nconf.js
36
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
|
||||
//
|
||||
|
|
Loading…
Reference in a new issue