Class: ThemeCheck::Cli

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

Defined Under Namespace

Classes: Abort

Constant Summary collapse

USAGE =
"Usage: theme-check [options] /path/to/your/theme\n\nOptions:\n  -c, [--category]          # Only run this category of checks\n  -x, [--exclude-category]  # Exclude this category of checks\n  -l, [--list]              # List enabled checks\n  -a, [--auto-correct]      # Automatically fix offenses\n  --init                    # Generate a .theme-check.yml file in the current directory\n  -h, [--help]              # Show this. Hi!\n  -v, [--version]           # Print Theme Check version\n\nDescription:\n  Theme Check helps you follow Shopify Themes & Liquid best practices by analyzing the\n  Liquid & JSON inside your theme.\n\n  You can configure checks in the .theme-check.yml file of your theme root directory.\n"

Instance Method Summary collapse

Instance Method Details

#checkObject

Raises:



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/theme_check/cli.rb', line 94

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

#initObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/theme_check/cli.rb', line 83

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

#listObject



75
76
77
# File 'lib/theme_check/cli.rb', line 75

def list
  puts @config.enabled_checks
end

#run(argv) ⇒ Object



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
56
57
58
59
60
61
62
63
# File 'lib/theme_check/cli.rb', line 25

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 "--version", "-v"
      command = :version
    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
    else
      @path = arg
    end
  end

  unless [:version, :init].include?(command)
    @config = ThemeCheck::Config.from_path(@path)
    @config.only_categories = only_categories
    @config.exclude_categories = exclude_categories
    @config.auto_correct = auto_correct
  end

  send(command)
end

#run!(argv) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/theme_check/cli.rb', line 65

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

#versionObject



79
80
81
# File 'lib/theme_check/cli.rb', line 79

def version
  puts ThemeCheck::VERSION
end