Class: Dependabot::NpmAndYarn::FileParser::PnpmLock

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/npm_and_yarn/file_parser/pnpm_lock.rb

Instance Method Summary collapse

Constructor Details

#initialize(dependency_file) ⇒ PnpmLock



19
20
21
# File 'lib/dependabot/npm_and_yarn/file_parser/pnpm_lock.rb', line 19

def initialize(dependency_file)
  @dependency_file = dependency_file
end

Instance Method Details

#dependenciesObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dependabot/npm_and_yarn/file_parser/pnpm_lock.rb', line 47

def dependencies
  dependency_set = Dependabot::FileParsers::Base::DependencySet.new

  # Separate dependencies into two categories: with specifiers and without specifiers.
  dependencies_with_specifiers = T.let([], T::Array[T::Hash[Symbol, T.untyped]])
  dependencies_without_specifiers = T.let([], T::Array[T::Hash[Symbol, T.untyped]])

  parsed.each do |details|
    next if details["aliased"]

    name = T.cast(details["name"], String)
    version = T.cast(details["version"], T.nilable(String))

    dependency_args = {
      name: name,
      version: version,
      package_manager: "npm_and_yarn",
      requirements: []
    }

    # Add metadata for subdependencies if marked as a dev dependency.
    dependency_args[:subdependency_metadata] = [{ production: !details["dev"] }] if details["dev"]

    specifiers = details["specifiers"]
    if specifiers&.any?
      dependencies_with_specifiers << dependency_args
    else
      dependencies_without_specifiers << dependency_args
    end
  end

  # Add prioritized dependencies to the dependency set.
  dependencies_with_specifiers.each do |dependency_args|
    dependency_set << Dependency.new(
      name: dependency_args[:name],
      version: dependency_args[:version],
      package_manager: dependency_args[:package_manager],
      requirements: dependency_args[:requirements],
      subdependency_metadata: dependency_args[:subdependency_metadata]
    )
  end

  dependencies_without_specifiers.each do |dependency_args|
    dependency_set << Dependency.new(
      name: dependency_args[:name],
      version: dependency_args[:version],
      package_manager: dependency_args[:package_manager],
      requirements: dependency_args[:requirements],
      subdependency_metadata: dependency_args[:subdependency_metadata]
    )
  end

  dependency_set
end

#details(dependency_name, requirement, _manifest_name) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dependabot/npm_and_yarn/file_parser/pnpm_lock.rb', line 112

def details(dependency_name, requirement, _manifest_name)
  details_candidates = parsed.select { |info| info["name"] == dependency_name }

  # If there's only one entry for this dependency, use it, even if
  # the requirement in the lockfile doesn't match
  if details_candidates.one?
    details_candidates.first
  else
    details_candidates.find { |info| info["specifiers"]&.include?(requirement) }
  end
end

#parsedObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dependabot/npm_and_yarn/file_parser/pnpm_lock.rb', line 24

def parsed
  @parsed ||= T.let(
    T.cast(
      SharedHelpers.in_a_temporary_directory do
        File.write("pnpm-lock.yaml", @dependency_file.content)

        SharedHelpers.run_helper_subprocess(
          command: NativeHelpers.helper_path,
          function: "pnpm:parseLockfile",
          args: [Dir.pwd]
        )
      rescue SharedHelpers::HelperSubprocessFailed
        raise Dependabot::DependencyFileNotParseable, @dependency_file.path
      end,
      T::Array[T::Hash[String, T.untyped]]
    ),
    T.nilable(T::Array[T::Hash[String, T.untyped]])
  )
end