Class: Dependabot::NpmAndYarn::PackageManager

Inherits:
Object
  • Object
show all
Extended by:
T::Helpers, T::Sig
Defined in:
lib/dependabot/npm_and_yarn/package_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(package_json, lockfiles:) ⇒ PackageManager

Returns a new instance of PackageManager.



12
13
14
15
16
17
# File 'lib/dependabot/npm_and_yarn/package_manager.rb', line 12

def initialize(package_json, lockfiles:)
  @package_json = package_json
  @lockfiles = lockfiles
  @package_manager = package_json.fetch("packageManager", nil)
  @engines = package_json.fetch("engines", nil)
end

Instance Method Details

#setup(name) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dependabot/npm_and_yarn/package_manager.rb', line 21

def setup(name)
  # we prioritize version mentioned in "packageManager" instead of "engines"
  # i.e. if { engines : "pnpm" : "6" } and { packageManager: "pnpm@6.0.2" },
  # we go for the specificity mentioned in packageManager (6.0.2)

  if Dependabot::Experiments.enabled?(:enable_pnpm_yarn_dynamic_engine)

    unless @package_manager&.start_with?("#{name}@") || (@package_manager&.==name.to_s) || @package_manager.nil?
      return
    end

    if @engines && @package_manager.nil?
      # if "packageManager" doesn't exists in manifest file,
      # we check if we can extract "engines" information
      Dependabot.logger.info("No \"packageManager\" info found for \"#{name}\"")
      version = check_engine_version(name)

    elsif @package_manager&.==name.to_s
      # if "packageManager" is found but no version is specified (i.e. pnpm@1.2.3),
      # we check if we can get "engines" info to override default version
      Dependabot.logger.info("Found \"packageManager\" : \"#{@package_manager}\"")
      version = check_engine_version(name) if @engines

    elsif @package_manager&.start_with?("#{name}@")
      # if "packageManager" info has version specification i.e. yarn@3.3.1
      # we go with the version in "packageManager"
      Dependabot.logger.info("Found \"packageManager\" : \"#{@package_manager}\". Skipped checking \"engines\".")
    end
  else
    return unless @package_manager.nil? || @package_manager&.start_with?("#{name}@")
  end

  version ||= requested_version(name)

  if version
    raise_if_unsupported!(name, version)

    install(name, version)
  else
    version = guessed_version(name)

    if version
      raise_if_unsupported!(name, version.to_s)

      install(name, version) if name == "pnpm"
    end
  end

  version
end