Class: Tailwindcss::Formatter::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/tailwindcss/formatter.rb

Overview

This class handles the command-line interface (CLI) for the TailwindCSS formatter. It processes command-line arguments and passes them to the ClassSorter

Class Method Summary collapse

Class Method Details

.add_check_formatted_option(opts, options) ⇒ Object



52
53
54
55
56
57
# File 'lib/tailwindcss/formatter.rb', line 52

def self.add_check_formatted_option(opts, options)
  opts.on("-c", "--check-formatted [FLAG]", TrueClass,
          "Raises exit 1 if unformatted class strings are detected (default: false)") do
    options[:check_formatted] = true
  end
end

.add_glob_option(opts, options) ⇒ Object



40
41
42
43
44
# File 'lib/tailwindcss/formatter.rb', line 40

def self.add_glob_option(opts, options)
  opts.on("-g", "--glob GLOB", "Glob path to match files (default: \"app/**/*.html.erb\")") do |glob|
    options[:glob] = glob
  end
end

.add_write_option(opts, options) ⇒ Object



46
47
48
49
50
# File 'lib/tailwindcss/formatter.rb', line 46

def self.add_write_option(opts, options)
  opts.on("-w", "--write [FLAG]", TrueClass, "Write any formatting back to the files (default: false)") do
    options[:write] = true
  end
end

.find_files(glob) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/tailwindcss/formatter.rb', line 59

def self.find_files(glob)
  files = Dir.glob(glob)

  if files.empty?
    puts "No files matched the glob pattern: #{glob}"
    exit 1
  end

  files
end

.option_parser(options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/tailwindcss/formatter.rb', line 29

def self.option_parser(options)
  OptionParser.new do |opts|
    opts.banner = "Usage: tailwindcss-formatter [options]\n
    Example: tailwindcss-formatter \"app/**/*.html.erb\""

    add_glob_option(opts, options)
    add_write_option(opts, options)
    add_check_formatted_option(opts, options)
  end
end

.parse_options(argv) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/tailwindcss/formatter.rb', line 19

def self.parse_options(argv)
  options = { glob: "app/**/*.html.erb", check_formatted: false, write: false }

  # Check if the first argument is a glob pattern (positional argument)
  options[:glob] = argv.shift if argv.any? && !argv.first.start_with?("-")

  option_parser(options).parse!(argv)
  options
end

.process_files(files, options) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/tailwindcss/formatter.rb', line 70

def self.process_files(files, options)
  class_sorter = ClassSorter.new(check_formatted: options[:check_formatted], write: options[:write])

  files.each do |file|
    class_sorter.process_file(file)
  end
end

.start(argv) ⇒ Object



13
14
15
16
17
# File 'lib/tailwindcss/formatter.rb', line 13

def self.start(argv)
  options = parse_options(argv)
  files = find_files(options[:glob])
  process_files(files, options)
end