Skip to content

Commit 1cee1e5

Browse files
justin808claude
andcommitted
Convert Ruby gem version format to npm semver format
Ruby gem versions use dots for pre-release versions (e.g., 16.2.0.beta.10) while npm requires hyphens (16.2.0-beta.10) to match semver spec. This commit fixes the generator to convert between the two formats so that beta/RC/alpha versions are installed correctly. Before this fix: - Generator would try to install [email protected] (invalid npm version) - This would fail to match any published version - yalc link would work, but package.json would have wrong version - Version checker would then fail due to version mismatch After this fix: - Generator converts 16.2.0.beta.10 → 16.2.0-beta.10 - npm installs the correct version - Version checker validates successfully The fix also handles both formats as input (dot or hyphen) and normalizes to the correct npm format. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 4304d81 commit 1cee1e5

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

lib/generators/react_on_rails/js_dependency_manager.rb

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,32 @@ def add_js_dependencies
124124

125125
def add_react_on_rails_package
126126
# Use exact version match between gem and npm package for all versions including pre-releases
127+
# Ruby gem versions use dots (16.2.0.beta.10) but npm requires hyphens (16.2.0-beta.10)
128+
# This method converts between the two formats.
129+
#
127130
# The regex matches:
128131
# - Stable: 16.2.0
129-
# - Beta: 16.2.0-beta.10
130-
# - RC: 16.1.0-rc.1
131-
# - Alpha: 16.0.0-alpha.5
132+
# - Beta (Ruby): 16.2.0.beta.10 or (npm): 16.2.0-beta.10
133+
# - RC (Ruby): 16.1.0.rc.1 or (npm): 16.1.0-rc.1
134+
# - Alpha (Ruby): 16.0.0.alpha.5 or (npm): 16.0.0-alpha.5
132135
# This ensures beta/rc versions use the exact version instead of "latest" which would
133136
# install the latest stable release and cause version mismatches.
134-
version_with_optional_prerelease = /\A\d+\.\d+\.\d+(-[a-zA-Z0-9.]+)?\z/
135-
react_on_rails_pkg = if ReactOnRails::VERSION.match?(version_with_optional_prerelease)
136-
"react-on-rails@#{ReactOnRails::VERSION}"
137+
138+
# Accept both dot and hyphen separators for pre-release versions
139+
version_with_optional_prerelease = /\A(\d+\.\d+\.\d+)([-.]([a-zA-Z0-9.]+))?\z/
140+
141+
react_on_rails_pkg = if (match = ReactOnRails::VERSION.match(version_with_optional_prerelease))
142+
base_version = match[1]
143+
prerelease = match[3]
144+
145+
# Convert Ruby gem format (dot) to npm semver format (hyphen)
146+
npm_version = if prerelease
147+
"#{base_version}-#{prerelease}"
148+
else
149+
base_version
150+
end
151+
152+
"react-on-rails@#{npm_version}"
137153
else
138154
puts "WARNING: Unrecognized version format #{ReactOnRails::VERSION}. " \
139155
"Adding the latest react-on-rails NPM module. " \

spec/react_on_rails/generators/js_dependency_manager_spec.rb

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,19 +208,37 @@ def errors
208208
expect(mock_manager).to have_received(:add).with(["[email protected]"], exact: true)
209209
end
210210

211-
it "adds react-on-rails with version for RC pre-releases" do
211+
it "adds react-on-rails with version for RC pre-releases (npm format with hyphen)" do
212212
stub_const("ReactOnRails::VERSION", "16.0.0-rc.1")
213213
instance.send(:add_react_on_rails_package)
214214
expect(mock_manager).to have_received(:add).with(["[email protected]"], exact: true)
215215
end
216216

217-
it "adds react-on-rails with version for beta pre-releases" do
217+
it "converts Ruby gem beta format to npm format" do
218+
stub_const("ReactOnRails::VERSION", "16.2.0.beta.10")
219+
instance.send(:add_react_on_rails_package)
220+
expect(mock_manager).to have_received(:add).with(["[email protected]"], exact: true)
221+
end
222+
223+
it "converts Ruby gem RC format to npm format" do
224+
stub_const("ReactOnRails::VERSION", "16.0.0.rc.1")
225+
instance.send(:add_react_on_rails_package)
226+
expect(mock_manager).to have_received(:add).with(["[email protected]"], exact: true)
227+
end
228+
229+
it "converts Ruby gem alpha format to npm format" do
230+
stub_const("ReactOnRails::VERSION", "16.0.0.alpha.5")
231+
instance.send(:add_react_on_rails_package)
232+
expect(mock_manager).to have_received(:add).with(["[email protected]"], exact: true)
233+
end
234+
235+
it "accepts npm format beta pre-releases (already with hyphen)" do
218236
stub_const("ReactOnRails::VERSION", "16.2.0-beta.10")
219237
instance.send(:add_react_on_rails_package)
220238
expect(mock_manager).to have_received(:add).with(["[email protected]"], exact: true)
221239
end
222240

223-
it "adds react-on-rails with version for alpha releases" do
241+
it "accepts npm format alpha releases (already with hyphen)" do
224242
stub_const("ReactOnRails::VERSION", "16.0.0-alpha.5")
225243
instance.send(:add_react_on_rails_package)
226244
expect(mock_manager).to have_received(:add).with(["[email protected]"], exact: true)

0 commit comments

Comments
 (0)