Skip to content

Instantly share code, notes, and snippets.

@mcculloughsean
Created November 27, 2010 00:57
Show Gist options
  • Save mcculloughsean/717422 to your computer and use it in GitHub Desktop.
Save mcculloughsean/717422 to your computer and use it in GitHub Desktop.
Compiles coffeescripts to js in the background, preserving directory structure
# Run me with:
# $ watchr compile.watchr
# --------------------------------------------------
# Rules
# --------------------------------------------------
watch( '^Resources/cs/(.*\.coffee)' ) { |m| coffee m }
# --------------------------------------------------
# Signal Handling
# --------------------------------------------------
Signal.trap('INT' ) { abort("\n") } # Ctrl-C
Signal.trap('QUIT' ) { coffee everything } # Ctrl-\
# --------------------------------------------------
# Helpers
# --------------------------------------------------
def coffee(*paths)
if paths[0].is_a?(Array)
paths = paths[0]
end
paths.each do |i|
if (i.is_a?(MatchData))
input = i[0].to_s
output_dir = i[0].to_s
else
puts "#{i}\n"
input = i.clone
output_dir = i.clone
end
output_dir.sub!(/\/(\w*\.coffee)/, '')
output_dir.sub!(/cs/, 'js')
FileUtils.mkdir_p output_dir
run "coffee -c -o #{output_dir} #{input}"
end
end
def everything
scripts = Dir['Resources/cs/**/*.coffee']
scripts.flatten
end
def run( cmd )
puts cmd
puts system cmd
end
@mcculloughsean
Copy link
Author

I was using 0.9.4! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment