Created
November 27, 2010 00:57
-
-
Save mcculloughsean/717422 to your computer and use it in GitHub Desktop.
Compiles coffeescripts to js in the background, preserving directory structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
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!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.