things are in motion -- bin/node_coffee is the new JS-only command line ... it can pass some of the tests

This commit is contained in:
Jeremy Ashkenas
2010-02-11 01:57:33 -05:00
parent f761c25dcd
commit 872b36c11d
83 changed files with 8312 additions and 226 deletions

88
src/command_line.coffee Normal file
View File

@@ -0,0 +1,88 @@
optparse: require('./../../vendor/optparse-js/src/optparse')
posix: require 'posix'
coffee: require 'coffee-script'
BANNER: '''
coffee compiles CoffeeScript source files into JavaScript.
Usage:
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']
['-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
# The CommandLine handles all of the functionality of the `coffee` utility.
exports.run: ->
@parse_options()
@compile_scripts()
this
# The "--help" usage message.
exports.usage: ->
puts '\n' + @option_parser.toString() + '\n'
process.exit 0
# The "--version" message.
exports.version: ->
puts "CoffeeScript version " + coffee.VERSION
process.exit 0
# Compile a single source file to JavaScript.
exports.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.
exports.compile_scripts: ->
return unless source: @sources.shift()
posix.cat(source).addCallback (code) =>
js: coffee.compile code
return eval js if @options.run
return puts js if @options.print
exports.compile_scripts()
# Use OptionParser for all the options.
exports.parse_options: ->
opts: @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 'help', => @usage()
oparser.add 'version', => @version()
paths: oparser.parse(process.ARGV)
@sources: paths[2...paths.length]