Class: ReactOnRails::VersionChecker::NodePackageVersion

Inherits:
Object
  • Object
show all
Defined in:
lib/react_on_rails/version_checker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package_json) ⇒ NodePackageVersion

Returns a new instance of NodePackageVersion.



73
74
75
# File 'lib/react_on_rails/version_checker.rb', line 73

def initialize(package_json)
  @package_json = package_json
end

Instance Attribute Details

#package_jsonObject (readonly)

Returns the value of attribute package_json.



63
64
65
# File 'lib/react_on_rails/version_checker.rb', line 63

def package_json
  @package_json
end

Class Method Details

.buildObject



65
66
67
# File 'lib/react_on_rails/version_checker.rb', line 65

def self.build
  new(package_json_path)
end

.package_json_pathObject



69
70
71
# File 'lib/react_on_rails/version_checker.rb', line 69

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

Instance Method Details

#local_path_or_url?Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
# File 'lib/react_on_rails/version_checker.rb', line 100

def local_path_or_url?
  # See https://docs.npmjs.com/cli/v10/configuring-npm/package-json#dependencies
  # All path and protocol "version ranges" include / somewhere,
  # but we want to make an exception for npm:@scope/pkg@version.
  !raw.nil? && raw.include?("/") && !raw.start_with?("npm:")
end

#partsObject



107
108
109
110
111
112
113
114
115
116
# File 'lib/react_on_rails/version_checker.rb', line 107

def parts
  return if local_path_or_url?

  match = raw.match(VERSION_PARTS_REGEX)
  unless match
    raise ReactOnRails::Error, "Cannot parse version number '#{raw}' (only exact versions are supported)"
  end

  match.captures.compact
end

#rawObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/react_on_rails/version_checker.rb', line 77

def raw
  return @raw if defined?(@raw)

  if File.exist?(package_json)
    parsed_package_contents = JSON.parse(package_json_contents)
    if parsed_package_contents.key?("dependencies") &&
       parsed_package_contents["dependencies"].key?("react-on-rails")
      return @raw = parsed_package_contents["dependencies"]["react-on-rails"]
    end
  end
  msg = "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"
  Rails.logger.warn(msg)
  @raw = nil
end

#semver_wildcard?Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
# File 'lib/react_on_rails/version_checker.rb', line 93

def semver_wildcard?
  # See https://docs.npmjs.com/cli/v10/configuring-npm/package-json#dependencies
  # We want to disallow all expressions other than exact versions
  # and the ones allowed by local_path_or_url?
  raw.blank? || raw.start_with?(/[~^><*]/) || raw.include?(" - ") || raw.include?(" || ")
end