Class: Sakusei::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dispatch(meth, given_args, given_opts, config) ⇒ Object

Override dispatch to treat file paths as build commands



213
214
215
216
217
218
219
# File 'lib/sakusei/cli.rb', line 213

def self.dispatch(meth, given_args, given_opts, config)
  # If first arg is an existing file or glob pattern (and not a known subcommand), treat it as a build command
  if given_args.any? && !all_commands.key?(given_args.first) && file_arg?(given_args.first)
    given_args.unshift('build')
  end
  super
end

.file_arg?(arg) ⇒ Boolean

Returns:

  • (Boolean)


221
222
223
224
225
226
227
# File 'lib/sakusei/cli.rb', line 221

def self.file_arg?(arg)
  return false if arg.nil?
  return false if arg.start_with?('-')  # Skip options

  # Check if it's a file (with or without extension), glob pattern, or directory
  File.exist?(arg) || file_with_extension?(arg) || arg.include?('*') || File.directory?(arg)
end

.file_with_extension?(arg) ⇒ Boolean

Check if file exists with any of the supported markdown extensions

Returns:

  • (Boolean)


230
231
232
233
234
235
# File 'lib/sakusei/cli.rb', line 230

def self.file_with_extension?(arg)
  return false if arg.nil? || arg.empty?
  return false if File.extname(arg).length > 0  # Already has an extension

  %w[.md .text .markdown].any? { |ext| File.exist?(arg + ext) }
end

Instance Method Details

#build(*files) ⇒ Object



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
# File 'lib/sakusei/cli.rb', line 27

def build(*files)
  raise Error, 'No input files provided' if files.empty?

  # Resolve file extensions (.md, .text, .markdown) if not provided
  resolved_files = files.map { |f| resolve_file_extension(f) }

  # Check if we have multiple files, globs, or directories
  if resolved_files.length > 1 || resolved_files.any? { |f| f.include?('*') || File.directory?(f) }
    # Multi-file build
    builder = MultiFileBuilder.new(resolved_files, options)
  else
    # Single file build
    raise Error, "File not found: #{resolved_files.first}" unless File.exist?(resolved_files.first)
    builder = Builder.new(resolved_files.first, options)
  end

  output_path = builder.build
  say "PDF created: #{output_path}", :green

  # Open the PDF if requested
  open_pdf(output_path) if options[:open]
rescue Error => e
  say_error e.message
  exit 1
end

#component(name) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/sakusei/cli.rb', line 133

def component(name)
  info = StylePack.find_component(options[:directory], name)

  unless info
    say_error "Component '#{name}' not found"
    exit 1
  end

  say "\n"
  say "" * 60, :cyan
  say "  #{info[:name]}", :cyan
  say "" * 60, :cyan
  say "\n"

  # Description
  if info[:description]
    say "📄 Description:"
    say "   #{info[:description]}"
    say "\n"
  end

  # Location
  say "📁 Location:"
  say "   #{info[:path]}", :cyan
  say "   (in style pack: #{info[:pack_name]})"
  say "\n"

  # Props
  if info[:props]&.any?
    say "⚙️  Props:"
    info[:props].each do |prop|
      req_str = prop[:required] ? 'required' : 'optional'
      default_str = prop[:default] ? " (default: #{prop[:default]})" : ""
      type_str = prop[:type] ? " [#{prop[:type]}]" : ""
      say "#{prop[:name]}#{type_str} - #{req_str}#{default_str}"
    end
    say "\n"
  end

  # Usage
  say "📝 Usage:"
  say "   #{info[:usage]}", :green
  say "\n"

  # Full documentation if available
  if info[:full_description] && info[:full_description].lines.count > 1
    say "📖 Documentation:"
    info[:full_description].lines.each do |line|
      say "   #{line.rstrip}"
    end
    say "\n"
  end

  # Full source option
  if options[:verbose]
    say "🔍 Template:"
    say info[:template] || "(no template)"
    say "\n"
    say "🔍 Script:"
    say info[:script] || "(no script)"
    say "\n"
    say "🔍 Style:"
    say info[:style] || "(no style)"
  else
    say "💡 Use --verbose to see the full component source code"
  end
rescue Error => e
  say_error e.message
  exit 1
end

#components(style = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/sakusei/cli.rb', line 97

def components(style = nil)
  pack = StylePack.discover(options[:directory], style)

  say "\nAvailable Vue components:\n\n"

  pack_components = pack.list_components
  if pack_components.any?
    say "  Style pack: #{pack.name}", :cyan
    pack_components.each do |comp|
      desc_str = comp[:description] || '(no description)'
      say "#{comp[:name].ljust(18)} #{desc_str}"
    end
  else
    say "  Style pack '#{pack.name}' has no Vue components.", :yellow
  end

  if style.nil?
    local_components_dir = File.join(Dir.pwd, 'components')
    if Dir.exist?(local_components_dir)
      say ""
      say "  Local (./components/)", :cyan
      Dir.glob(File.join(local_components_dir, '*.vue')).sort.each do |file|
        name = File.basename(file, '.vue')
        desc_str = StylePack.extract_docs_description(file) || '(no description)'
        say "#{name.ljust(18)} #{desc_str}"
      end
    end
  end
rescue Error => e
  say_error e.message
  exit 1
end

#concat(*files) ⇒ Object



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

def concat(*files)
  raise Error, 'No input files provided' if files.empty?

  PdfConcatenator.new(files, options[:output]).concat
  say "PDFs concatenated: #{options[:output]}", :green
rescue Error => e
  say_error e.message
  exit 1
end

#init(name = 'default') ⇒ Object



55
56
57
58
59
60
61
# File 'lib/sakusei/cli.rb', line 55

def init(name = 'default')
  StylePackInitializer.new(options[:directory], name).run
  say "Style pack '#{name}' created in #{options[:directory]}/.sakusei/style_packs/#{name}", :green
rescue Error => e
  say_error e.message
  exit 1
end

#preview(style = nil) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/sakusei/cli.rb', line 11

def preview(style = nil)
  preview = StylePreview.new(style, options)
  output_path = preview.generate
  say "Style preview generated: #{output_path}", :green
rescue Error => e
  say_error e.message
  exit 1
end

#stylesObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sakusei/cli.rb', line 77

def styles
  style_packs = StylePack.list_available(options[:directory])

  if style_packs.empty?
    say 'No style packs found.', :yellow
    say "Run 'sakusei init <name>' to create a new style pack."
  else
    say 'Available style packs:', :green
    style_packs.each do |pack|
      say "#{pack[:name]}"
      say "    Path: #{pack[:path]}", :cyan
    end
  end
rescue Error => e
  say_error e.message
  exit 1
end

#versionObject



205
206
207
# File 'lib/sakusei/cli.rb', line 205

def version
  say "Sakusei #{Sakusei::VERSION}"
end