Skip to content

Commit 98fcc29

Browse files
authored
Fix Version Checker's hard-coded path for package.json (#1657)
* check both root & client packages * add logging methods instead of changing raise methods
1 parent 70523e2 commit 98fcc29

File tree

5 files changed

+137
-41
lines changed

5 files changed

+137
-41
lines changed

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ Please follow the recommendations outlined at [keepachangelog.com](http://keepac
1818
### [Unreleased]
1919
Changes since the last non-beta release.
2020

21-
#### Added(https://github.com/AbanoubGhadban).
21+
#### Fixed
22+
23+
- 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.
24+
- 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).
25+
26+
#### Added
2227
- Added streaming server rendering support:
2328
- [PR #1633](https://github.com/shakacode/react_on_rails/pull/1633) by [AbanoubGhadban](https://github.com/AbanoubGhadban).
2429
- New `stream_react_component` helper for adding streamed components to views

lib/react_on_rails/engine.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
module ReactOnRails
66
class Engine < ::Rails::Engine
77
config.to_prepare do
8-
if File.exist?(VersionChecker::NodePackageVersion.package_json_path)
9-
VersionChecker.build.raise_if_gem_and_node_package_versions_differ
10-
end
8+
VersionChecker.build.log_if_gem_and_node_package_versions_differ
119
ReactOnRails::ServerRenderingPool.reset_pool
1210
end
1311
end

lib/react_on_rails/version_checker.rb

+22-19
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,17 @@ def initialize(node_package_version)
1919
# For compatibility, the gem and the node package versions should always match,
2020
# unless the user really knows what they're doing. So we will give a
2121
# warning if they do not.
22-
def raise_if_gem_and_node_package_versions_differ
23-
return if node_package_version.relative_path?
22+
def log_if_gem_and_node_package_versions_differ
23+
return if node_package_version.raw.nil? || node_package_version.relative_path?
24+
return log_node_semver_version_warning if node_package_version.semver_wildcard?
2425

2526
node_major_minor_patch = node_package_version.major_minor_patch
2627
gem_major_minor_patch = gem_major_minor_patch_version
2728
versions_match = node_major_minor_patch[0] == gem_major_minor_patch[0] &&
2829
node_major_minor_patch[1] == gem_major_minor_patch[1] &&
2930
node_major_minor_patch[2] == gem_major_minor_patch[2]
3031

31-
raise_differing_versions_warning unless versions_match
32-
33-
raise_node_semver_version_warning if node_package_version.semver_wildcard?
32+
log_differing_versions_warning unless versions_match
3433
end
3534

3635
private
@@ -46,15 +45,15 @@ def common_error_msg
4645
MSG
4746
end
4847

49-
def raise_differing_versions_warning
50-
msg = "**ERROR** ReactOnRails: ReactOnRails gem and node package versions do not match\n#{common_error_msg}"
51-
raise ReactOnRails::Error, msg
48+
def log_differing_versions_warning
49+
msg = "**WARNING** ReactOnRails: ReactOnRails gem and node package versions do not match\n#{common_error_msg}"
50+
Rails.logger.warn(msg)
5251
end
5352

54-
def raise_node_semver_version_warning
55-
msg = "**ERROR** ReactOnRails: Your node package version for react-on-rails contains a " \
53+
def log_node_semver_version_warning
54+
msg = "**WARNING** ReactOnRails: Your node package version for react-on-rails contains a " \
5655
"^ or ~\n#{common_error_msg}"
57-
raise ReactOnRails::Error, msg
56+
Rails.logger.warn(msg)
5857
end
5958

6059
def gem_version
@@ -74,29 +73,33 @@ def self.build
7473
end
7574

7675
def self.package_json_path
77-
Rails.root.join("client", "package.json")
76+
Rails.root.join(ReactOnRails.configuration.node_modules_location, "package.json")
7877
end
7978

8079
def initialize(package_json)
8180
@package_json = package_json
8281
end
8382

8483
def raw
85-
parsed_package_contents = JSON.parse(package_json_contents)
86-
if parsed_package_contents.key?("dependencies") &&
87-
parsed_package_contents["dependencies"].key?("react-on-rails")
88-
parsed_package_contents["dependencies"]["react-on-rails"]
89-
else
90-
raise ReactOnRails::Error, "No 'react-on-rails' entry in package.json dependencies"
84+
if File.exist?(package_json)
85+
parsed_package_contents = JSON.parse(package_json_contents)
86+
if parsed_package_contents.key?("dependencies") &&
87+
parsed_package_contents["dependencies"].key?("react-on-rails")
88+
return parsed_package_contents["dependencies"]["react-on-rails"]
89+
end
9190
end
91+
msg = "No 'react-on-rails' entry in the dependencies of #{NodePackageVersion.package_json_path}, " \
92+
"which is the expected location according to ReactOnRails.configuration.node_modules_location"
93+
Rails.logger.warn(msg)
94+
nil
9295
end
9396

9497
def semver_wildcard?
9598
raw.match(/[~^]/).present?
9699
end
97100

98101
def relative_path?
99-
raw.match(%r{(\.\.|\Afile:///)}).present?
102+
raw.match(/(\.\.|\Afile:)/).present?
100103
end
101104

102105
def major_minor_patch
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"dependencies": {
3+
"babel": "^6.3.26",
4+
"react-on-rails": "file:.yalc/react-on-rails",
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

+98-18
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def error(message)
1111
end
1212
end
1313

14-
module ReactOnRails
14+
module ReactOnRails # rubocop:disable Metrics/ModuleLength
1515
describe VersionChecker do
1616
describe "#warn_if_gem_and_node_package_versions_differ" do
1717
let(:logger) { FakeLogger.new }
@@ -23,8 +23,10 @@ module ReactOnRails
2323

2424
before { stub_gem_version("2.2.5.beta.2") }
2525

26-
it "does not raise" do
27-
expect { check_version(node_package_version) }.not_to raise_error
26+
it "does not log" do
27+
allow(Rails.logger).to receive(:warn)
28+
check_version_and_log(node_package_version)
29+
expect(Rails.logger).not_to have_received(:warn)
2830
end
2931
end
3032

@@ -35,9 +37,11 @@ module ReactOnRails
3537

3638
before { stub_gem_version("2.2.5") }
3739

38-
it "does raise" do
39-
error = /ReactOnRails: Your node package version for react-on-rails contains a \^ or ~/
40-
expect { check_version(node_package_version) }.to raise_error(error)
40+
it "logs" do
41+
allow(Rails.logger).to receive(:warn)
42+
message = /ReactOnRails: Your node package version for react-on-rails contains a \^ or ~/
43+
check_version_and_log(node_package_version)
44+
expect(Rails.logger).to have_received(:warn).with(message)
4145
end
4246
end
4347

@@ -48,9 +52,11 @@ module ReactOnRails
4852

4953
before { stub_gem_version("12.0.0.beta.1") }
5054

51-
it "raises" do
52-
error = /ReactOnRails: ReactOnRails gem and node package versions do not match/
53-
expect { check_version(node_package_version) }.to raise_error(error)
55+
it "logs" do
56+
allow(Rails.logger).to receive(:warn)
57+
message = /ReactOnRails: ReactOnRails gem and node package versions do not match/
58+
check_version_and_log(node_package_version)
59+
expect(Rails.logger).to have_received(:warn).with(message)
5460
end
5561
end
5662

@@ -61,9 +67,11 @@ module ReactOnRails
6167

6268
before { stub_gem_version("13.1.0") }
6369

64-
it "raises" do
65-
error = /ReactOnRails: ReactOnRails gem and node package versions do not match/
66-
expect { check_version(node_package_version) }.to raise_error(error)
70+
it "logs" do
71+
allow(Rails.logger).to receive(:warn)
72+
message = /ReactOnRails: ReactOnRails gem and node package versions do not match/
73+
check_version_and_log(node_package_version)
74+
expect(Rails.logger).to have_received(:warn).with(message)
6775
end
6876
end
6977

@@ -74,9 +82,11 @@ module ReactOnRails
7482

7583
before { stub_gem_version("13.0.0") }
7684

77-
it "raises" do
78-
error = /ReactOnRails: ReactOnRails gem and node package versions do not match/
79-
expect { check_version(node_package_version) }.to raise_error(error)
85+
it "logs" do
86+
allow(Rails.logger).to receive(:warn)
87+
message = /ReactOnRails: ReactOnRails gem and node package versions do not match/
88+
check_version_and_log(node_package_version)
89+
expect(Rails.logger).to have_received(:warn).with(message)
8090
end
8191
end
8292

@@ -87,8 +97,20 @@ module ReactOnRails
8797

8898
before { stub_gem_version("2.0.0.beta.1") }
8999

90-
it "does not raise" do
91-
expect { check_version(node_package_version) }.not_to raise_error
100+
it "does not log" do
101+
allow(Rails.logger).to receive(:warn)
102+
check_version_and_log(node_package_version)
103+
expect(Rails.logger).not_to have_received(:warn)
104+
end
105+
end
106+
107+
context "when package json doesn't exist" do
108+
let(:node_package_version) do
109+
double_package_version(raw: nil)
110+
end
111+
112+
it "log method returns nil" do
113+
expect(check_version_and_log(node_package_version)).to be_nil
92114
end
93115
end
94116
end
@@ -102,14 +124,32 @@ def double_package_version(raw: nil, semver_wildcard: false,
102124
relative_path?: relative_path)
103125
end
104126

105-
def check_version(node_package_version)
127+
def check_version_and_raise(node_package_version)
106128
version_checker = VersionChecker.new(node_package_version)
107129
version_checker.raise_if_gem_and_node_package_versions_differ
108130
end
109131

132+
def check_version_and_log(node_package_version)
133+
version_checker = VersionChecker.new(node_package_version)
134+
version_checker.log_if_gem_and_node_package_versions_differ
135+
end
136+
110137
describe VersionChecker::NodePackageVersion do
111138
subject(:node_package_version) { described_class.new(package_json) }
112139

140+
describe "#build" do
141+
it "initializes NodePackageVersion with ReactOnRails.configuration.node_modules_location" do
142+
allow(ReactOnRails).to receive_message_chain(:configuration, :node_modules_location).and_return("spec/dummy")
143+
root_package_json_path = File.expand_path("../../package.json", __dir__)
144+
allow(Rails).to receive_message_chain(:root, :join).and_return(root_package_json_path)
145+
message = "No 'react-on-rails' entry in the dependencies of #{root_package_json_path}, which is " \
146+
"the expected location according to ReactOnRails.configuration.node_modules_location"
147+
allow(Rails.logger).to receive(:warn)
148+
described_class.build.raw
149+
expect(Rails.logger).to have_received(:warn).with(message)
150+
end
151+
end
152+
113153
describe "#semver_wildcard?" do
114154
context "when package json lists an exact version of '0.0.2'" do
115155
let(:package_json) { File.expand_path("fixtures/normal_package.json", __dir__) }
@@ -193,6 +233,46 @@ def check_version(node_package_version)
193233
specify { expect(node_package_version.major_minor_patch).to be_nil }
194234
end
195235
end
236+
237+
context "with node version of 'file:.yalc/react-on-rails'" do
238+
let(:package_json) { File.expand_path("fixtures/yalc_package.json", __dir__) }
239+
240+
describe "#raw" do
241+
specify { expect(node_package_version.raw).to eq("file:.yalc/react-on-rails") }
242+
end
243+
244+
describe "#relative_path?" do
245+
specify { expect(node_package_version.relative_path?).to be true }
246+
end
247+
248+
describe "#major" do
249+
specify { expect(node_package_version.major_minor_patch).to be_nil }
250+
end
251+
end
252+
253+
context "with package.json without react-on-rails dependency" do
254+
let(:package_json) { File.expand_path("../../package.json", __dir__) }
255+
256+
describe "#raw" do
257+
it "returns nil" do
258+
root_package_json_path = File.expand_path("fixtures/nonexistent_package.json", __dir__)
259+
allow(Rails).to receive_message_chain(:root, :join).and_return(root_package_json_path)
260+
expect(node_package_version.raw).to be_nil
261+
end
262+
end
263+
end
264+
265+
context "with non-existing package.json" do
266+
let(:package_json) { File.expand_path("fixtures/nonexistent_package.json", __dir__) }
267+
268+
describe "#raw" do
269+
it "returns nil" do
270+
root_package_json_path = File.expand_path("fixtures/nonexistent_package.json", __dir__)
271+
allow(Rails).to receive_message_chain(:root, :join).and_return(root_package_json_path)
272+
expect(node_package_version.raw).to be_nil
273+
end
274+
end
275+
end
196276
end
197277
end
198278
end

0 commit comments

Comments
 (0)