class Webpacker::VersionChecker

Constants

MAJOR_MINOR_PATCH_VERSION_REGEX

Attributes

node_package_version[R]

Public Class Methods

build() click to toggle source
# File lib/webpacker/version_checker.rb, line 10
def self.build
  new(NodePackageVersion.build)
end
new(node_package_version) click to toggle source
# File lib/webpacker/version_checker.rb, line 14
def initialize(node_package_version)
  @node_package_version = node_package_version
end

Public Instance Methods

raise_if_gem_and_node_package_versions_differ() click to toggle source
# File lib/webpacker/version_checker.rb, line 18
    def raise_if_gem_and_node_package_versions_differ
      # Skip check if package is not in package.json or listed from relative path, git repo or github URL
      return if node_package_version.skip_processing?

      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]

      uses_wildcard = node_package_version.semver_wildcard?

      if !Webpacker.config.ensure_consistent_versioning? && (uses_wildcard || !versions_match)
        check_failed = if uses_wildcard
          "Semver wildcard detected"
        else
          "Version mismatch detected"
        end

        warn <<-MSG.strip_heredoc
          Webpacker::VersionChecker - #{check_failed}

          You are currently not checking for consistent versions of shakapacker gem and npm package. A version mismatch or usage of semantic versioning wildcard (~ or ^) has been detected.

          Version mismatch can lead to incorrect behavior and bugs. You should ensure that both the gem and npm package dependencies are locked to the same version.

          You can enable the version check by setting `ensure_consistent_versioning: true` in your `webpacker.yml` file.

          Checking for gem and npm package versions mismatch or wildcard will be enabled by default in the next major version of shakapacker.
        MSG

        return
      end

      raise_differing_versions_warning unless versions_match

      raise_node_semver_version_warning if uses_wildcard
    end

Private Instance Methods

common_error_msg() click to toggle source
# File lib/webpacker/version_checker.rb, line 59
      def common_error_msg
        <<-MSG.strip_heredoc
         Detected: #{node_package_version.raw}
              gem: #{gem_version}
         Ensure the installed version of the gem is the same as the version of
         your installed node package. Do not use >= or ~> in your Gemfile for shakapacker.
         Do not use ^ or ~ in your package.json for shakapacker.
         Run `yarn add shakapacker --exact` in the directory containing folder node_modules.
      MSG
      end
gem_major_minor_patch_version() click to toggle source
# File lib/webpacker/version_checker.rb, line 85
def gem_major_minor_patch_version
  match = gem_version.match(MAJOR_MINOR_PATCH_VERSION_REGEX)
  [match[1], match[2], match[3]]
end
gem_version() click to toggle source
# File lib/webpacker/version_checker.rb, line 81
def gem_version
  Webpacker::VERSION
end
raise_differing_versions_warning() click to toggle source
# File lib/webpacker/version_checker.rb, line 70
def raise_differing_versions_warning
  msg = "**ERROR** Webpacker: Webpacker gem and node package versions do not match\n#{common_error_msg}"
  raise msg
end
raise_node_semver_version_warning() click to toggle source
# File lib/webpacker/version_checker.rb, line 75
def raise_node_semver_version_warning
  msg = "**ERROR** Webpacker: Your node package version for shakapacker contains a "\
        "^ or ~\n#{common_error_msg}"
  raise msg
end