diff --git a/lib/coffee_script/command_line.rb b/lib/coffee_script/command_line.rb index 66c02af7..99b4f321 100644 --- a/lib/coffee_script/command_line.rb +++ b/lib/coffee_script/command_line.rb @@ -14,10 +14,14 @@ Usage: coffee-script path/to/script.cs EOS + WATCH_INTERVAL = 0.5 + def initialize + @mtimes = {} parse_options check_sources - compile_javascript + @sources.each {|source| compile_javascript(source) } + watch_coffee_scripts if @options[:watch] end def usage @@ -28,14 +32,30 @@ Usage: private - def compile_javascript - @sources.each do |source| - next tokens(source) if @options[:tokens] - contents = compile(source) - next puts(contents) if @options[:print] - next lint(contents) if @options[:lint] - File.open(path_for(source), 'w+') {|f| f.write(contents) } + def compile_javascript(source) + return tokens(source) if @options[:tokens] + contents = compile(source) + return unless contents + return puts(contents) if @options[:print] + return lint(contents) if @options[:lint] + File.open(path_for(source), 'w+') {|f| f.write(contents) } + end + + 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 end + watch_thread.join end def check_sources @@ -52,7 +72,7 @@ Usage: stdin, stdout, stderr = Open3.popen3('jsl -nologo -stdin') stdin.write(js) stdin.close - print stdout.read + puts stdout.read.tr("\n", '') stdout.close and stderr.close end @@ -65,7 +85,8 @@ Usage: CoffeeScript.compile(File.open(source)) rescue CoffeeScript::ParseError => e STDERR.puts e.message(source) - exit(1) + exit(1) unless @options[:watch] + nil end end @@ -84,6 +105,9 @@ Usage: @options[:output] = d FileUtils.mkdir_p(d) unless File.exists?(d) end + opts.on('-w', '--watch', 'watch scripts for changes, and recompile') do |w| + @options[:watch] = true + end opts.on('-p', '--print', 'print the compiled javascript to stdout') do |d| @options[:print] = true end diff --git a/lib/coffee_script/nodes.rb b/lib/coffee_script/nodes.rb index 548f9816..61532d47 100644 --- a/lib/coffee_script/nodes.rb +++ b/lib/coffee_script/nodes.rb @@ -76,6 +76,7 @@ class ReturnNode < Node end def compile(indent, scope, opts={}) + return @expression.compile(indent, scope, opts.merge(:return => true)) if @expression.custom_return? compiled = @expression.compile(indent, scope) @expression.statement? ? "#{compiled}\n#{indent}return null" : "return #{compiled}" end