Skip to content

Instantly share code, notes, and snippets.

@dmitry-vsl
Last active August 29, 2015 14:10
Show Gist options
  • Save dmitry-vsl/9214dde791b1a7fbf79a to your computer and use it in GitHub Desktop.
Save dmitry-vsl/9214dde791b1a7fbf79a to your computer and use it in GitHub Desktop.
Script to refactor requirejs(amd) modules to more convenient style
#!/opt/vagrant_ruby/bin/ruby
file = IO.read ARGV[0]
header = /
define \s* \[
([\s\w\.'"\/,]*?)
\] \s* , \s* \(
([\s\w,]+)
\) \s* ->
/x
match = header.match(file)
unless match
exit 1
end
def split string
strs = []
string.split(/[\s,]+/).each do |p|
p = p.strip
if p != ''
strs.push p
end
end
strs
end
paths = split match[1]
names = split match[2]
result = "define (require) ->\n"
i = 0
while i < paths.length
if names[i]
result += " #{names[i]} = require #{paths[i]}\n"
else
result += " require #{paths[i]}\n"
end
i += 1
end
result += "\n"
puts file.sub header, result
#!/bin/bash
for file in `find app tests -name '*.coffee'` ; do
result=`./ref.rb $file`
if [ $? == 0 ] ; then
echo "$result" > $file
echo OK $file
else
echo SKIPPING $file
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment