summaryrefslogtreecommitdiff
path: root/Rakefile
blob: b359d38e824a26926f178e69d9d971a50d53343d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require "bundler/setup"

task default: :test

desc "Publish new release"
task :publish do
  sh("git push --tags")
  sh("npm publish")
end

desc "Open your default browser with the test page"
task :test do
  sh("open test/index.html")
end

desc "Automatically wrap given locale files in UMD declarations"
task :umd, [:files] do |t, args|
    def indent(str)
        indented = str.split("\n").map do |line|
          "  #{line}"
        end
        indented.join("\n")
    end

    def wrap_in_umd(str)
      <<~HEREDOC
        (function (factory) {
          if (typeof define === 'function' && define.amd) {
            define(['jquery'], factory);
          } else if (typeof module === 'object' && typeof module.exports === 'object') {
            factory(require('jquery'));
          } else {
            factory(jQuery);
          }
        }(function (jQuery) {
        #{indent(str)}
        }));
      HEREDOC
    end

    matches = Dir.glob(args[:files])
    puts "Pattern did not match any file" if matches.length == 0
    matches.each do |file|
        input = File.read(file)
        base = Pathname.new(file).basename
        output = File.open("locales/#{base}", "w")
        output.write(wrap_in_umd(input))
        output.close
    end
end