Skip to content

Commit 4339d30

Browse files
alexeyr-cialexeyr
andauthored
Enable use as a Git dependency (#1664)
* Enable use as a Git dependency * Allow URLs and other local paths in version checker --------- Co-authored-by: Alexey Romanov <alexey.v.romanov@gmail.com>
1 parent 98fcc29 commit 4339d30

File tree

8 files changed

+92
-25
lines changed

8 files changed

+92
-25
lines changed

CHANGELOG.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ Changes since the last non-beta release.
2020

2121
#### Fixed
2222

23+
- Incorrect type and confusing name for `ReactOnRails.registerStore`, use `registerStoreGenerators` instead. [PR 1651](https://github.com/shakacode/react_on_rails/pull/1651) by [alexeyr-ci](https://github.com/alexeyr-ci).
2324
- Changed the ReactOnRails' version checker to use `ReactOnRails.configuration.node_modules_location` to determine the location of the package.json that the `react-on-rails` dependency is expected to be set by.
2425
- Also, all errors that would be raised by the version checking have been converted to `Rails.Logger` warnings to avoid any breaking changes. [PR 1657](https://github.com/shakacode/react_on_rails/pull/1657) by [judahmeek](https://github.com/judahmeek).
26+
- Enable use as a `git:` dependency. [PR 1664](https://github.com/shakacode/react_on_rails/pull/1664) by [alexeyr-ci](https://github.com/alexeyr-ci).
2527

2628
#### Added
2729
- Added streaming server rendering support:
@@ -35,9 +37,6 @@ Changes since the last non-beta release.
3537
#### Changed
3638
- Console replay script generation now awaits the render request promise before generating, allowing it to capture console logs from asynchronous operations. This requires using a version of the Node renderer that supports replaying async console logs. [PR #1649](https://github.com/shakacode/react_on_rails/pull/1649) by [AbanoubGhadban](https://github.com/AbanoubGhadban).
3739

38-
#### Fixed
39-
- Incorrect type and confusing name for `ReactOnRails.registerStore`, use `registerStoreGenerators` instead. [PR 1651](https://github.com/shakacode/react_on_rails/pull/1651) by [alexeyr-ci](https://github.com/alexeyr-ci).
40-
4140
### [14.0.5] - 2024-08-20
4241
#### Fixed
4342
- Should force load react-components which send over turbo-stream [PR #1620](https://github.com/shakacode/react_on_rails/pull/1620) by [theforestvn88](https://github.com/theforestvn88).

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
react_on_rails (14.0.5)
4+
react_on_rails (15.0.0.alpha.1)
55
addressable
66
connection_pool
77
execjs (~> 2.5)

lib/react_on_rails/version_checker.rb

+16-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def initialize(node_package_version)
2020
# unless the user really knows what they're doing. So we will give a
2121
# warning if they do not.
2222
def log_if_gem_and_node_package_versions_differ
23-
return if node_package_version.raw.nil? || node_package_version.relative_path?
23+
return if node_package_version.raw.nil? || node_package_version.local_path_or_url?
2424
return log_node_semver_version_warning if node_package_version.semver_wildcard?
2525

2626
node_major_minor_patch = node_package_version.major_minor_patch
@@ -81,33 +81,41 @@ def initialize(package_json)
8181
end
8282

8383
def raw
84+
return @raw if defined?(@raw)
85+
8486
if File.exist?(package_json)
8587
parsed_package_contents = JSON.parse(package_json_contents)
8688
if parsed_package_contents.key?("dependencies") &&
8789
parsed_package_contents["dependencies"].key?("react-on-rails")
88-
return parsed_package_contents["dependencies"]["react-on-rails"]
90+
return @raw = parsed_package_contents["dependencies"]["react-on-rails"]
8991
end
9092
end
9193
msg = "No 'react-on-rails' entry in the dependencies of #{NodePackageVersion.package_json_path}, " \
9294
"which is the expected location according to ReactOnRails.configuration.node_modules_location"
9395
Rails.logger.warn(msg)
94-
nil
96+
@raw = nil
9597
end
9698

9799
def semver_wildcard?
98-
raw.match(/[~^]/).present?
100+
# See https://docs.npmjs.com/cli/v10/configuring-npm/package-json#dependencies
101+
# We want to disallow all expressions other than exact versions
102+
# and the ones allowed by local_path_or_url?
103+
raw.blank? || raw.match(/[~^><|*-]/).present?
99104
end
100105

101-
def relative_path?
102-
raw.match(/(\.\.|\Afile:)/).present?
106+
def local_path_or_url?
107+
# See https://docs.npmjs.com/cli/v10/configuring-npm/package-json#dependencies
108+
# All path and protocol "version ranges" include / somewhere,
109+
# but we want to make an exception for npm:@scope/pkg@version.
110+
!raw.nil? && raw.include?("/") && !raw.start_with?("npm:")
103111
end
104112

105113
def major_minor_patch
106-
return if relative_path?
114+
return if local_path_or_url?
107115

108116
match = raw.match(MAJOR_MINOR_PATCH_VERSION_REGEX)
109117
unless match
110-
raise ReactOnRails::Error, "Cannot parse version number '#{raw}' (wildcard versions are not supported)"
118+
raise ReactOnRails::Error, "Cannot parse version number '#{raw}' (only exact versions are supported)"
111119
end
112120

113121
[match[1], match[2], match[3]]

package-scripts.yml

+16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ scripts:
1414
description: Run eslint in debug mode.
1515
script: DEBUG=eslint:cli-engine nps eslint
1616

17+
build:
18+
prepack:
19+
description: Build the project in the prepack script.
20+
# This is necessary when used as a Git dependency since we don't have the dist directory in the repo.
21+
# Depending on the package manager, `prepack`, `prepare`, or both may be run.
22+
# They may also be run when publishing or in other cases, so we want to cover all of them.
23+
# 1. If the project is already built, do nothing;
24+
# 2. Build the project but ignore TypeScript errors from missing devDependencies;
25+
# 3. Check if the project is built now;
26+
# 4. If it failed, print an error message (still follow https://docs.npmjs.com/cli/v8/using-npm/scripts#best-practices).
27+
script: >
28+
[ -f node_package/lib/ReactOnRails.js ] ||
29+
(npm run build >/dev/null 2>&1 || true) &&
30+
[ -f node_package/lib/ReactOnRails.js ] ||
31+
{ echo 'Building react-on-rails seems to have failed!'; }
32+
1733
format:
1834
default:
1935
description: Format files using prettier.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
"test": "jest node_package/tests",
6262
"clean": "rm -rf node_package/lib",
6363
"start": "nps",
64+
"prepack": "nps build.prepack",
65+
"prepare": "nps build.prepack",
6466
"prepublishOnly": "yarn run build",
6567
"build": "yarn run clean && yarn run tsc --declaration",
6668
"build-watch": "yarn run clean && yarn run tsc --watch",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"dependencies": {
3+
"babel": "^6.3.26",
4+
"react-on-rails": "git://github.com/shakacode/react-on-rails.git",
5+
"webpack": "^1.12.8"
6+
},
7+
"devDependencies": {
8+
"babel-eslint": "^5.0.0-beta6"
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"dependencies": {
3+
"babel": "^6.3.26",
4+
"react-on-rails": ">=1.0.0 <2.0.0",
5+
"webpack": "^1.12.8"
6+
},
7+
"devDependencies": {
8+
"babel-eslint": "^5.0.0-beta6"
9+
}
10+
}

spec/react_on_rails/version_checker_spec.rb

+35-13
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module ReactOnRails # rubocop:disable Metrics/ModuleLength
9292

9393
context "when package json uses a relative path with dots" do
9494
let(:node_package_version) do
95-
double_package_version(raw: "../../..", major_minor_patch: "", relative_path: true)
95+
double_package_version(raw: "../../..", major_minor_patch: "", local_path_or_url: true)
9696
end
9797

9898
before { stub_gem_version("2.0.0.beta.1") }
@@ -116,12 +116,12 @@ module ReactOnRails # rubocop:disable Metrics/ModuleLength
116116
end
117117

118118
def double_package_version(raw: nil, semver_wildcard: false,
119-
major_minor_patch: nil, relative_path: false)
119+
major_minor_patch: nil, local_path_or_url: false)
120120
instance_double(VersionChecker::NodePackageVersion,
121121
raw: raw,
122122
semver_wildcard?: semver_wildcard,
123123
major_minor_patch: major_minor_patch,
124-
relative_path?: relative_path)
124+
local_path_or_url?: local_path_or_url)
125125
end
126126

127127
def check_version_and_raise(node_package_version)
@@ -168,6 +168,12 @@ def check_version_and_log(node_package_version)
168168

169169
specify { expect(node_package_version.semver_wildcard?).to be true }
170170
end
171+
172+
context "when package json lists a version range of '>=1.2.3 <2.0.0'" do
173+
let(:package_json) { File.expand_path("fixtures/semver_range_package.json", __dir__) }
174+
175+
specify { expect(node_package_version.semver_wildcard?).to be true }
176+
end
171177
end
172178

173179
context "when package json lists a version of '0.0.2'" do
@@ -177,8 +183,8 @@ def check_version_and_log(node_package_version)
177183
specify { expect(node_package_version.raw).to eq("0.0.2") }
178184
end
179185

180-
describe "#relative_path?" do
181-
specify { expect(node_package_version.relative_path?).to be false }
186+
describe "#local_path_or_url?" do
187+
specify { expect(node_package_version.local_path_or_url?).to be false }
182188
end
183189

184190
describe "#major" do
@@ -193,8 +199,8 @@ def check_version_and_log(node_package_version)
193199
specify { expect(node_package_version.raw).to eq("^14.0.0.beta-2") }
194200
end
195201

196-
describe "#relative_path?" do
197-
specify { expect(node_package_version.relative_path?).to be false }
202+
describe "#local_path_or_url?" do
203+
specify { expect(node_package_version.local_path_or_url?).to be false }
198204
end
199205

200206
describe "#major_minor_patch" do
@@ -209,8 +215,8 @@ def check_version_and_log(node_package_version)
209215
specify { expect(node_package_version.raw).to eq("../../..") }
210216
end
211217

212-
describe "#relative_path?" do
213-
specify { expect(node_package_version.relative_path?).to be true }
218+
describe "#local_path_or_url?" do
219+
specify { expect(node_package_version.local_path_or_url?).to be true }
214220
end
215221

216222
describe "#major" do
@@ -225,8 +231,8 @@ def check_version_and_log(node_package_version)
225231
specify { expect(node_package_version.raw).to eq("file:///Users/justin/shakacode/react_on_rails") }
226232
end
227233

228-
describe "#relative_path?" do
229-
specify { expect(node_package_version.relative_path?).to be true }
234+
describe "#local_path_or_url?" do
235+
specify { expect(node_package_version.local_path_or_url?).to be true }
230236
end
231237

232238
describe "#major" do
@@ -241,8 +247,24 @@ def check_version_and_log(node_package_version)
241247
specify { expect(node_package_version.raw).to eq("file:.yalc/react-on-rails") }
242248
end
243249

244-
describe "#relative_path?" do
245-
specify { expect(node_package_version.relative_path?).to be true }
250+
describe "#local_path_or_url?" do
251+
specify { expect(node_package_version.local_path_or_url?).to be true }
252+
end
253+
254+
describe "#major" do
255+
specify { expect(node_package_version.major_minor_patch).to be_nil }
256+
end
257+
end
258+
259+
context "with node version of `git:` URL" do
260+
let(:package_json) { File.expand_path("fixtures/git_package.json", __dir__) }
261+
262+
describe "#raw" do
263+
specify { expect(node_package_version.raw).to eq("git://github.com/shakacode/react-on-rails.git") }
264+
end
265+
266+
describe "#local_path_or_url?" do
267+
specify { expect(node_package_version.local_path_or_url?).to be true }
246268
end
247269

248270
describe "#major" do

0 commit comments

Comments
 (0)