Created
November 13, 2014 15:25
-
-
Save trotzig/d3ff1e156701bfcd144a to your computer and use it in GitHub Desktop.
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
# Converts global chained `var`s into single-line vars. | |
# | |
# Example: | |
# | |
# >> cat test.js | |
# var A = require('a'), | |
# B = require('b'), | |
# C = B.C; | |
# | |
# var D = function() {}; | |
# | |
# >> ruby unchain_vars.rb test.js | |
# | |
# >> cat test.js | |
# var A = require('a'); | |
# var B = require('b'); | |
# var C = B.C; | |
# | |
# var D = function() {}; | |
# | |
filename = ARGV[0] | |
active = false | |
result = '' | |
File.open(filename).each do |line| | |
if line =~ /^var .*,$/ | |
active = true | |
end | |
if active | |
result << line.gsub(/^(var | +)(.+)[,;]$/, 'var \2;') | |
else | |
result << line | |
end | |
if active && line =~ /;$/ | |
active = false | |
end | |
end | |
File.write(filename, result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I made it able to take multiple filenames: https://gist.github.com/lencioni/b638689132e6c3a665c4