-
Notifications
You must be signed in to change notification settings - Fork 567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow property files to be node modules that export objects #89
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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} | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See my outdated comment #89 (review) for an explanation. I think JSON error handling worked fine, but for all the kinds of errors that can be made in node modules the extra error detail is helpful. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JSON error example with new... file name is in the existing e.message:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perfect, thanks! |
||
} | ||
|
||
if (deep) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this module needed? Is there an issue with relative paths in
require()
on line 43?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, the node resole alg. is basically (check me on this...)
There's no part of it really that's "start at cwd() and give me file some/path relative to that".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could leave that up to the end user to make process.cwd() part of the glob pattern, too, I suppose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok, I see now. It's a difference in how fs.readFile and require resolve the file paths, and because we are changing how we get the file contents from fs to require this is to ensue it is backwards compatible. Thank you for explanation and care to make sure this doesn't break backwards compatibility.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the traditional method is to use fs.readFileSync(__dirname + './my-file')). The __dirname global is baked into node.js - https://nodejs.org/docs/latest/api/modules.html#modules_dirname