Class: ThemeCheck::Cli

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

Defined Under Namespace

Classes: Abort

Constant Summary collapse

USAGE =
<<~END
  Usage: theme-check [options] /path/to/your/theme

  Options:
    -c, [--category]          # Only run this category of checks
    -x, [--exclude-category]  # Exclude this category of checks
    -l, [--list]              # List enabled checks
    -a, [--auto-correct]      # Automatically fix offenses
    -h, [--help]              # Show this. Hi!

  Description:
    Theme Check helps you follow Shopify Themes & Liquid best practices by analyzing the
    Liquid & JSON inside your theme.

    You can configure checks in the .theme-check.yml file of your theme root directory.
END

Instance Method Summary collapse

Instance Method Details

#checkObject

Raises:



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/theme_check/cli.rb', line 71

def check
  puts "Checking #{@config.root} ..."
  theme = ThemeCheck::Theme.new(@config.root)
  if theme.all.empty?
    raise Abort, "No templates found.\n#{USAGE}"
  end
  analyzer = ThemeCheck::Analyzer.new(theme, @config.enabled_checks, @config.auto_correct)
  analyzer.analyze_theme
  analyzer.correct_offenses
  ThemeCheck::Printer.new.print(theme, analyzer.offenses, @config.auto_correct)
  raise Abort, "" if analyzer.uncorrectable_offenses.any?
end

#listObject



67
68
69
# File 'lib/theme_check/cli.rb', line 67

def list
  puts @config.enabled_checks
end

#run(argv) ⇒ Object



23
24
25
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
# File 'lib/theme_check/cli.rb', line 23

def run(argv)
  path = "."

  command = :check
  only_categories = []
  exclude_categories = []
  auto_correct = false

  args = argv.dup
  while (arg = args.shift)
    case arg
    when "--help", "-h"
      raise Abort, USAGE
    when "--category", "-c"
      only_categories << args.shift.to_sym
    when "--exclude-category", "-x"
      exclude_categories << args.shift.to_sym
    when "--list", "-l"
      command = :list
    when "--auto-correct", "-a"
      auto_correct = true
    else
      path = arg
    end
  end

  @config = ThemeCheck::Config.from_path(path)
  @config.only_categories = only_categories
  @config.exclude_categories = exclude_categories
  @config.auto_correct = auto_correct

  send(command)
end

#run!(argv) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/theme_check/cli.rb', line 57

def run!(argv)
  run(argv)
rescue Abort => e
  if e.message.empty?
    exit(1)
  else
    abort(e.message)
  end
end