-
-
Save mcculloughsean/717422 to your computer and use it in GitHub Desktop.
# 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 |
That doesn't preserve the directory structure. If you have a tree of coffeescript files and would like it to translate into a tree of javascript files, the coffee command will not work. Every way i tried it, all files were outputted at the same depth in the output directory.
E.g.
coffeescripts/
--lib/
-- -- file1.coffee
-- -- file2.coffee
--models/
-- -- model1.coffee
--widgets/
-- -- checkbox.coffee
-- -- selector.coffee
coffee -wc -o js coffeescripts will output
js/
-- file1.js
-- selector.coffee
...etc.
The latest stable CoffeeScript (0.9.5 at this time) preserves the structure in --watch
mode. Just tried it with the tree above:
% coffee -wc -o js coffeescripts
Compiled coffeescripts/lib/file2.coffee
Compiled coffeescripts/lib/file1.coffee
Compiled coffeescripts/models/file2.coffee
Compiled coffeescripts/widgets/checkbox.coffee
Compiled coffeescripts/widgets/selector.coffee
% tree
.
├── coffeescripts
│ ├── lib
│ │ ├── file1.coffee
│ │ └── file2.coffee
│ ├── models
│ │ └── file2.coffee
│ └── widgets
│ ├── checkbox.coffee
│ └── selector.coffee
└── js
├── lib
│ ├── file1.js
│ └── file2.js
├── models
│ └── file2.js
└── widgets
├── checkbox.js
└── selector.js
8 directories, 10 files
Give it a spin and if it still isn't working, post an issue on Github perhaps? I am sure it is can be fixed.
I was using 0.9.4! Thanks!
Mind sharing why
coffee -wc -o lib src &
is not enough for you? What's lacking, perhaps we can port it over to CoffeeScript itself.