Skip to content

Commit 3dbbbdd

Browse files
authored
Ruby CLI: Expose Node.js binaries in Herb Ruby CLI (#1192)
```diff Commands: bundle exec herb lex [file] Lex a file. bundle exec herb parse [file] Parse a file. bundle exec herb compile [file] Compile ERB template to Ruby code. bundle exec herb render [file] Compile and render ERB template to final output. bundle exec herb analyze [path] Analyze a project by passing a directory to the root of the project bundle exec herb config [path] Show configuration and file patterns for a project bundle exec herb ruby [file] Extract Ruby from a file. bundle exec herb html [file] Extract HTML from a file. bundle exec herb playground [file] Open the content of the source file in the playground bundle exec herb version Prints the versions of the Herb gem and the libherb library. + bundle exec herb lint [patterns] Lint templates (delegates to @herb-tools/linter) + bundle exec herb format [patterns] Format templates (delegates to @herb-tools/formatter) + bundle exec herb highlight [file] Syntax highlight templates (delegates to @herb-tools/highlighter) + bundle exec herb print [file] Print AST (delegates to @herb-tools/printer) + bundle exec herb lsp Start the language server (delegates to @herb-tools/language-server) ``` **`bundle exec herb lint examples/if_else.html.erb`** ``` Node.js: v22.13.0 Running: /Users/marcoroth/Development/herb-release-0.8.9/node_modules/.bin/herb-lint examples/if_else.html.erb ✓ examples/if_else.html.erb - No issues found Summary: Checked 1 file Offenses 0 offenses Start at 17:48:14 Duration 79ms (51 rules) ``` Related #475
1 parent 324b1f5 commit 3dbbbdd

1 file changed

Lines changed: 75 additions & 11 deletions

File tree

lib/herb/cli.rb

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,22 @@ def help(exit_code = 0)
9595
bundle exec herb [command] [options]
9696
9797
Commands:
98-
bundle exec herb lex [file] Lex a file.
99-
bundle exec herb parse [file] Parse a file.
100-
bundle exec herb compile [file] Compile ERB template to Ruby code.
101-
bundle exec herb render [file] Compile and render ERB template to final output.
102-
bundle exec herb analyze [path] Analyze a project by passing a directory to the root of the project
103-
bundle exec herb config [path] Show configuration and file patterns for a project
104-
bundle exec herb ruby [file] Extract Ruby from a file.
105-
bundle exec herb html [file] Extract HTML from a file.
106-
bundle exec herb prism [file] Extract Ruby from a file and parse the Ruby source with Prism.
107-
bundle exec herb playground [file] Open the content of the source file in the playground
108-
bundle exec herb version Prints the versions of the Herb gem and the libherb library.
98+
bundle exec herb lex [file] Lex a file.
99+
bundle exec herb parse [file] Parse a file.
100+
bundle exec herb compile [file] Compile ERB template to Ruby code.
101+
bundle exec herb render [file] Compile and render ERB template to final output.
102+
bundle exec herb analyze [path] Analyze a project by passing a directory to the root of the project
103+
bundle exec herb config [path] Show configuration and file patterns for a project
104+
bundle exec herb ruby [file] Extract Ruby from a file.
105+
bundle exec herb html [file] Extract HTML from a file.
106+
bundle exec herb playground [file] Open the content of the source file in the playground
107+
bundle exec herb version Prints the versions of the Herb gem and the libherb library.
108+
109+
bundle exec herb lint [patterns] Lint templates (delegates to @herb-tools/linter)
110+
bundle exec herb format [patterns] Format templates (delegates to @herb-tools/formatter)
111+
bundle exec herb highlight [file] Syntax highlight templates (delegates to @herb-tools/highlighter)
112+
bundle exec herb print [file] Print AST (delegates to @herb-tools/printer)
113+
bundle exec herb lsp Start the language server (delegates to @herb-tools/language-server)
109114
110115
stdin:
111116
Commands that accept [file] also accept input via stdin:
@@ -171,6 +176,16 @@ def result
171176
system(%(open "#{url}##{hash}"))
172177
exit(0)
173178
end
179+
when "lint"
180+
run_node_tool("herb-lint", "@herb-tools/linter")
181+
when "format"
182+
run_node_tool("herb-format", "@herb-tools/formatter")
183+
when "print"
184+
run_node_tool("herb-print", "@herb-tools/printer")
185+
when "highlight"
186+
run_node_tool("herb-highlight", "@herb-tools/highlighter")
187+
when "lsp"
188+
run_node_tool("herb-language-server", "@herb-tools/language-server")
174189
when "help"
175190
help
176191
when "version"
@@ -253,11 +268,60 @@ def option_parser
253268
end
254269

255270
def options
271+
return if ["lint", "format", "print", "highlight", "lsp"].include?(@command)
272+
256273
option_parser.parse!(@args)
257274
end
258275

259276
private
260277

278+
def find_node_binary(name)
279+
local_bin = File.join(Dir.pwd, "node_modules", ".bin", name)
280+
return local_bin if File.executable?(local_bin)
281+
282+
path_result = `which #{name} 2>/dev/null`.strip
283+
return path_result unless path_result.empty?
284+
285+
nil
286+
end
287+
288+
def node_available?
289+
system("which node > /dev/null 2>&1")
290+
end
291+
292+
def run_node_tool(binary_name, package_name)
293+
unless node_available?
294+
warn "Error: Node.js is required to run 'herb #{@command}'."
295+
warn ""
296+
warn "Install the tool:"
297+
warn " npm install #{package_name}"
298+
warn " yarn add #{package_name}"
299+
warn " pnpm add #{package_name}"
300+
warn " bun add #{package_name}"
301+
warn ""
302+
warn "Or install Node.js from https://nodejs.org"
303+
exit 1
304+
end
305+
306+
remaining_args = @args[1..]
307+
binary = find_node_binary(binary_name)
308+
node_version = `node --version 2>/dev/null`.strip
309+
310+
command_parts = if binary
311+
[binary, *remaining_args]
312+
else
313+
["npx", package_name, *remaining_args]
314+
end
315+
316+
escaped_command = command_parts.map { |arg| arg.include?(" ") ? "\"#{arg}\"" : arg }.join(" ")
317+
318+
warn "Node.js: #{node_version}"
319+
warn "Running: #{escaped_command}"
320+
warn ""
321+
322+
exec(*command_parts)
323+
end
324+
261325
def print_error_summary(errors)
262326
puts
263327
puts white("#{bold(red("Errors"))} #{dimmed("(#{errors.size} total)")}")

0 commit comments

Comments
 (0)