Sha256: 0db50bc923a0f3ef9f9b649abacc800744db8e429c530ddedbf7f1fd2a1ded6b

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

module Aigu
  class EmberExporter < Exporter
  protected

    def build_output
      @output = {}

      pattern = File.join(@input_directory, "#{@locale}/translations.js")
      Dir[pattern].each do |file|
        filepath = file.gsub(/\A#{@input_directory}\//, '')

        if ignored_filepath?(filepath)
          puts "Ignoring #{filepath}"
        else
          puts "Processing #{filepath}"

          content = JSON.parse(run_javascript_script(file))

          content = flattenize_hash(content)
          @output.merge! content
        end
      end
    end

    def run_javascript_script(file)
      script = "require('babel/register')({stage: 0}); console.log(JSON.stringify(require('#{file}')))"
      command = "node -e \"#{script}\" 2>/dev/null"
      puts "Executing `#{command}`"
      content = `#{command}`

      if content.empty?
        puts 'There was an error when executing the command. Please make sure `node` and `babel` are installed.'
        exit 0
      end

      content
    end

    def write_json_file
      file_path = @output_file
      puts "Generating #{file_path}"
      FileUtils.mkdir_p(File.dirname(file_path))

      File.open(file_path, 'w+') do |file|
        file << @output.to_json
      end
    end

    def flattenize_hash(hash, base_key = '')
      if hash.is_a?(Hash)
        hash.reduce({}) do |memo, (key, value)|
          new_base_key = [base_key, key].join('.').gsub(/\|\.+/, '|')
          memo.merge! flattenize_hash(value, new_base_key)
        end
      else
        { base_key.gsub(/^\./, '') => hash }
      end
    end

    def ignored_filepath?(filepath)
      @ignore && @ignore.any? do |pattern|
        File.fnmatch(pattern, filepath, File::FNM_PATHNAME | File::FNM_DOTMATCH)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aigu-0.5 lib/aigu/ember_exporter.rb