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

  Basic Options:
    -C, --config <path>                  Use the config provided, overriding .theme-check.yml if present
    -c, --category <category>            Only run this category of checks
    -x, --exclude-category  <category>   Exclude this category of checks
    -a, --auto-correct                   Automatically fix offenses

  Miscellaneous:
    --init                               Generate a .theme-check.yml file
    --print-config                       Output active config to STDOUT
    -h, --help                           Show this. Hi!
    -l, --list                           List enabled checks
    -v, --version                        Print Theme Check version

  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

#check ⇒ Object

Raises:



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/theme_check/cli.rb', line 114

def check
  puts "Checking #{@config.root} ..."
  storage = ThemeCheck::FileSystemStorage.new(@config.root, ignored_patterns: @config.ignored_patterns)
  theme = ThemeCheck::Theme.new(storage)
  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

#init ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/theme_check/cli.rb', line 99

def init
  dotfile_path = ThemeCheck::Config.find(@path)
  if dotfile_path.nil?
    File.write(File.join(@path, ThemeCheck::Config::DOTFILE), File.read(ThemeCheck::Config::DEFAULT_CONFIG))

    puts "Writing new #{ThemeCheck::Config::DOTFILE} to #{@path}"
  else
    raise Abort, "#{ThemeCheck::Config::DOTFILE} already exists at #{@path}."
  end
end

#list ⇒ Object



91
92
93
# File 'lib/theme_check/cli.rb', line 91

def list
  puts @config.enabled_checks
end


110
111
112
# File 'lib/theme_check/cli.rb', line 110

def print
  puts YAML.dump(@config.to_h)
end

#run(argv) ⇒ Object



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/theme_check/cli.rb', line 29

def run(argv)
  @path = "."

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

  args = argv.dup
  while (arg = args.shift)
    case arg
    when "--help", "-h"
      raise Abort, USAGE
    when "--version", "-v"
      command = :version
    when "--config", "-C"
      config_path = Pathname.new(args.shift)
    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
    when "--init"
      command = :init
    when "--print"
      command = :print
    else
      @path = arg
    end
  end

  unless [:version, :init].include?(command)
    @config = if config_path
      ThemeCheck::Config.new(
        root: @path,
        configuration: ThemeCheck::Config.load_file(config_path)
      )
    else
      ThemeCheck::Config.from_path(@path)
    end
    @config.only_categories = only_categories
    @config.exclude_categories = exclude_categories
    @config.auto_correct = auto_correct
  end

  send(command)
end

#run!(argv) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/theme_check/cli.rb', line 81

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

#version ⇒ Object



95
96
97
# File 'lib/theme_check/cli.rb', line 95

def version
  puts ThemeCheck::VERSION
end