module Aigu class Merger def initialize(opts = {}) @output_file = opts[:'output-file'] @input_directory = opts[:'input-directory'] end def process! puts "Generating flat Accent JSON file `#{@output_file}` merging json files in `#{@input_directory}` directory" puts '---' build_output write_json_file puts '---' puts 'Done' end protected def build_output @output = {} pattern = File.join(@input_directory, '*.json') Dir[pattern].each do |filepath| puts "Processing #{filepath}" file_name = filepath.rpartition(File::SEPARATOR).last.split('.', 2).first prefix = file_name == 'default' ? '' : "@#{file_name.upcase}@__" json = JSON.parse(File.read(filepath)) json.each_pair do |key, value| @output[prefix + key] = value end end @output 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 << JSON.pretty_generate(JSON.parse(@output.to_json)) end end end end