Skip to content

Instantly share code, notes, and snippets.

@unamashana
Created July 6, 2011 05:15
Show Gist options
  • Save unamashana/1066615 to your computer and use it in GitHub Desktop.
Save unamashana/1066615 to your computer and use it in GitHub Desktop.
Converting a Javascript Project to CoffeeScript
#SRC_BASE_DIR = "public/javascripts/backbone"
#DEST_BASE_DIR = "app/coffeescripts/backbone"
SRC_BASE_DIR = "spec/javascripts"
DEST_BASE_DIR = "spec/coffeescripts"
JS2COFFEE = "~/node_modules/js2coffee/bin/js2coffee"
dirs = []
files = []
Dir.glob("#{SRC_BASE_DIR}/**/*.js").each do |file|
files << file
dir = File.dirname(file).split(SRC_BASE_DIR)[1]
dirs << dir unless dirs.include?(dir)
end
puts dirs
dirs.each do |dir|
IO.popen("mkdir -p #{DEST_BASE_DIR}#{dir}")
end
files.each do |file|
# convert
relative_path = File.dirname(file).split(SRC_BASE_DIR)[1]
basename = File.basename(file, ".js")
output_file = "#{DEST_BASE_DIR}#{relative_path}/#{basename}.coffee"
puts "#{JS2COFFEE} #{file} > #{output_file}"
`#{JS2COFFEE} #{file} > #{output_file}`
puts "Removing #{file}"
`rm #{file}`
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment