Class: Browsable::CLI

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

Overview

The ‘browsable` command-line interface.

CLI is a thin shell: it parses flags, runs the audit pipeline (sources →analyzers → report), and hands the Report to a formatter. Every command’s real logic lives in the classes it orchestrates.

Constant Summary collapse

ANALYZERS =

The analyzer used for each routed file kind.

{
  css:  Analyzers::CSS,
  erb:  Analyzers::ERB,
  html: Analyzers::HTML,
  js:   Analyzers::Javascript
}.freeze
SKIP_REASONS =
{
  css: "stylelint not found — run `browsable doctor`",
  js:  "eslint / eslint-plugin-compat not found — run `browsable doctor`"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


28
# File 'lib/browsable/cli.rb', line 28

def self.exit_on_failure? = true

Instance Method Details

#audit(path = ".") ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/browsable/cli.rb', line 48

def audit(path = ".")
  root = File.expand_path(path)
  config = load_config(root)
  target = resolve_target(config)

  warn_missing_dependencies
  report = run_audit(root: root, config: config, target: target)
  emit(report)
  exit(report.exit_code(fail_on: options["fail-on"] || "error"))
end

#check(*files) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/browsable/cli.rb', line 79

def check(*files)
  abort_with("`check` needs at least one file path.") if files.empty?

  paths = files.map { |file| File.expand_path(file) }
  root = detect_root(paths.first)
  config = load_config(root)
  target = resolve_target(config)

  report = run_audit(root: root, config: config, target: target, file_list: paths)
  emit(report)
  exit(report.exit_code(fail_on: "error"))
end

#doctorObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/browsable/cli.rb', line 63

def doctor
  doc = Doctor.new
  puts doc.render(color: color?)

  if options[:fix] && !doc.ok?
    puts
    doc.fix!
    puts
    puts doc.render(color: color?)
  end

  exit(doc.ok? ? 0 : 1)
end

#initObject



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

def init
  destination = File.expand_path(".browsable.yml")
  if File.exist?(destination) && !options[:force]
    abort_with("#{destination} already exists. Pass --force to overwrite.")
  end

  File.write(destination, render_config_template)
  puts pastel.green("Created #{destination}")
  puts pastel.dim("This file is optional — delete it and browsable still works. " \
                  "Uncomment a line to override a default.")
  puts pastel.dim("Run `browsable audit` to try it out.")
end

#target(path = ".") ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/browsable/cli.rb', line 114

def target(path = ".")
  config = load_config(File.expand_path(path))
  resolved = resolve_target(config)

  unconstrained = options[:target] ? [] : config.unconstrained_browsers
  caveats = []
  unless options[:target]
    caveats << config.policy_note if config.policy_note
    caveats.concat(config.target_notes)
  end
  caveats << resolved.note if resolved.note

  if json_output?
    puts JSON.pretty_generate(
      resolved.as_json.merge(unconstrained_browsers: unconstrained, notes: caveats)
    )
  else
    puts pastel.bold("Target: #{resolved.query}")
    resolved.browsers.each { |browser, version| puts "  #{browser.ljust(9)}>= #{version}" }
    unconstrained.each do |browser|
      puts pastel.dim("  #{browser.ljust(9)}any version  (not listed in allow_browser)")
    end
    caveats.each { |note| puts pastel.yellow("! #{note}") }
  end
end

#versionObject



141
142
143
# File 'lib/browsable/cli.rb', line 141

def version
  puts "browsable #{Browsable::VERSION}"
end