1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

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(){ (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'); fs = require('fs');
path = require('path'); path = require('path');
coffee = require('coffee-script'); coffee = require('coffee-script');
optparse = require('optparse'); optparse = require('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 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 = {}; options = {};
sources = []; sources = [];
option_parser = null; option_parser = null;
@ -45,26 +45,6 @@
puts("CoffeeScript version " + coffee.VERSION); puts("CoffeeScript version " + coffee.VERSION);
return process.exit(0); 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, // Compiles the source CoffeeScript, returning the desired JavaScript, tokens,
// or JSLint results. // or JSLint results.
compile_scripts = function compile_scripts() { compile_scripts = function compile_scripts() {
@ -80,15 +60,18 @@
// Compile a single source script, containing the given code, according to the // Compile a single source script, containing the given code, according to the
// requested options. Both compile_scripts and watch_scripts share this method. // requested options. Both compile_scripts and watch_scripts share this method.
compile_script = function compile_script(source, code) { compile_script = function compile_script(source, code) {
var js, opts; var js, o, opts;
opts = options; opts = options;
o = opts.no_wrap ? {
no_wrap: true
} : {};
try { try {
if (opts.tokens) { if (opts.tokens) {
return coffee.print_tokens(coffee.tokenize(code)); return coffee.print_tokens(coffee.tokenize(code));
} else if (opts.tree) { } else if (opts.tree) {
return puts(coffee.tree(code).toString()); return puts(coffee.tree(code).toString());
} else { } else {
js = coffee.compile(code); js = coffee.compile(code, o);
if (opts.run) { if (opts.run) {
return eval(js); return eval(js);
} else if (opts.print) { } else if (opts.print) {
@ -186,6 +169,9 @@
oparser.add('tree', function() { oparser.add('tree', function() {
return opts.tree = true; return opts.tree = true;
}); });
oparser.add('no-wrap', function() {
return opts.no_wrap = true;
});
oparser.add('help', (function(__this) { oparser.add('help', (function(__this) {
var __func = function() { var __func = function() {
return usage(); return usage();

View file

@ -66,7 +66,7 @@
}; };
// Private: // Private:
// Regex matchers for option flags. // Regex matchers for option flags.
LONG_FLAG = /^(--\w+)/; LONG_FLAG = /^(--[\w\-]+)/;
SHORT_FLAG = /^(-\w+)/; SHORT_FLAG = /^(-\w+)/;
OPTIONAL = /\[(.+)\]/; OPTIONAL = /\[(.+)\]/;
// Build rules from a list of valid switch tuples in the form: // 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'] ['-e', '--eval', 'compile a string from the command line']
['-t', '--tokens', 'print the tokens that the lexer produces'] ['-t', '--tokens', 'print the tokens that the lexer produces']
['-tr','--tree', 'print the parse tree that Jison 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'] ['-v', '--version', 'display CoffeeScript version']
['-h', '--help', 'display this help message'] ['-h', '--help', 'display this help message']
] ]
@ -54,19 +55,6 @@ version: ->
puts "CoffeeScript version " + coffee.VERSION puts "CoffeeScript version " + coffee.VERSION
process.exit 0 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, # Compiles the source CoffeeScript, returning the desired JavaScript, tokens,
# or JSLint results. # or JSLint results.
compile_scripts: -> compile_scripts: ->
@ -79,11 +67,12 @@ compile_scripts: ->
# requested options. Both compile_scripts and watch_scripts share this method. # requested options. Both compile_scripts and watch_scripts share this method.
compile_script: (source, code) -> compile_script: (source, code) ->
opts: options opts: options
o: if opts.no_wrap then {no_wrap: true} else {}
try try
if opts.tokens then coffee.print_tokens coffee.tokenize code if opts.tokens then coffee.print_tokens coffee.tokenize code
else if opts.tree then puts coffee.tree(code).toString() else if opts.tree then puts coffee.tree(code).toString()
else else
js: coffee.compile code js: coffee.compile code, o
if opts.run then eval js if opts.run then eval js
else if opts.print then puts js else if opts.print then puts js
else if opts.lint then lint js else if opts.lint then lint js
@ -131,6 +120,7 @@ parse_options: ->
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 'no-wrap', -> opts.no_wrap: true
oparser.add 'help', => usage() oparser.add 'help', => usage()
oparser.add 'version', => version() oparser.add 'version', => version()

View file

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