2011-08-28 12:50:26 +00:00
|
|
|
/*
|
|
|
|
* utils.js: Utility functions for the nconf module.
|
|
|
|
*
|
2011-11-24 05:33:08 +00:00
|
|
|
* (C) 2011, Nodejitsu Inc.
|
2011-08-28 12:50:26 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
var fs = require('fs'),
|
|
|
|
async = require('async'),
|
|
|
|
formats = require('./formats'),
|
2011-09-19 20:51:17 +00:00
|
|
|
Memory = require('./stores/memory').Memory;
|
2011-08-28 12:50:26 +00:00
|
|
|
|
|
|
|
var common = exports;
|
|
|
|
|
|
|
|
//
|
2011-09-16 10:49:47 +00:00
|
|
|
// ### function path (key)
|
|
|
|
// #### @key {string} The ':' delimited key to split
|
|
|
|
// Returns a fully-qualified path to a nested nconf key.
|
|
|
|
//
|
|
|
|
common.path = function (key) {
|
|
|
|
return key.split(':');
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// ### function key (arguments)
|
|
|
|
// Returns a `:` joined string from the `arguments`.
|
|
|
|
//
|
|
|
|
common.key = function () {
|
|
|
|
return Array.prototype.slice.call(arguments).join(':');
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// ### function loadFiles (files, callback)
|
2011-08-28 12:50:26 +00:00
|
|
|
// #### @files {Object|Array} List of files (or settings object) to load.
|
|
|
|
// #### @callback {function} Continuation to respond to when complete.
|
|
|
|
// Loads all the data in the specified `files`.
|
|
|
|
//
|
|
|
|
common.loadFiles = function (files, callback) {
|
|
|
|
if (!files) {
|
|
|
|
return callback(null, {});
|
|
|
|
}
|
|
|
|
|
2011-09-16 10:49:47 +00:00
|
|
|
var options = Array.isArray(files) ? { files: files } : files;
|
2011-08-28 12:50:26 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Set the default JSON format if not already
|
|
|
|
// specified
|
|
|
|
//
|
|
|
|
options.format = options.format || formats.json;
|
|
|
|
|
2011-09-16 10:49:47 +00:00
|
|
|
function parseFile (file, next) {
|
2011-08-28 12:50:26 +00:00
|
|
|
fs.readFile(file, function (err, data) {
|
2011-09-16 10:49:47 +00:00
|
|
|
return !err
|
|
|
|
? next(null, options.format.parse(data.toString()))
|
|
|
|
: next(err);
|
|
|
|
});
|
|
|
|
}
|
2011-08-28 12:50:26 +00:00
|
|
|
|
2011-09-16 10:49:47 +00:00
|
|
|
async.map(files, parseFile, function (err, objs) {
|
|
|
|
return err ? callback(err) : callback(null, common.merge(objs));
|
|
|
|
});
|
|
|
|
};
|
2011-08-28 12:50:26 +00:00
|
|
|
|
2011-09-16 10:49:47 +00:00
|
|
|
//
|
|
|
|
// ### function loadFilesSync (files)
|
|
|
|
// #### @files {Object|Array} List of files (or settings object) to load.
|
|
|
|
// Loads all the data in the specified `files` synchronously.
|
|
|
|
//
|
|
|
|
common.loadFilesSync = function (files) {
|
|
|
|
if (!files) {
|
|
|
|
return;
|
2011-08-28 12:50:26 +00:00
|
|
|
}
|
|
|
|
|
2011-09-16 10:49:47 +00:00
|
|
|
//
|
|
|
|
// Set the default JSON format if not already
|
|
|
|
// specified
|
|
|
|
//
|
|
|
|
var options = Array.isArray(files) ? { files: files } : files;
|
|
|
|
options.format = options.format || formats.json;
|
|
|
|
|
|
|
|
return common.merge(files.map(function (file) {
|
|
|
|
return options.format.parse(fs.readFileSync(file, 'utf8'));
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// ### function merge (objs)
|
|
|
|
// #### @objs {Array} Array of object literals to merge
|
|
|
|
// Merges the specified `objs` using a temporary instance
|
|
|
|
// of `stores.Memory`.
|
|
|
|
//
|
|
|
|
common.merge = function (objs) {
|
|
|
|
var store = new Memory();
|
|
|
|
|
|
|
|
objs.forEach(function (obj) {
|
|
|
|
Object.keys(obj).forEach(function (key) {
|
|
|
|
store.merge(key, obj[key]);
|
|
|
|
});
|
2011-08-28 12:50:26 +00:00
|
|
|
});
|
2011-09-16 10:49:47 +00:00
|
|
|
|
|
|
|
return store.store;
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// ### function capitalize (str)
|
|
|
|
// #### @str {string} String to capitalize
|
|
|
|
// Capitalizes the specified `str`.
|
|
|
|
//
|
|
|
|
common.capitalize = function (str) {
|
|
|
|
return str && str[0].toUpperCase() + str.slice(1);
|
2011-08-28 12:50:26 +00:00
|
|
|
};
|