-
-
Notifications
You must be signed in to change notification settings - Fork 627
/
version_checker.rb
84 lines (66 loc) · 2.09 KB
/
version_checker.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require_relative "version"
module ReactOnRails
class VersionChecker
attr_reader :node_package_version, :logger
def self.build
new(NodePackageVersion.build, Rails.logger)
end
def initialize(node_package_version, logger)
@logger = logger
@node_package_version = node_package_version
end
# 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 warn_if_gem_and_node_package_versions_differ
return if node_package_version.major == gem_major_version || node_package_version.relative_path?
log_differing_versions_warning
end
private
def log_differing_versions_warning
msg = "**WARNING** ReactOnRails: ReactOnRails gem and node package major versions do not match\n" \
" gem: #{gem_version}\n" \
" node package: #{node_package_version.raw}\n" \
"Ensure the installed version of the gem is the same as the version of your installed node package"
logger.warn(msg)
end
def gem_version
ReactOnRails::VERSION
end
def gem_major_version
gem_version.match(/(\d+)\..*/)[1]
end
end
class NodePackageVersion
attr_reader :package_json
def self.build
new(package_json_path)
end
def self.package_json_path
Rails.root.join("client", "package.json")
end
def initialize(package_json)
@package_json = package_json
end
def raw
JSON.parse(package_json_contents)["dependencies"]["react-on-rails"]
end
def normalized
match = raw
.tr("-", ".")
.strip
.match(/(\d.*)/)
match.present? ? match[0] : nil
end
def relative_path?
normalized.nil? # must be a relative path if nil and we haven't failed
end
def major
return if normalized.nil?
normalized.match(/(\d+)\..*/)[1]
end
private
def package_json_contents
@package_json_contents ||= File.read(package_json)
end
end
end