2009-12-16 22:42:53 -05:00
|
|
|
require 'optparse'
|
|
|
|
require 'fileutils'
|
|
|
|
require 'open3'
|
|
|
|
require File.expand_path(File.dirname(__FILE__) + '/../coffee-script')
|
|
|
|
|
|
|
|
module CoffeeScript
|
|
|
|
|
2009-12-26 08:57:13 -08:00
|
|
|
# The CommandLine handles all of the functionality of the `coffee`
|
2009-12-17 21:57:21 -05:00
|
|
|
# utility.
|
2009-12-16 22:42:53 -05:00
|
|
|
class CommandLine
|
|
|
|
|
|
|
|
BANNER = <<-EOS
|
2009-12-26 08:57:13 -08:00
|
|
|
coffee compiles CoffeeScript source files into JavaScript.
|
2009-12-16 22:42:53 -05:00
|
|
|
|
|
|
|
Usage:
|
2009-12-26 08:57:13 -08:00
|
|
|
coffee path/to/script.coffee
|
2009-12-16 22:42:53 -05:00
|
|
|
EOS
|
|
|
|
|
2009-12-17 21:57:21 -05:00
|
|
|
# Seconds to pause between checks for changed source files.
|
2009-12-17 20:37:39 -05:00
|
|
|
WATCH_INTERVAL = 0.5
|
|
|
|
|
2009-12-28 01:16:57 -08:00
|
|
|
# Command to execute in Narwhal
|
2009-12-28 16:23:48 -05:00
|
|
|
PACKAGE = File.expand_path(File.dirname(__FILE__) + '/../..')
|
2009-12-28 01:49:07 -08:00
|
|
|
LAUNCHER = "narwhal -p #{PACKAGE} -e 'require(\"coffee-script\").run(system.args);'"
|
fixing paths for running
coffee compiles CoffeeScript source files into JavaScript.
Usage:
coffee path/to/script.coffee
-i, --interactive run a CoffeeScript REPL (requires Narwhal)
-r, --run compile and run a script (requires Narwhal)
-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
-v, --verbose print at every step of code generation
-n, --no-wrap raw output, no safety wrapper or vars
--install-bundle install the CoffeeScript TextMate bundle
--version display CoffeeScript version
-h, --help display this help message outside of the coffee-script directory
2009-12-27 12:43:05 -08:00
|
|
|
|
2009-12-17 21:57:21 -05:00
|
|
|
# Run the CommandLine off the contents of ARGV.
|
2009-12-16 22:42:53 -05:00
|
|
|
def initialize
|
2009-12-17 20:37:39 -05:00
|
|
|
@mtimes = {}
|
2009-12-16 22:42:53 -05:00
|
|
|
parse_options
|
2009-12-24 23:57:27 -08:00
|
|
|
return launch_repl if @options[:interactive]
|
2009-12-19 22:55:58 -05:00
|
|
|
return eval_scriptlet if @options[:eval]
|
2009-12-16 22:42:53 -05:00
|
|
|
check_sources
|
2009-12-24 23:57:27 -08:00
|
|
|
return run_scripts if @options[:run]
|
2009-12-17 20:37:39 -05:00
|
|
|
@sources.each {|source| compile_javascript(source) }
|
|
|
|
watch_coffee_scripts if @options[:watch]
|
2009-12-16 22:42:53 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 21:57:21 -05:00
|
|
|
# The "--help" usage message.
|
2009-12-16 22:42:53 -05:00
|
|
|
def usage
|
|
|
|
puts "\n#{@option_parser}\n"
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2009-12-17 21:57:21 -05:00
|
|
|
# Compiles (or partially compiles) the source CoffeeScript file, returning
|
|
|
|
# the desired JS, tokens, or lint results.
|
2009-12-17 20:37:39 -05:00
|
|
|
def compile_javascript(source)
|
2009-12-24 15:49:42 -08:00
|
|
|
script = File.read(source)
|
2009-12-23 19:42:18 -05:00
|
|
|
return tokens(script) if @options[:tokens]
|
|
|
|
js = compile(script, source)
|
|
|
|
return unless js
|
2009-12-24 15:49:42 -08:00
|
|
|
return puts(js) if @options[:print]
|
2009-12-23 19:42:18 -05:00
|
|
|
return lint(js) if @options[:lint]
|
|
|
|
File.open(path_for(source), 'w+') {|f| f.write(js) }
|
2009-12-17 20:37:39 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 21:57:21 -05:00
|
|
|
# Spins up a watcher thread to keep track of the modification times of the
|
|
|
|
# source files, recompiling them whenever they're saved.
|
2009-12-17 20:37:39 -05:00
|
|
|
def watch_coffee_scripts
|
|
|
|
watch_thread = Thread.start do
|
|
|
|
loop do
|
|
|
|
@sources.each do |source|
|
|
|
|
mtime = File.stat(source).mtime
|
|
|
|
@mtimes[source] ||= mtime
|
|
|
|
if mtime > @mtimes[source]
|
|
|
|
@mtimes[source] = mtime
|
|
|
|
compile_javascript(source)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
sleep WATCH_INTERVAL
|
|
|
|
end
|
2009-12-16 22:42:53 -05:00
|
|
|
end
|
2009-12-21 12:15:13 -05:00
|
|
|
Signal.trap("INT") { watch_thread.kill }
|
2009-12-17 20:37:39 -05:00
|
|
|
watch_thread.join
|
2009-12-16 22:42:53 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 21:57:21 -05:00
|
|
|
# Ensure that all of the source files exist.
|
2009-12-16 22:42:53 -05:00
|
|
|
def check_sources
|
|
|
|
usage if @sources.empty?
|
2009-12-24 15:49:42 -08:00
|
|
|
missing = @sources.detect {|s| !File.exists?(s) }
|
2009-12-16 22:42:53 -05:00
|
|
|
if missing
|
|
|
|
STDERR.puts("File not found: '#{missing}'")
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-17 21:57:21 -05:00
|
|
|
# Pipe compiled JS through JSLint (requires a working 'jsl' command).
|
2009-12-16 22:42:53 -05:00
|
|
|
def lint(js)
|
|
|
|
stdin, stdout, stderr = Open3.popen3('jsl -nologo -stdin')
|
|
|
|
stdin.write(js)
|
|
|
|
stdin.close
|
2009-12-17 20:37:39 -05:00
|
|
|
puts stdout.read.tr("\n", '')
|
2009-12-17 21:57:21 -05:00
|
|
|
errs = stderr.read.chomp
|
|
|
|
puts errs unless errs.empty?
|
2009-12-16 22:42:53 -05:00
|
|
|
stdout.close and stderr.close
|
|
|
|
end
|
|
|
|
|
2009-12-19 22:55:58 -05:00
|
|
|
# Eval a little piece of CoffeeScript directly from the command line.
|
|
|
|
def eval_scriptlet
|
2009-12-24 01:22:41 -05:00
|
|
|
script = STDIN.tty? ? @sources.join(' ') : STDIN.read
|
2009-12-23 19:42:18 -05:00
|
|
|
return tokens(script) if @options[:tokens]
|
|
|
|
js = compile(script)
|
|
|
|
return lint(js) if @options[:lint]
|
|
|
|
puts js
|
2009-12-19 22:55:58 -05:00
|
|
|
end
|
|
|
|
|
2009-12-24 23:57:27 -08:00
|
|
|
# Use Narwhal to run an interactive CoffeeScript session.
|
|
|
|
def launch_repl
|
2009-12-28 01:16:57 -08:00
|
|
|
exec "#{LAUNCHER}"
|
2009-12-25 00:02:27 -08:00
|
|
|
rescue Errno::ENOENT
|
|
|
|
puts "Error: Narwhal must be installed to use the interactive REPL."
|
|
|
|
exit(1)
|
2009-12-24 23:57:27 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Use Narwhal to compile and execute CoffeeScripts.
|
|
|
|
def run_scripts
|
|
|
|
sources = @sources.join(' ')
|
2009-12-28 01:16:57 -08:00
|
|
|
exec "#{LAUNCHER} #{sources}"
|
2009-12-25 00:02:27 -08:00
|
|
|
rescue Errno::ENOENT
|
|
|
|
puts "Error: Narwhal must be installed in order to execute CoffeeScripts."
|
|
|
|
exit(1)
|
2009-12-24 23:57:27 -08:00
|
|
|
end
|
|
|
|
|
2009-12-17 21:57:21 -05:00
|
|
|
# Print the tokens that the lexer generates from a source script.
|
2009-12-23 19:42:18 -05:00
|
|
|
def tokens(script)
|
|
|
|
puts Lexer.new.tokenize(script).inspect
|
2009-12-17 08:23:07 -05:00
|
|
|
end
|
|
|
|
|
2009-12-17 21:57:21 -05:00
|
|
|
# Compile a single source file to JavaScript.
|
2009-12-25 20:36:22 -08:00
|
|
|
def compile(script, source='error')
|
2009-12-17 10:04:43 -05:00
|
|
|
begin
|
2009-12-25 19:48:47 -08:00
|
|
|
options = {}
|
|
|
|
options[:no_wrap] = true if @options[:no_wrap]
|
|
|
|
CoffeeScript.compile(script, options)
|
2009-12-25 20:36:22 -08:00
|
|
|
rescue CoffeeScript::ParseError, SyntaxError => e
|
|
|
|
STDERR.puts "#{source}: #{e.message}"
|
2009-12-17 20:37:39 -05:00
|
|
|
exit(1) unless @options[:watch]
|
|
|
|
nil
|
2009-12-17 10:04:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-16 22:42:53 -05:00
|
|
|
# Write out JavaScript alongside CoffeeScript unless an output directory
|
|
|
|
# is specified.
|
|
|
|
def path_for(source)
|
|
|
|
filename = File.basename(source, File.extname(source)) + '.js'
|
|
|
|
dir = @options[:output] || File.dirname(source)
|
|
|
|
File.join(dir, filename)
|
|
|
|
end
|
|
|
|
|
2009-12-17 21:57:21 -05:00
|
|
|
# Install the CoffeeScript TextMate bundle to ~/Library.
|
2009-12-17 21:46:12 -05:00
|
|
|
def install_bundle
|
|
|
|
bundle_dir = File.expand_path('~/Library/Application Support/TextMate/Bundles/')
|
|
|
|
FileUtils.cp_r(File.dirname(__FILE__) + '/CoffeeScript.tmbundle', bundle_dir)
|
|
|
|
end
|
|
|
|
|
2009-12-17 21:57:21 -05:00
|
|
|
# Use OptionParser for all the options.
|
2009-12-16 22:42:53 -05:00
|
|
|
def parse_options
|
|
|
|
@options = {}
|
|
|
|
@option_parser = OptionParser.new do |opts|
|
2009-12-24 23:57:27 -08:00
|
|
|
opts.on('-i', '--interactive', 'run a CoffeeScript REPL (requires Narwhal)') do |i|
|
|
|
|
@options[:interactive] = true
|
|
|
|
end
|
|
|
|
opts.on('-r', '--run', 'compile and run a script (requires Narwhal)') do |r|
|
|
|
|
@options[:run] = true
|
|
|
|
end
|
2009-12-17 21:46:12 -05:00
|
|
|
opts.on('-o', '--output [DIR]', 'set the directory for compiled JavaScript') do |d|
|
2009-12-16 22:42:53 -05:00
|
|
|
@options[:output] = d
|
|
|
|
FileUtils.mkdir_p(d) unless File.exists?(d)
|
|
|
|
end
|
2009-12-17 20:37:39 -05:00
|
|
|
opts.on('-w', '--watch', 'watch scripts for changes, and recompile') do |w|
|
|
|
|
@options[:watch] = true
|
|
|
|
end
|
2009-12-17 21:46:12 -05:00
|
|
|
opts.on('-p', '--print', 'print the compiled JavaScript to stdout') do |d|
|
2009-12-16 22:42:53 -05:00
|
|
|
@options[:print] = true
|
|
|
|
end
|
2009-12-17 21:46:12 -05:00
|
|
|
opts.on('-l', '--lint', 'pipe the compiled JavaScript through JSLint') do |l|
|
2009-12-16 22:42:53 -05:00
|
|
|
@options[:lint] = true
|
|
|
|
end
|
2009-12-24 23:57:27 -08:00
|
|
|
opts.on('-e', '--eval', 'compile a cli scriptlet or read from stdin') do |e|
|
2009-12-19 22:55:58 -05:00
|
|
|
@options[:eval] = true
|
|
|
|
end
|
2009-12-17 08:23:07 -05:00
|
|
|
opts.on('-t', '--tokens', 'print the tokens that the lexer produces') do |t|
|
|
|
|
@options[:tokens] = true
|
|
|
|
end
|
2009-12-19 00:37:54 -05:00
|
|
|
opts.on('-v', '--verbose', 'print at every step of code generation') do |v|
|
|
|
|
ENV['VERBOSE'] = 'true'
|
|
|
|
end
|
2009-12-24 17:37:24 -08:00
|
|
|
opts.on('-n', '--no-wrap', 'raw output, no safety wrapper or vars') do |n|
|
2009-12-24 15:31:00 -08:00
|
|
|
@options[:no_wrap] = true
|
|
|
|
end
|
2009-12-17 21:46:12 -05:00
|
|
|
opts.on_tail('--install-bundle', 'install the CoffeeScript TextMate bundle') do |i|
|
|
|
|
install_bundle
|
|
|
|
exit
|
|
|
|
end
|
2009-12-26 08:57:13 -08:00
|
|
|
opts.on_tail('--version', 'display CoffeeScript version') do
|
|
|
|
puts "CoffeeScript version #{CoffeeScript::VERSION}"
|
2009-12-16 22:42:53 -05:00
|
|
|
exit
|
|
|
|
end
|
|
|
|
opts.on_tail('-h', '--help', 'display this help message') do
|
|
|
|
usage
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@option_parser.banner = BANNER
|
|
|
|
begin
|
|
|
|
@option_parser.parse!(ARGV)
|
|
|
|
rescue OptionParser::InvalidOption => e
|
|
|
|
puts e.message
|
|
|
|
exit(1)
|
|
|
|
end
|
|
|
|
@sources = ARGV
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|