Created
August 11, 2014 22:39
-
-
Save ncherro/d320f6a833fd29bdda63 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'fileutils' | |
# setup | |
root_dir = ARGV[0] || "#{ENV['HOME']}/Desktop/coffee-to-browserify" | |
src_dir = ARGV[1] || "src" | |
dst_dir = ARGV[2] || "dst" | |
src = "#{root_dir}/#{src_dir}" | |
dst = "#{root_dir}/#{dst_dir}" | |
FileUtils.mkdir_p dst | |
raise "#{src}/src does not exist" unless Dir.exists?(src) | |
raise "#{dst} does not exist" unless Dir.exists?(dst) | |
# read all coffeescript files | |
Dir.chdir(root_dir) do | |
coffeescript_files = Dir.glob("./src/**/*.coffee") | |
puts "Parsing #{coffeescript_files.length} files..." | |
errors = [] | |
coffeescript_files.each_with_index do |path, j| | |
puts "parsing #{path}" | |
top_lines = [] | |
bottom_lines = [] | |
begin | |
File.open(path) do |f| | |
# get stuff up to the first "->" | |
top_lines_done = false | |
f.each_line do |line| | |
if top_lines_done | |
bottom_lines << line | |
else | |
top_lines << line | |
if line.include?('->') | |
if extra = line.split('->')[1] | |
# add it below | |
bottom_lines << extra | |
end | |
top_lines_done = true | |
end | |
end | |
end | |
end | |
# move all onto one line, ensuring separated by no more than one space | |
top = top_lines.map(&:strip).join(' ').split("\n").join(' ').gsub(/\s+/, ' ').gsub(',', '') | |
# split dependencies and vars into arrays | |
dependencies = /\[(.*)\]/.match(top)[1].gsub(/\"|\'/, '').split(' ') | |
vars = /\((.*)\)/.match(top)[1].split(' ') | |
# rewrite top, browserify style | |
lines = [] | |
dependencies.each_with_index do |dep, i| | |
if vars[i] | |
lines << "#{vars[i]} = require('#{dep}')" | |
else | |
# require it but do not assign to a var | |
lines << "require('#{dep}')" | |
end | |
end | |
# shift bottom lines X spaces to the left - start with a big number | |
spaces = 10000 | |
last_left_aligned = 0 | |
bottom_lines.each_with_index do |line, k| | |
next if line.strip.empty? | |
leading_spaces = line[/\A */].size | |
if spaces > leading_spaces | |
# set it | |
spaces = leading_spaces | |
# store this index - we will use it for module export | |
last_left_aligned = k | |
elsif spaces == leading_spaces | |
last_left_aligned = k | |
end | |
end | |
mod_def = bottom_lines[last_left_aligned] | |
if mod_def.include?('=') | |
mod_def = mod_def.split('=')[0].strip | |
else | |
mod_def = '_MODULE_' | |
bottom_lines[last_left_aligned] = "#{' ' * spaces}#{mod_def} = #{bottom_lines[last_left_aligned].lstrip}" | |
end | |
bottom_lines += [ | |
"", | |
"", | |
"module.exports(#{mod_def})" | |
] | |
lines += bottom_lines.map { |line| line.gsub(/\A\ {#{spaces}}/, '').rstrip } | |
parsed = path.split('/src/').join('/dst/') | |
FileUtils.mkdir_p(parsed.split('/')[0..-2].join('/')) | |
File.open(parsed, "w+") do |f| | |
f.puts(lines) | |
end | |
rescue => error | |
errors << "COULD NOT PARSE #{path} -- #{error}" | |
end | |
end | |
puts errors.join "\n" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment