Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
Judahmeek committed Dec 7, 2024
1 parent 9a07b64 commit 72c5f60
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 80 deletions.
5 changes: 1 addition & 4 deletions lib/react_on_rails/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
module ReactOnRails
class Engine < ::Rails::Engine
config.to_prepare do
if VersionChecker.instance("package.json").log_if_gem_and_node_package_versions_differ &&
VersionChecker.instance("client/package.json").log_if_gem_and_node_package_versions_differ
Rails.logger.warn("No 'react-on-rails' entry found in 'dependencies' in package.json or client/package.json.")
end
VersionChecker.build.log_if_gem_and_node_package_versions_differ
ReactOnRails::ServerRenderingPool.reset_pool
end
end
Expand Down
51 changes: 13 additions & 38 deletions lib/react_on_rails/version_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,21 @@ def initialize(node_package_version)
# For compatibility, the gem and the node package versions should always match,
# unless the user really knows what they're doing. So we will give a
# warning if they do not.
def raise_if_gem_and_node_package_versions_differ
return true unless node_package_version.raw
return if node_package_version.relative_path?

raise_node_semver_version_warning if node_package_version.semver_wildcard?

versions_match = compare_versions(node_package_version.major_minor_patch, gem_major_minor_patch_version)

raise_differing_versions_warning unless versions_match
false
end

def log_if_gem_and_node_package_versions_differ
return true unless node_package_version.raw
return if node_package_version.relative_path?

log_node_semver_version_warning if node_package_version.semver_wildcard?
return if node_package_version.raw.nil? || node_package_version.relative_path?
return log_node_semver_version_warning if node_package_version.semver_wildcard?

versions_match = compare_versions(node_package_version.major_minor_patch, gem_major_minor_patch_version)
node_major_minor_patch = node_package_version.major_minor_patch
gem_major_minor_patch = gem_major_minor_patch_version
versions_match = node_major_minor_patch[0] == gem_major_minor_patch[0] &&
node_major_minor_patch[1] == gem_major_minor_patch[1] &&
node_major_minor_patch[2] == gem_major_minor_patch[2]

log_differing_versions_warning unless versions_match
false
end

private

def compare_versions(node_major_minor_patch, gem_major_minor_patch)
node_major_minor_patch[0] == gem_major_minor_patch[0] &&
node_major_minor_patch[1] == gem_major_minor_patch[1] &&
node_major_minor_patch[2] == gem_major_minor_patch[2]
end

def common_error_msg
<<-MSG.strip_heredoc
Detected: #{node_package_version.raw}
Expand All @@ -63,21 +46,9 @@ def common_error_msg
your installed node package. Do not use >= or ~> in your Gemfile for react_on_rails.
Do not use ^ or ~ in your package.json for react-on-rails.
Run `yarn add react-on-rails --exact` in the directory containing folder node_modules.
***This warning will become a fatal error in ReactOnRails v15***
MSG
end

def raise_differing_versions_warning
msg = "**WARNING** ReactOnRails: ReactOnRails gem and node package versions do not match\n#{common_error_msg}"
raise ReactOnRails::Error, msg
end

def raise_node_semver_version_warning
msg = "**WARNING** ReactOnRails: Your node package version for react-on-rails contains a " \
"^ or ~\n#{common_error_msg}"
raise ReactOnRails::Error, msg
end

def log_differing_versions_warning
msg = "**WARNING** ReactOnRails: ReactOnRails gem and node package versions do not match\n#{common_error_msg}"
Rails.logger.warn(msg)
Expand Down Expand Up @@ -105,8 +76,8 @@ def self.build
new(package_json_path)
end

def self.package_json_path(relative_path = "package.json")
Rails.root.join(relative_path)
def self.package_json_path
Rails.root.join(ReactOnRails.configuration.node_modules_location, "package.json")
end

def initialize(package_json)
Expand All @@ -120,6 +91,10 @@ def raw
if parsed_package_contents.key?("dependencies") &&
parsed_package_contents["dependencies"].key?("react-on-rails")
parsed_package_contents["dependencies"]["react-on-rails"]
else
raise ReactOnRails::Error,
"No 'react-on-rails' entry in the dependencies of #{NodePackageVersion.package_json_path}, " \
"which is the expected location according to ReactOnRails.configuration.node_modules_location"
end
end

