re-enabling the --no-wrap flag, and cleaning up an unused method in command_line

This commit is contained in:
Jeremy Ashkenas 2010-02-21 13:48:38 -05:00
parent f679590bef
commit bea40a7a92
4 changed files with 16 additions and 40 deletions

View File

@ -1,11 +1,11 @@
(function(){
var BANNER, SWITCHES, coffee, compile, compile_script, compile_scripts, fs, lint, option_parser, options, optparse, parse_options, path, sources, usage, version, watch_scripts, write_js;
var BANNER, SWITCHES, coffee, compile_script, compile_scripts, fs, lint, option_parser, options, optparse, parse_options, path, sources, usage, version, watch_scripts, write_js;
fs = require('fs');
path = require('path');
coffee = require('coffee-script');
optparse = require('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 string from the command line'], ['-t', '--tokens', 'print the tokens that the lexer produces'], ['-tr', '--tree', 'print the parse tree that Jison produces'], ['-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 string from the command line'], ['-t', '--tokens', 'print the tokens that the lexer produces'], ['-tr', '--tree', 'print the parse tree that Jison produces'], ['-n', '--no-wrap', 'compile without the top-level function wrapper'], ['-v', '--version', 'display CoffeeScript version'], ['-h', '--help', 'display this help message']];
options = {};
sources = [];
option_parser = null;
@ -45,26 +45,6 @@
puts("CoffeeScript version " + coffee.VERSION);
return process.exit(0);
};
// Compile a single source file to JavaScript.
compile = function compile(script, source) {
source = source || 'error';
options = {};
if (options.no_wrap) {
options.no_wrap = true;
}
if (options.globals) {
options.globals = true;
}
try {
return CoffeeScript.compile(script, options);
} catch (error) {
process.stdio.writeError(source + ': ' + error.toString());
if (!(options.watch)) {
process.exit(1);
}
return null;
}
};
// Compiles the source CoffeeScript, returning the desired JavaScript, tokens,
// or JSLint results.
compile_scripts = function compile_scripts() {
@ -80,15 +60,18 @@
// Compile a single source script, containing the given code, according to the
// requested options. Both compile_scripts and watch_scripts share this method.
compile_script = function compile_script(source, code) {
var js, opts;
var js, o, opts;
opts = options;
o = opts.no_wrap ? {
no_wrap: true
} : {};
try {
if (opts.tokens) {
return coffee.print_tokens(coffee.tokenize(code));
} else if (opts.tree) {
return puts(coffee.tree(code).toString());
} else {
js = coffee.compile(code);
js = coffee.compile(code, o);
if (opts.run) {
return eval(js);
} else if (opts.print) {
@ -186,6 +169,9 @@
oparser.add('tree', function() {
return opts.tree = true;
});
oparser.add('no-wrap', function() {
return opts.no_wrap = true;
});
oparser.add('help', (function(__this) {
var __func = function() {
return usage();

View File

@ -66,7 +66,7 @@
};
// Private:
// Regex matchers for option flags.
LONG_FLAG = /^(--\w+)/;
LONG_FLAG = /^(--[\w\-]+)/;
SHORT_FLAG = /^(-\w+)/;
OPTIONAL = /\[(.+)\]/;
// Build rules from a list of valid switch tuples in the form:

View File

@ -20,6 +20,7 @@ SWITCHES: [
['-e', '--eval', 'compile a string from the command line']
['-t', '--tokens', 'print the tokens that the lexer produces']
['-tr','--tree', 'print the parse tree that Jison produces']
['-n', '--no-wrap', 'compile without the top-level function wrapper']
['-v', '--version', 'display CoffeeScript version']
['-h', '--help', 'display this help message']
]
@ -54,19 +55,6 @@ version: ->
puts "CoffeeScript version " + coffee.VERSION
process.exit 0
# Compile a single source file to JavaScript.
compile: (script, source) ->
source ||= 'error'
options: {}
options.no_wrap: true if options.no_wrap
options.globals: true if options.globals
try
CoffeeScript.compile(script, options)
catch error
process.stdio.writeError(source + ': ' + error.toString())
process.exit 1 unless options.watch
null
# Compiles the source CoffeeScript, returning the desired JavaScript, tokens,
# or JSLint results.
compile_scripts: ->
@ -79,11 +67,12 @@ compile_scripts: ->
# requested options. Both compile_scripts and watch_scripts share this method.
compile_script: (source, code) ->
opts: options
o: if opts.no_wrap then {no_wrap: true} else {}
try
if opts.tokens then coffee.print_tokens coffee.tokenize code
else if opts.tree then puts coffee.tree(code).toString()
else
js: coffee.compile code
js: coffee.compile code, o
if opts.run then eval js
else if opts.print then puts js
else if opts.lint then lint js
@ -131,6 +120,7 @@ parse_options: ->
oparser.add 'eval', -> opts.eval: true
oparser.add 'tokens', -> opts.tokens: true
oparser.add 'tree', -> opts.tree: true
oparser.add 'no-wrap', -> opts.no_wrap: true
oparser.add 'help', => usage()
oparser.add 'version', => version()

View File

@ -45,7 +45,7 @@ op::help: ->
# Private:
# Regex matchers for option flags.
LONG_FLAG: /^(--\w+)/
LONG_FLAG: /^(--[\w\-]+)/
SHORT_FLAG: /^(-\w+)/
OPTIONAL: /\[(.+)\]/