Skip to content

Commit bf6cd8f

Browse files
committed
Refactor Generators::AppBase to avoid blind rescues
1 parent be7f922 commit bf6cd8f

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

railties/lib/rails/generators/app_base.rb

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -529,38 +529,30 @@ def using_bun?
529529
using_js_runtime? && %w[bun].include?(options[:javascript])
530530
end
531531

532+
def capture_command(command, pattern)
533+
`#{command}`[pattern]
534+
rescue SystemCallError
535+
nil
536+
end
537+
532538
def node_version
533539
if using_node?
534540
ENV.fetch("NODE_VERSION") do
535-
`node --version`[/\d+\.\d+\.\d+/]
536-
rescue
537-
NODE_LTS_VERSION
541+
capture_command("node --version", /\d+\.\d+\.\d+/) || NODE_LTS_VERSION
538542
end
539543
end
540544
end
541545

542546
def dockerfile_yarn_version
543-
version = begin
544-
`yarn --version`[/\d+\.\d+\.\d+/]
545-
rescue
546-
nil
547-
end
548-
549-
version || "latest"
547+
capture_command("yarn --version", /\d+\.\d+\.\d+/) || "latest"
550548
end
551549

552550
def yarn_through_corepack?
553551
using_node? and "#{dockerfile_yarn_version}" >= "2"
554552
end
555553

556554
def dockerfile_bun_version
557-
version = begin
558-
`bun --version`[/\d+\.\d+\.\d+/]
559-
rescue
560-
nil
561-
end
562-
563-
version || BUN_VERSION
555+
capture_command("bun --version", /\d+\.\d+\.\d+/) || BUN_VERSION
564556
end
565557

566558
def dockerfile_binfile_fixups

0 commit comments

Comments
 (0)