Skip to content

Commit b313a83

Browse files
committed
add rake task for automatically wrapping locale files in umd
1 parent 14ae52d commit b313a83

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.bundle
22
vendor/ruby
33
public
4+
node_modules/

Rakefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,39 @@ desc "Open your default browser with the test page"
1212
task :test do
1313
sh("open test/index.html")
1414
end
15+
16+
desc "Automatically wrap given locale files in UMD declarations"
17+
task :umd, [:files] do |t, args|
18+
def indent(str)
19+
indented = str.split("\n").map do |line|
20+
" #{line}"
21+
end
22+
indented.join("\n")
23+
end
24+
25+
def wrap_in_umd(str)
26+
<<~HEREDOC
27+
(function (factory) {
28+
if (typeof define === 'function' && define.amd) {
29+
define(['jquery'], factory);
30+
} else if (typeof module === 'object' && typeof module.exports === 'object') {
31+
factory(require('jquery'));
32+
} else {
33+
factory(jQuery);
34+
}
35+
}(function (jQuery) {
36+
#{indent(str)}
37+
});
38+
HEREDOC
39+
end
40+
41+
matches = Dir.glob(args[:files])
42+
puts "Pattern did not match any file" if matches.length == 0
43+
matches.each do |file|
44+
input = File.read(file)
45+
base = Pathname.new(file).basename
46+
output = File.open("locales/#{base}", "w")
47+
output.write(wrap_in_umd(input))
48+
output.close
49+
end
50+
end

0 commit comments

Comments
 (0)