class Prettify::Command

Public Instance Methods

json() click to toggle source
# File lib/prettify.rb, line 26
def json
  _options = options.dup
  _options['output'] = _options['location'] if !_options['output']

  puts "\nRunning with options:"
  _options.each do |k, v|
    puts "\t#{k}: #{v}".yellow
  end
  puts ''

  prettify_block = Proc.new do |read_file, write_file|
    puts "Processing file: #{File.basename(read_file)}".cyan
    content = File.read(read_file)
    content = prettify(content)
    f = File.open(write_file, 'w')
    f.write(content)
    f.close
  end

  location = _options['location']
  output = _options['output']
  if File.directory?(location)
    raise 'Output location must be a directory' unless File.directory?(output)
    Dir.glob(File.join(location, '*.json')) do |file|
      prettify_block.call(file, File.join(output, File.basename(file)))
    end
  else
    output = File.directory?(output) ? File.join(output, location) : output
    prettify_block.call(location, output)
  end

  puts ''
end