module Capucine class CoffeeScript require 'packr' require 'coffee-script' def initialize(capucine) @cap = capucine end def run_once(file = nil) unless file files = Dir.glob(File.join @cap.settings.working_dir, @cap.settings.conf['coffeescript_output_dir'], '*') FileUtils.rm_r files self.compile_dir @cap.settings.conf['coffeescript_files_dir'], @cap.settings.conf['coffeescript_output_dir'] else self.compile_dir file, @cap.settings.conf['coffeescript_output_dir'] end puts "[coffee] - Compiled" end def compile_dir(input, output) if File.extname(input) == '.coffee' coffee_files = input else coffee_files = File.join @cap.settings.working_dir, input, "**/**.coffee" end Dir.glob(coffee_files).each do |file_coffee_o| file_coffee = File.expand_path file_coffee_o base_in_dir = File.join @cap.settings.working_dir, input relative_path = file_coffee.gsub(File.join(@cap.settings.working_dir, @cap.settings.conf['coffeescript_files_dir'] ),'') relative_path = relative_path.gsub(/\.coffee$/, '.js') relative_path_min = relative_path.gsub(/\.js$/, '.min.js') file_out = File.join @cap.settings.working_dir, output, relative_path file_out_min = File.join @cap.settings.working_dir, output, relative_path_min relative_coffee_file = file_coffee.gsub(base_in_dir, '') opts = {:bare => @cap.settings.conf['coffeescript_bare']} coffee_output_min = "" error = false begin coffee_output = ::CoffeeScript.compile(File.read(file_coffee), opts) rescue Exception => e error = true message = "#{e.message}" coffee_output = "var message = \"CoffeeScript Error (#{relative_coffee_file.gsub(/^\//, '')}) => \";" coffee_output += "message += \"#{message}\";" coffee_output += "throw message;" end coffee_output_min << Packr.pack(coffee_output) FileUtils.mkdir_p File.dirname(file_out) puts "[coffee] - #{relative_path} => #{message}" if error f1 = File.open(file_out, 'w+') f1.write(coffee_output) f1.close f2 = File.open(file_out_min, 'w+') f2.write(coffee_output_min) f2.close end end def run_watch require 'fssm' files_to_lookat = File.join @cap.settings.working_dir, @cap.settings.conf['coffeescript_files_dir'] js_generated_dir = File.join @cap.settings.working_dir, @cap.settings.conf['coffeescript_output_dir'] coffee = @cap.coffee coffee_thread = Thread.new { FSSM.monitor(files_to_lookat, :directories => true) do update do |b, r| file = File.join b, r coffee.run_once file if File.extname(r) == '.coffee' # COFFEE end create do |b, r| file = File.join b, r coffee.run_once file if File.extname(r) == '.coffee' # COFFEE end delete do |b, r| file_name = File.expand_path File.join(b, r) folder_name = File.dirname file_name js_files = [ File.join(js_generated_dir, r.gsub('.coffee', '.js')), File.join(js_generated_dir, r.gsub('.coffee', '.min.js')), ] if Dir["#{folder_name}/*"].empty? and folder_name != js_generated_dir # delete full dir to_delete = File.expand_path(File.dirname(js_files[0])) FileUtils.rm_r to_delete else js_files.each do |file| File.delete(file) if File.exist?(file) end end end end } return coffee_thread end end end