Class: InertiaI18n::Cli

Inherits:
Thor
  • Object
show all
Defined in:
lib/inertia_i18n/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/inertia_i18n/cli.rb', line 10

def self.exit_on_failure?
  true
end

Instance Method Details

#convertObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/inertia_i18n/cli.rb', line 17

def convert
  load_config(options[:config])

  if options[:locale]
    result = FileConverter.convert_locale(options[:locale].to_sym)
    print_convert_result(result)
  else
    results = FileConverter.convert_all
    results.each { |r| print_convert_result(r) }
  end
end

#healthObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/inertia_i18n/cli.rb', line 120

def health
  load_config(options[:config])

  checker = HealthChecker.new.check!

  if options[:format] == "json"
    output = {
      healthy: checker.healthy?,
      summary: checker.summary,
      issues: checker.issues
    }
    puts JSON.pretty_generate(output)
  else
    print_health_results(checker, options[:verbose])
  end

  exit 1 unless checker.healthy?
end

#initObject



155
156
157
158
159
160
161
162
163
164
# File 'lib/inertia_i18n/cli.rb', line 155

def init
  format = options[:format]
  path = options[:path] || default_init_path(format)

  content = (format == "yaml") ? yaml_config_template : ruby_config_template

  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, content)
  puts "Created #{path}"
end

#missingObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/inertia_i18n/cli.rb', line 80

def missing
  load_config(options[:config])

  checker = HealthChecker.new.check!(checks: [:missing])

  if options[:format] == "json"
    puts JSON.pretty_generate({issues: checker.issues[:missing], count: checker.issues[:missing].size})
  else
    print_issues("Missing Keys (used in code, not translated)", checker.issues[:missing], options[:verbose])
    puts "#{checker.issues[:missing].size} missing key(s) found." if checker.issues[:missing].any?
    puts "No missing keys found." if checker.issues[:missing].empty?
  end

  exit 1 if checker.issues[:missing].any?
end

#normalizeObject



141
142
143
144
145
# File 'lib/inertia_i18n/cli.rb', line 141

def normalize
  load_config(options[:config])

  Normalizer.new.normalize
end

#scanObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/inertia_i18n/cli.rb', line 60

def scan
  load_config(options[:config])

  scanner = Scanner.new
  results = scanner.scan

  case options[:format]
  when "json"
    puts JSON.pretty_generate(results.to_h)
  when "yaml"
    puts results.to_h.to_yaml
  else
    print_scan_results(results)
  end
end

#unusedObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/inertia_i18n/cli.rb', line 100

def unused
  load_config(options[:config])

  checker = HealthChecker.new.check!(checks: [:unused])

  if options[:format] == "json"
    puts JSON.pretty_generate({issues: checker.issues[:unused], count: checker.issues[:unused].size})
  else
    print_issues("Unused Keys (translated, not used in code)", checker.issues[:unused], options[:verbose])
    puts "#{checker.issues[:unused].size} unused key(s) found." if checker.issues[:unused].any?
    puts "No unused keys found." if checker.issues[:unused].empty?
  end

  exit 1 if checker.issues[:unused].any?
end

#versionObject



148
149
150
# File 'lib/inertia_i18n/cli.rb', line 148

def version
  puts "InertiaI18n #{InertiaI18n::VERSION}"
end

#watchObject



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
# File 'lib/inertia_i18n/cli.rb', line 31

def watch
  load_config(options[:config])
  config = InertiaI18n.configuration
  paths = config.source_paths.map(&:to_s)

  puts "šŸ‘€ Watching #{paths.join(", ")} for YAML changes..."

  listener = Listen.to(*paths, only: /\.(yml|yaml)$/) do |modified, added, removed|
    puts "šŸ“ Detected locale file changes..."
    (modified + added + removed).each do |file|
      puts "   #{file}"
    end

    puts "šŸ”„ Regenerating JSON files..."
    results = FileConverter.convert_all
    results.each { |r| print_convert_result(r) }
    puts "āœ… Done!"
  end

  listener.start
  sleep
rescue Interrupt
  puts "\nšŸ‘‹ Stopping watch mode..."
  listener.stop
end