Skip to content

Commit

Permalink
Allow updating the version of PNPM itself
Browse files Browse the repository at this point in the history
  • Loading branch information
deivid-rodriguez committed May 11, 2023
1 parent f103d05 commit 9ccc08a
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 11 deletions.
10 changes: 8 additions & 2 deletions common/lib/dependabot/dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def self.register_name_normaliser(package_manager, name_builder)

def initialize(name:, requirements:, package_manager:, version: nil,
previous_version: nil, previous_requirements: nil,
subdependency_metadata: [], removed: false, metadata: {})
subdependency_metadata: [], removed: false, on_package_manager: false, metadata: {})
@name = name
@version = version
@requirements = requirements.map { |req| symbolize_keys(req) }
Expand All @@ -54,6 +54,7 @@ def initialize(name:, requirements:, package_manager:, version: nil,
map { |h| symbolize_keys(h) }
end
@removed = removed
@on_package_manager = on_package_manager
@metadata = symbolize_keys(metadata || {})

check_values
Expand All @@ -67,6 +68,10 @@ def removed?
@removed
end

def on_package_manager?
@on_package_manager
end

def numeric_version
@numeric_version ||= version_class.new(version) if version && version_class.correct?(version)
end
Expand All @@ -80,7 +85,8 @@ def to_h
"previous_requirements" => previous_requirements,
"package_manager" => package_manager,
"subdependency_metadata" => subdependency_metadata,
"removed" => removed? ? true : nil
"removed" => removed? ? true : nil,
"on_package_manager" => on_package_manager? ? true : nil
}.compact
end

Expand Down
3 changes: 2 additions & 1 deletion common/lib/dependabot/file_parsers/base/dependency_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ def <<(dep)
version: dep.version,
requirements: dep.requirements,
package_manager: dep.package_manager,
subdependency_metadata: dep.subdependency_metadata
subdependency_metadata: dep.subdependency_metadata,
on_package_manager: dep.on_package_manager?
)
end

Expand Down
3 changes: 2 additions & 1 deletion common/lib/dependabot/update_checkers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ def updated_dependency_with_own_req_unlock
requirements: updated_requirements,
previous_version: previous_version,
previous_requirements: dependency.requirements,
package_manager: dependency.package_manager
package_manager: dependency.package_manager,
on_package_manager: dependency.on_package_manager?
)
end

Expand Down
32 changes: 31 additions & 1 deletion npm_and_yarn/lib/dependabot/npm_and_yarn/file_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require "dependabot/shared_helpers"
require "dependabot/npm_and_yarn/helpers"
require "dependabot/npm_and_yarn/native_helpers"
require "dependabot/npm_and_yarn/package_manager"
require "dependabot/npm_and_yarn/version"
require "dependabot/git_metadata_fetcher"
require "dependabot/git_commit_checker"
Expand Down Expand Up @@ -76,9 +77,30 @@ def manifest_dependencies
end
end

pnpm_version = package_manager.locked_version("pnpm")

if pnpm_version
dependency_set << Dependency.new(
name: "pnpm",
version: pnpm_version,
package_manager: "npm_and_yarn",
requirements: [{
requirement: pnpm_version,
file: "package.json",
groups: ["dependencies"],
source: nil
}],
on_package_manager: true
)
end

dependency_set
end

def package_manager
@package_manager ||= PackageManager.new(parsed_root_package_file)
end

def lockfile_parser
@lockfile_parser ||= LockfileParser.new(
dependency_files: dependency_files
Expand Down Expand Up @@ -314,6 +336,10 @@ def url_for_relevant_cred(resolved_url)
resolved_url.gsub(/#{Regexp.quote(reg)}.*/, "") + reg
end

def parsed_root_package_file
JSON.parse(root_package_file.content)
end

def package_files
@package_files ||=
begin
Expand All @@ -325,12 +351,16 @@ def package_files
reject(&:support_file?)

[
dependency_files.find { |f| f.name == "package.json" },
root_package_file,
*sub_packages
].compact
end
end

def root_package_file
@root_package_file ||= dependency_files.find { |f| f.name == "package.json" }
end

def version_class
NpmAndYarn::Version
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def updated_package_json_content

new_content = update_package_json_declaration(
package_json_content: content,
dependency_name: dep.name,
dependency: dep,
old_req: old_req,
new_req: new_req
)
Expand Down Expand Up @@ -79,7 +79,23 @@ def updated_requirements(dependency)
end

def update_package_json_declaration(package_json_content:, new_req:,
dependency_name:, old_req:)
dependency:, old_req:)
args = {
package_json_content: package_json_content,
new_req: new_req,
dependency_name: dependency.name,
old_req: old_req
}

if dependency.on_package_manager?
update_package_manager_declaration(**args)
else
update_dependency_declaration(**args)
end
end

def update_dependency_declaration(package_json_content:, new_req:,
dependency_name:, old_req:)
original_line = declaration_line(
dependency_name: dependency_name,
dependency_req: old_req,
Expand Down Expand Up @@ -139,6 +155,16 @@ def update_package_json_resolutions(package_json_content:, new_req:,
content
end

def update_package_manager_declaration(package_json_content:, new_req:,
dependency_name:, old_req:)
parsed_json_content = JSON.parse(package_json_content)
package_manager = parsed_json_content["packageManager"]

return package_json_content unless package_manager == "#{dependency_name}@#{old_req[:requirement]}"

package_json_content.sub("\"#{package_manager}\"", "\"#{dependency_name}@#{new_req[:requirement]}\"")
end

def declaration_line(dependency_name:, dependency_req:, content:)
git_dependency = dependency_req.dig(:source, :type) == "git"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ def updated_pnpm_lock_content(pnpm_lock)
def run_pnpm_update(pnpm_lock:)
SharedHelpers.in_a_temporary_repo_directory(base_dir, repo_contents_path) do
SharedHelpers.with_git_configured(credentials: credentials) do
run_pnpm_updater
run_pnpm_updater unless dependencies.first.on_package_manager?

write_final_package_json_files

run_pnpm_install
sync_lockfile

File.read(pnpm_lock.name)
end
Expand All @@ -60,7 +60,7 @@ def run_pnpm_updater
)
end

def run_pnpm_install
def sync_lockfile
SharedHelpers.run_shell_command(
"pnpm install --lockfile-only"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def initialize(dependency:, credentials:, dependency_files:,
end

def latest_resolvable_version
return latest_allowable_version if git_dependency?(dependency)
return latest_allowable_version if git_dependency?(dependency) || dependency.on_package_manager?
return if part_of_tightly_locked_monorepo?
return if types_update_available?
return if original_package_update_available?
Expand Down

0 comments on commit 9ccc08a

Please sign in to comment.