adding the ability to write out compiled coffeescript to the command line

This commit is contained in:
Jeremy Ashkenas
2010-02-12 22:59:21 -05:00
parent 1552470413
commit e02bedcf82
4 changed files with 38 additions and 21 deletions

View File

@@ -1,8 +1,9 @@
(function(){ (function(){
var BANNER, SWITCHES, WATCH_INTERVAL, coffee, optparse, posix; var BANNER, SWITCHES, WATCH_INTERVAL, coffee, optparse, path, posix;
optparse = require('./../../vendor/optparse-js/src/optparse');
posix = require('posix'); posix = require('posix');
path = require('path');
coffee = require('coffee-script'); 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"; 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']]; 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; WATCH_INTERVAL = 0.5;
@@ -53,10 +54,20 @@
} }
opts = this.options; opts = this.options;
return posix.cat(source).addCallback(function(code) { 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(); 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. // Use OptionParser for all the options.
exports.parse_options = function parse_options() { exports.parse_options = function parse_options() {
var oparser, opts, paths; var oparser, opts, paths;
@@ -70,7 +81,7 @@
oparser.add('run', function() { oparser.add('run', function() {
return opts.run = true; return opts.run = true;
}); });
oparser.add('output', function(dir) { oparser.add('output', function(n, dir) {
return opts.output = dir; return opts.output = dir;
}); });
oparser.add('watch', function() { oparser.add('watch', function() {

View File

@@ -558,8 +558,7 @@
// puts parser.generate() // puts parser.generate()
posix = require('posix'); posix = require('posix');
parser_path = 'lib/coffee_script/parser.js'; parser_path = 'lib/coffee_script/parser.js';
posix.unlink(parser_path); posix.open(parser_path, process.O_CREAT | process.O_WRONLY | process.O_TRUNC, 0755).addCallback(function(fd) {
posix.open(parser_path, process.O_CREAT | process.O_WRONLY, 0755).addCallback(function(fd) {
return posix.write(fd, js); return posix.write(fd, js);
}); });
})(); })();

View File

@@ -1,6 +1,7 @@
optparse: require('./../../vendor/optparse-js/src/optparse')
posix: require 'posix' posix: require 'posix'
path: require 'path'
coffee: require 'coffee-script' coffee: require 'coffee-script'
optparse: require('./../../vendor/optparse-js/src/optparse')
BANNER: ''' BANNER: '''
coffee compiles CoffeeScript source files into JavaScript. 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.tree then puts coffee.tree(code).toString()
else if opts.run then eval coffee.compile code else if opts.run then eval coffee.compile code
else if opts.print then puts coffee.compile code else if opts.print then puts coffee.compile code
else exports.write_js source, coffee.compile code
exports.compile_scripts() 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. # Use OptionParser for all the options.
exports.parse_options: -> exports.parse_options: ->
@@ -75,18 +84,17 @@ exports.parse_options: ->
oparser: @option_parser: new optparse.OptionParser SWITCHES oparser: @option_parser: new optparse.OptionParser SWITCHES
oparser.add: oparser['on'] oparser.add: oparser['on']
oparser.add 'interactive', -> opts.interactive: true oparser.add 'interactive', -> opts.interactive: true
oparser.add 'run', -> opts.run: true oparser.add 'run', -> opts.run: true
oparser.add 'output', (dir) -> opts.output: dir oparser.add 'output', (n, dir) -> opts.output: dir
oparser.add 'watch', -> opts.watch: true oparser.add 'watch', -> opts.watch: true
oparser.add 'print', -> opts.print: true oparser.add 'print', -> opts.print: true
oparser.add 'lint', -> opts.lint: true oparser.add 'lint', -> opts.lint: true
oparser.add 'eval', -> opts.eval: true oparser.add 'eval', -> opts.eval: true
oparser.add 'tokens', -> opts.tokens: true oparser.add 'tokens', -> opts.tokens: true
oparser.add 'tree', -> opts.tree: true oparser.add 'tree', -> opts.tree: true
oparser.add 'help', => @usage() oparser.add 'help', => @usage()
oparser.add 'version', => @version() oparser.add 'version', => @version()
paths: oparser.parse(process.ARGV) paths: oparser.parse(process.ARGV)
@sources: paths[2...paths.length] @sources: paths[2...paths.length]

View File

@@ -456,6 +456,5 @@ js: parser.generate()
# puts parser.generate() # puts parser.generate()
posix: require 'posix' posix: require 'posix'
parser_path: 'lib/coffee_script/parser.js' parser_path: 'lib/coffee_script/parser.js'
posix.unlink parser_path posix.open(parser_path, process.O_CREAT | process.O_WRONLY | process.O_TRUNC, 0755).addCallback (fd) ->
posix.open(parser_path, process.O_CREAT | process.O_WRONLY, 0755).addCallback (fd) ->
posix.write(fd, js) posix.write(fd, js)