diff --git a/lib/coffee_script/command_line.js b/lib/coffee_script/command_line.js index e55cb66e..ab8be60d 100644 --- a/lib/coffee_script/command_line.js +++ b/lib/coffee_script/command_line.js @@ -1,8 +1,9 @@ (function(){ - var BANNER, SWITCHES, WATCH_INTERVAL, coffee, optparse, posix; - optparse = require('./../../vendor/optparse-js/src/optparse'); + var BANNER, SWITCHES, WATCH_INTERVAL, coffee, optparse, path, posix; posix = require('posix'); + path = require('path'); coffee = require('coffee-script'); + optparse = require('./../../vendor/optparse-js/src/optparse'); BANNER = "coffee compiles CoffeeScript source files into JavaScript.\n\nUsage:\n coffee path/to/script.coffee"; SWITCHES = [['-i', '--interactive', 'run an interactive CoffeeScript REPL'], ['-r', '--run', 'compile and run a CoffeeScript'], ['-o', '--output [DIR]', 'set the directory for compiled JavaScript'], ['-w', '--watch', 'watch scripts for changes, and recompile'], ['-p', '--print', 'print the compiled JavaScript to stdout'], ['-l', '--lint', 'pipe the compiled JavaScript through JSLint'], ['-e', '--eval', 'compile a cli scriptlet or read from stdin'], ['-t', '--tokens', 'print the tokens that the lexer produces'], ['--tree', 'print the parse tree that Jison produces'], ['-n', '--no-wrap', 'raw output, no function safety wrapper'], ['-g', '--globals', 'attach all top-level variables as globals'], ['-v', '--version', 'display CoffeeScript version'], ['-h', '--help', 'display this help message']]; WATCH_INTERVAL = 0.5; @@ -53,10 +54,20 @@ } opts = this.options; return posix.cat(source).addCallback(function(code) { - opts.tokens ? puts(coffee.tokenize(code).join(' ')) : opts.tree ? puts(coffee.tree(code).toString()) : opts.run ? eval(coffee.compile(code)) : opts.print ? puts(coffee.compile(code)) : null; + opts.tokens ? puts(coffee.tokenize(code).join(' ')) : opts.tree ? puts(coffee.tree(code).toString()) : opts.run ? eval(coffee.compile(code)) : opts.print ? puts(coffee.compile(code)) : exports.write_js(source, coffee.compile(code)); return exports.compile_scripts(); }); }; + // Write out a JavaScript source file with the compiled code. + exports.write_js = function write_js(source, js) { + var dir, filename, js_path; + filename = path.basename(source, path.extname(source)) + '.js'; + dir = this.options.output || path.dirname(source); + js_path = path.join(dir, filename); + return posix.open(js_path, process.O_CREAT | process.O_WRONLY | process.O_TRUNC, 0755).addCallback(function(fd) { + return posix.write(fd, js); + }); + }; // Use OptionParser for all the options. exports.parse_options = function parse_options() { var oparser, opts, paths; @@ -70,7 +81,7 @@ oparser.add('run', function() { return opts.run = true; }); - oparser.add('output', function(dir) { + oparser.add('output', function(n, dir) { return opts.output = dir; }); oparser.add('watch', function() { diff --git a/lib/coffee_script/grammar.js b/lib/coffee_script/grammar.js index 88f147df..5a460b65 100644 --- a/lib/coffee_script/grammar.js +++ b/lib/coffee_script/grammar.js @@ -558,8 +558,7 @@ // puts parser.generate() posix = require('posix'); parser_path = 'lib/coffee_script/parser.js'; - posix.unlink(parser_path); - posix.open(parser_path, process.O_CREAT | process.O_WRONLY, 0755).addCallback(function(fd) { + posix.open(parser_path, process.O_CREAT | process.O_WRONLY | process.O_TRUNC, 0755).addCallback(function(fd) { return posix.write(fd, js); }); })(); \ No newline at end of file diff --git a/src/command_line.coffee b/src/command_line.coffee index f59cbf05..5ece9f79 100644 --- a/src/command_line.coffee +++ b/src/command_line.coffee @@ -1,6 +1,7 @@ -optparse: require('./../../vendor/optparse-js/src/optparse') posix: require 'posix' +path: require 'path' coffee: require 'coffee-script' +optparse: require('./../../vendor/optparse-js/src/optparse') BANNER: ''' coffee compiles CoffeeScript source files into JavaScript. @@ -66,8 +67,16 @@ exports.compile_scripts: -> else if opts.tree then puts coffee.tree(code).toString() else if opts.run then eval coffee.compile code else if opts.print then puts coffee.compile code + else exports.write_js source, coffee.compile code exports.compile_scripts() +# Write out a JavaScript source file with the compiled code. +exports.write_js: (source, js) -> + filename: path.basename(source, path.extname(source)) + '.js' + dir: @options.output or path.dirname(source) + js_path: path.join dir, filename + posix.open(js_path, process.O_CREAT | process.O_WRONLY | process.O_TRUNC, 0755).addCallback (fd) -> + posix.write(fd, js) # Use OptionParser for all the options. exports.parse_options: -> @@ -75,18 +84,17 @@ exports.parse_options: -> oparser: @option_parser: new optparse.OptionParser SWITCHES oparser.add: oparser['on'] - oparser.add 'interactive', -> opts.interactive: true - oparser.add 'run', -> opts.run: true - oparser.add 'output', (dir) -> opts.output: dir - oparser.add 'watch', -> opts.watch: true - oparser.add 'print', -> opts.print: true - oparser.add 'lint', -> opts.lint: true - oparser.add 'eval', -> opts.eval: true - oparser.add 'tokens', -> opts.tokens: true - oparser.add 'tree', -> opts.tree: true - oparser.add 'help', => @usage() - oparser.add 'version', => @version() + oparser.add 'interactive', -> opts.interactive: true + oparser.add 'run', -> opts.run: true + oparser.add 'output', (n, dir) -> opts.output: dir + oparser.add 'watch', -> opts.watch: true + oparser.add 'print', -> opts.print: true + oparser.add 'lint', -> opts.lint: true + oparser.add 'eval', -> opts.eval: true + oparser.add 'tokens', -> opts.tokens: true + oparser.add 'tree', -> opts.tree: true + oparser.add 'help', => @usage() + oparser.add 'version', => @version() paths: oparser.parse(process.ARGV) @sources: paths[2...paths.length] - diff --git a/src/grammar.coffee b/src/grammar.coffee index 4140008c..50ec78d4 100644 --- a/src/grammar.coffee +++ b/src/grammar.coffee @@ -456,6 +456,5 @@ js: parser.generate() # puts parser.generate() posix: require 'posix' parser_path: 'lib/coffee_script/parser.js' -posix.unlink parser_path -posix.open(parser_path, process.O_CREAT | process.O_WRONLY, 0755).addCallback (fd) -> +posix.open(parser_path, process.O_CREAT | process.O_WRONLY | process.O_TRUNC, 0755).addCallback (fd) -> posix.write(fd, js)