Class: Prettify::Command

Inherits:
Thor
  • Object
show all
Includes:
Prettifier
Defined in:
lib/prettify.rb

Instance Method Summary collapse

Methods included from Prettifier

#prettify

Instance Method Details

#jsonObject



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
51
52
53
54
55
56
57
58
# 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