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(){
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() {