Class: Webpacker::VersionChecker

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

Defined Under Namespace

Classes: NodePackageVersion

Constant Summary collapse

MAJOR_MINOR_PATCH_VERSION_REGEX =
/(\d+)\.(\d+)\.(\d+)/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node_package_version) ⇒ VersionChecker

Returns a new instance of VersionChecker.



14
15
16
# File 'lib/webpacker/version_checker.rb', line 14

def initialize(node_package_version)
  @node_package_version = node_package_version
end

Instance Attribute Details

#node_package_versionObject (readonly)

Returns the value of attribute node_package_version.



6
7
8
# File 'lib/webpacker/version_checker.rb', line 6

def node_package_version
  @node_package_version
end

Class Method Details

.buildObject



10
11
12
# File 'lib/webpacker/version_checker.rb', line 10

def self.build
  new(NodePackageVersion.build)
end

Instance Method Details

#raise_if_gem_and_node_package_versions_differObject



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
# 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 without a lockfile 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 ^) without a lockfile 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