Expand Down
51 changes: 13 additions & 38 deletions spec/react_on_rails/version_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ module ReactOnRails # rubocop:disable Metrics/ModuleLength

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

it "does not raise" do
expect { check_version_and_raise(node_package_version) }.not_to raise_error
end

it "does not log" do
allow(Rails.logger).to receive(:warn)
check_version_and_log(node_package_version)
Expand All @@ -47,12 +43,6 @@ module ReactOnRails # rubocop:disable Metrics/ModuleLength
check_version_and_log(node_package_version)
expect(Rails.logger).to have_received(:warn).with(message)
end

it "raises" do
allow(Rails.logger).to receive(:warn)
message = /ReactOnRails: Your node package version for react-on-rails contains a \^ or ~/
expect { check_version_and_raise(node_package_version) }.to raise_error(message)
end
end

context "when gem and node package major versions differ" do
Expand All @@ -68,12 +58,6 @@ module ReactOnRails # rubocop:disable Metrics/ModuleLength
check_version_and_log(node_package_version)
expect(Rails.logger).to have_received(:warn).with(message)
end

it "raises" do
allow(Rails.logger).to receive(:warn)
message = /ReactOnRails: ReactOnRails gem and node package versions do not match/
expect { check_version_and_raise(node_package_version) }.to raise_error(message)
end
end

context "when gem and node package major versions match and minor differs" do
Expand All @@ -89,12 +73,6 @@ module ReactOnRails # rubocop:disable Metrics/ModuleLength
check_version_and_log(node_package_version)
expect(Rails.logger).to have_received(:warn).with(message)
end

it "raises" do
allow(Rails.logger).to receive(:warn)
message = /ReactOnRails: ReactOnRails gem and node package versions do not match/
expect { check_version_and_raise(node_package_version) }.to raise_error(message)
end
end

context "when gem and node package major, minor versions match and patch differs" do
Expand All @@ -110,12 +88,6 @@ module ReactOnRails # rubocop:disable Metrics/ModuleLength
check_version_and_log(node_package_version)
expect(Rails.logger).to have_received(:warn).with(message)
end

it "raises" do
allow(Rails.logger).to receive(:warn)
message = /ReactOnRails: ReactOnRails gem and node package versions do not match/
expect { check_version_and_raise(node_package_version) }.to raise_error(message)
end
end

context "when package json uses a relative path with dots" do
Expand All @@ -125,10 +97,6 @@ module ReactOnRails # rubocop:disable Metrics/ModuleLength

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

it "does not raise" do
expect { check_version_and_raise(node_package_version) }.not_to raise_error
end

it "does not log" do
allow(Rails.logger).to receive(:warn)
check_version_and_log(node_package_version)
Expand All @@ -141,12 +109,8 @@ module ReactOnRails # rubocop:disable Metrics/ModuleLength
double_package_version(raw: nil)
end

it "raise method returns true" do
expect(check_version_and_raise(node_package_version)).to be(true)
end

it "log method returns true" do
expect(check_version_and_log(node_package_version)).to be(true)
it "log method returns nil" do
expect(check_version_and_log(node_package_version)).to be_nil
end
end
end
Expand All @@ -173,6 +137,17 @@ def check_version_and_log(node_package_version)
describe VersionChecker::NodePackageVersion do
subject(:node_package_version) { described_class.new(package_json) }

describe "#build" do
it "initializes NodePackageVersion with ReactOnRails.configuration.node_modules_location" do
allow(ReactOnRails).to receive_message_chain(:configuration, :node_modules_location).and_return("spec/dummy")
root_package_json_path = File.expand_path("../../package.json", __dir__)
allow(Rails).to receive_message_chain(:root, :join).and_return(root_package_json_path)
message = "No 'react-on-rails' entry in the dependencies of #{root_package_json_path}, which is " \
"the expected location according to ReactOnRails.configuration.node_modules_location"
expect { described_class.build.raw }.to raise_error(message)
end
end

describe "#semver_wildcard?" do
context "when package json lists an exact version of '0.0.2'" do
let(:package_json) { File.expand_path("fixtures/normal_package.json", __dir__) }
Expand Down

0 comments on commit 72c5f60

Please sign in to comment.