Someone directed me to this article:
http://scottwb.com/blog/2012/06/30/compile-a-single-coffeescript-file-from-your-rails-project/ and I updated it to give me the option to run recursively on a directory, or once on a single file. I added this to lib/tasks/
and it worked like a charm. I added a test for sprockets-style directives, which start with #= require
, because the CoffeeScript compiler strips out all comments, which causes everything to break. Instead, I converted all skipped files to JS manually and included the directives as //= require
, and that worked.
namespace :coffee do
def do_one(filepath)
File.write(filepath.chomp(".coffee"), CoffeeScript.compile(File.open(filepath)))
File.rename(filepath, filepath.chomp(".js.coffee") + ".backup")
end
def cs_task(path)
Dir.glob("#{path.chomp("/")}/*.js.coffee").each do |filename|
file = File.open(filename)
if (file.read[/\= require/])
puts "skip #{filename}"
else
puts "process #{filename}"
do_one(filename)
end
end
Dir.glob("#{path.chomp("/")}/*/").each do |child_path|
cs_task(child_path)
end
end
task :cancel, :path do |t, args|
cs_task(args.path)
end
task :show, :path do |t, args|
puts CoffeeScript.compile(File.open(args.path))
end
task :one_off, :path do |t, args|
do_one(args.path)
end
end