Skip to content

Commit b8d22ce

Browse files
authored
Merge pull request rails#55205 from zzak/re-53174
Ensure yarn and bun version fallback
2 parents 3fd19bd + bf6cd8f commit b8d22ce

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

railties/lib/rails/generators/app_base.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -529,31 +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-
using_node? and `yarn --version`[/\d+\.\d+\.\d+/]
544-
rescue
545-
"latest"
547+
capture_command("yarn --version", /\d+\.\d+\.\d+/) || "latest"
546548
end
547549

548550
def yarn_through_corepack?
549-
true if dockerfile_yarn_version == "latest"
550-
dockerfile_yarn_version >= "2"
551+
using_node? and "#{dockerfile_yarn_version}" >= "2"
551552
end
552553

553554
def dockerfile_bun_version
554-
using_bun? and `bun --version`[/\d+\.\d+\.\d+/]
555-
rescue
556-
BUN_VERSION
555+
capture_command("bun --version", /\d+\.\d+\.\d+/) || BUN_VERSION
557556
end
558557

559558
def dockerfile_binfile_fixups

railties/test/generators/app_generator_test.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,22 @@ def test_esbuild_option_with_js_argument
925925
assert_gem "jsbundling-rails"
926926
end
927927

928+
def test_esbuild_without_yarn_installed
929+
generator([destination_root], javascript: "esbuild")
930+
931+
# fallback to latest when yarn is not installed
932+
generator.stub :dockerfile_yarn_version, "latest" do
933+
quietly { generator.invoke_all }
934+
end
935+
936+
assert_gem "jsbundling-rails"
937+
assert_file "Dockerfile" do |content|
938+
assert_match(/ARG YARN_VERSION=latest/, content)
939+
940+
assert_match("RUN corepack enable && yarn set version $YARN_VERSION", content)
941+
end
942+
end
943+
928944
def test_bun_option
929945
generator([destination_root], javascript: "bun")
930946

@@ -949,6 +965,23 @@ def test_bun_option_with_js_argument
949965
assert_gem "jsbundling-rails"
950966
end
951967

968+
def test_bun_without_bun_installed
969+
generator([destination_root], javascript: "bun")
970+
bun_version = generator.class.const_get(:BUN_VERSION)
971+
972+
# fallback to constant when bun is not installed
973+
generator.stub :dockerfile_bun_version, bun_version do
974+
quietly { generator.invoke_all }
975+
end
976+
977+
assert_gem "jsbundling-rails"
978+
assert_file "Dockerfile" do |content|
979+
assert_match(/ARG BUN_VERSION=#{bun_version}/, content)
980+
981+
assert_match("RUN bun install --frozen-lockfile", content)
982+
end
983+
end
984+
952985
def test_skip_javascript_option_with_skip_javascript_argument
953986
run_generator [destination_root, "--skip-javascript"]
954987
assert_no_gem "stimulus-rails"

0 commit comments

Comments
 (0)