Skip to content

Commit

Permalink
Allow property files to be node modules that export objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Reichenberg committed May 21, 2018
1 parent 5946cb9 commit 10331ae
Show file tree
Hide file tree
Showing 5 changed files with 3,133 additions and 42 deletions.
17 changes: 10 additions & 7 deletions lib/utils/combineJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@
* and limitations under the License.
*/

var fs = require('fs'),
glob = require('glob'),
var glob = require('glob'),
deepExtend = require('./deepExtend'),
extend = require('lodash/extend');
extend = require('lodash/extend'),
path = require('path'),
resolveCwd = require('resolve-cwd');

/**
* Takes an array of json files and merges
* them together. Optionally does a deep extend.
* @private
* @param {String[]} arr - Array of paths to json files
* @param {String[]} arr - Array of paths to json (or node modules that export objects) files
* @param {Boolean} [deep=false] - If it should perform a deep merge
* @param {Function} collision - A function to be called when a name collision happens that isn't a normal deep merge of objects
* @returns {Object}
Expand All @@ -35,12 +36,14 @@ function combineJSON(arr, deep, collision) {
}

for (i = 0; i < files.length; i++) {
var file_content = fs.readFileSync(files[i], 'utf8');
var resolvedPath = resolveCwd(path.isAbsolute(files[i]) ? files[i] : '.' + path.sep + files[i]);
var file_content;

try {
file_content = JSON.parse(file_content);
file_content = require(resolvedPath);
} catch (e) {
throw new SyntaxError(files[i] + ' - failed to parse JSON: ' + e.message);
e.message = 'Failed to load or parse JSON or JS Object: ' + e.message;
throw e;
}

if (deep) {
Expand Down
Loading

0 comments on commit 10331ae

Please sign in to comment.