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.



473
474
475
476
477
# File 'lib/react_on_rails/version_checker.rb', line 473

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.



449
450
451
# File 'lib/react_on_rails/version_checker.rb', line 449

def package_json
  @package_json
end

#package_lockObject (readonly)

Returns the value of attribute package_lock.



449
450
451
# File 'lib/react_on_rails/version_checker.rb', line 449

def package_lock
  @package_lock
end

#yarn_lockObject (readonly)

Returns the value of attribute yarn_lock.



449
450
451
# File 'lib/react_on_rails/version_checker.rb', line 449

def yarn_lock
  @yarn_lock
end

Class Method Details

.buildObject



451
452
453
# File 'lib/react_on_rails/version_checker.rb', line 451

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

.package_json_pathObject



455
456
457
# File 'lib/react_on_rails/version_checker.rb', line 455

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

.package_lock_pathObject



466
467
468
469
470
471
# File 'lib/react_on_rails/version_checker.rb', line 466

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



459
460
461
462
463
464
# File 'lib/react_on_rails/version_checker.rb', line 459

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)


552
553
554
555
556
557
# File 'lib/react_on_rails/version_checker.rb', line 552

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



517
518
519
520
521
# File 'lib/react_on_rails/version_checker.rb', line 517

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

  "react-on-rails"
end

#partsObject



565
566
567
568
569
570
571
572
573
574
575
576
# File 'lib/react_on_rails/version_checker.rb', line 565

def parts
  return if local_path_or_url? || workspace_protocol?

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

  match.captures.compact
end

#range_operator?Boolean

Returns:

  • (Boolean)


544
545
546
# File 'lib/react_on_rails/version_checker.rb', line 544

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

#range_syntax?Boolean

Returns:

  • (Boolean)


548
549
550
# File 'lib/react_on_rails/version_checker.rb', line 548

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

#rawObject



479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# File 'lib/react_on_rails/version_checker.rb', line 479

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)


509
510
511
# File 'lib/react_on_rails/version_checker.rb', line 509

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

#react_on_rails_pro_package?Boolean

Returns:

  • (Boolean)


513
514
515
# File 'lib/react_on_rails/version_checker.rb', line 513

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

#semver_wildcard?Boolean

Returns:

  • (Boolean)


523
524
525
526
527
528
529
530
# File 'lib/react_on_rails/version_checker.rb', line 523

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)


532
533
534
# File 'lib/react_on_rails/version_checker.rb', line 532

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

#wildcard_or_x_range?Boolean

Returns:

  • (Boolean)


536
537
538
539
540
541
542
# File 'lib/react_on_rails/version_checker.rb', line 536

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

#workspace_protocol?Boolean

Returns:

  • (Boolean)


559
560
561
562
563
# File 'lib/react_on_rails/version_checker.rb', line 559

def workspace_protocol?
  # pnpm workspace protocol: workspace:* or workspace:^
  # Used for monorepo internal dependencies
  !raw.nil? && raw.start_with?("workspace:")
end