Class: ReactOnRails::VersionChecker::NodePackageVersion

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

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package_json, yarn_lock = nil, package_lock = nil) ⇒ NodePackageVersion

Returns a new instance of NodePackageVersion.



241
242
243
244
245
# File 'lib/react_on_rails/version_checker.rb', line 241

def initialize(package_json, yarn_lock = nil, package_lock = nil)
  @package_json = package_json
  @yarn_lock = yarn_lock
  @package_lock = package_lock
end

Instance Attribute Details

#package_jsonObject (readonly)

Returns the value of attribute package_json.



217
218
219
# File 'lib/react_on_rails/version_checker.rb', line 217

def package_json
  @package_json
end

#package_lockObject (readonly)

Returns the value of attribute package_lock.



217
218
219
# File 'lib/react_on_rails/version_checker.rb', line 217

def package_lock
  @package_lock
end

#yarn_lockObject (readonly)

Returns the value of attribute yarn_lock.



217
218
219
# File 'lib/react_on_rails/version_checker.rb', line 217

def yarn_lock
  @yarn_lock
end

Class Method Details

.buildObject



219
220
221
# File 'lib/react_on_rails/version_checker.rb', line 219

def self.build
  new(package_json_path, yarn_lock_path, package_lock_path)
end

.package_json_pathObject



223
224
225
# File 'lib/react_on_rails/version_checker.rb', line 223

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

.package_lock_pathObject



234
235
236
237
238
239
# File 'lib/react_on_rails/version_checker.rb', line 234

def self.package_lock_path
  # Lockfiles are in the same directory as package.json
  # If node_modules_location is empty, use Rails.root
  base_dir = ReactOnRails.configuration.node_modules_location.presence || ""
  Rails.root.join(base_dir, "package-lock.json").to_s
end

.yarn_lock_pathObject



227
228
229
230
231
232
# File 'lib/react_on_rails/version_checker.rb', line 227

def self.yarn_lock_path
  # Lockfiles are in the same directory as package.json
  # If node_modules_location is empty, use Rails.root
  base_dir = ReactOnRails.configuration.node_modules_location.presence || ""
  Rails.root.join(base_dir, "yarn.lock").to_s
end

Instance Method Details

#local_path_or_url?Boolean

Returns:

  • (Boolean)


320
321
322
323
324
325
# File 'lib/react_on_rails/version_checker.rb', line 320

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

#package_nameObject



285
286
287
288
289
# File 'lib/react_on_rails/version_checker.rb', line 285

def package_name
  return "react-on-rails-pro" if react_on_rails_pro_package?

  "react-on-rails"
end

#partsObject



327
328
329
330
331
332
333
334
335
336
# File 'lib/react_on_rails/version_checker.rb', line 327

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

#range_operator?Boolean

Returns:

  • (Boolean)


312
313
314
# File 'lib/react_on_rails/version_checker.rb', line 312

def range_operator?
  raw.start_with?(/[~^><*]/)
end

#range_syntax?Boolean

Returns:

  • (Boolean)


316
317
318
# File 'lib/react_on_rails/version_checker.rb', line 316

def range_syntax?
  raw.include?(" - ") || raw.include?(" || ")
end

#rawObject



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/react_on_rails/version_checker.rb', line 247

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

  return @raw = nil unless File.exist?(package_json)

  parsed = parsed_package_contents
  return @raw = nil unless parsed.key?("dependencies")

  deps = parsed["dependencies"]

  # Check for react-on-rails-pro first (Pro takes precedence)
  if deps.key?("react-on-rails-pro")
    @raw = resolve_version(deps["react-on-rails-pro"], "react-on-rails-pro")
    return @raw
  end

  # Fall back to react-on-rails
  if deps.key?("react-on-rails")
    @raw = resolve_version(deps["react-on-rails"], "react-on-rails")
    return @raw
  end

  # Neither package found
  msg = "No 'react-on-rails' or 'react-on-rails-pro' 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

#react_on_rails_package?Boolean

Returns:

  • (Boolean)


277
278
279
# File 'lib/react_on_rails/version_checker.rb', line 277

def react_on_rails_package?
  package_installed?("react-on-rails")
end

#react_on_rails_pro_package?Boolean

Returns:

  • (Boolean)


281
282
283
# File 'lib/react_on_rails/version_checker.rb', line 281

def react_on_rails_pro_package?
  package_installed?("react-on-rails-pro")
end

#semver_wildcard?Boolean

Returns:

  • (Boolean)


291
292
293
294
295
296
297
298
# File 'lib/react_on_rails/version_checker.rb', line 291

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?
  return true if raw.blank?

  special_version_string? || wildcard_or_x_range? || range_operator? || range_syntax?
end

#special_version_string?Boolean

Returns:

  • (Boolean)


300
301
302
# File 'lib/react_on_rails/version_checker.rb', line 300

def special_version_string?
  %w[latest next canary beta alpha rc].include?(raw.downcase)
end

#wildcard_or_x_range?Boolean

Returns:

  • (Boolean)


304
305
306
307
308
309
310
# File 'lib/react_on_rails/version_checker.rb', line 304

def wildcard_or_x_range?
  raw == "*" ||
    raw =~ /^[xX*]$/ ||
    raw =~ /^[xX*]\./ ||
    raw =~ /\.[xX*]\b/ ||
    raw =~ /\.[xX*]$/
end