-
Notifications
You must be signed in to change notification settings - Fork 452
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
Check for Node’s fs and path before trying to use them #339
base: master
Are you sure you want to change the base?
Changes from 1 commit
45b66e5
de43eb3
91e2307
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 |
---|---|---|
|
@@ -966,7 +966,7 @@ lrGeneratorMixin.generateCommonJSModule = function generateCommonJSModule (opt) | |
opt = typal.mix.call({}, this.options, opt); | ||
var moduleName = opt.moduleName || "parser"; | ||
var out = this.generateModule(opt) | ||
+ "\n\n\nif (typeof require !== 'undefined' && typeof exports !== 'undefined' && new Function('try{return this===global;}catch(e){return false;}')()) {" | ||
+ "\n\n\nif (typeof require !== 'undefined' && typeof exports !== 'undefined') {" | ||
+ "\nexports.parser = "+moduleName+";" | ||
+ "\nexports.Parser = "+moduleName+".Parser;" | ||
+ "\nexports.parse = function () { return "+moduleName+".parse.apply("+moduleName+", arguments); };" | ||
|
@@ -1280,7 +1280,12 @@ function commonjsMain (args) { | |
console.log('Usage: '+args[0]+' FILE'); | ||
process.exit(1); | ||
} | ||
var source = require('fs').readFileSync(require('path').normalize(args[1]), "utf8"); | ||
var source = null; | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
if (typeof fs !== 'undefined' && fs !== null && typeof path !== 'undefined' && path !== null) { | ||
source = fs.readFileSync(path.normalize(args[1]), "utf8"); | ||
} | ||
return exports.parser.parse(source); | ||
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. And given that you want to code this defensively, wouldn't it be better to code it as a feature detect, i.e. along the lines of Besides, now that you start with an empty string a 'default input if fs/path don't exist in my env', which would very probably throw a parse error (on empty input) anyway, why the
That would at least make the CommonJS interface usable in both situations and add meaning to your checks; right now, all I see is a change in the way this will crash/fail. 😕 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. This is getting included inside the browser-based CoffeeScript compiler. The use case for these checks is when someone is using the compiler in a project that has require.js, which basically simulates a CommonJS environment. See jashkenas/coffeescript#4391. I’m trying to make things not crash in such an environment. Perhaps the |
||
} | ||
|
||
|
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.
Can
exports.parser.parse(source)
(a few lines below) handlesource === null
?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.
Nope, it can't. Expected input is string, always. (... I now see you tweaked for that already.)