From 882a4ecdb33edae3c9722d5ab23fe167df7ec0fe Mon Sep 17 00:00:00 2001 From: Kamil Bukum Date: Fri, 15 Nov 2024 16:33:39 -0800 Subject: [PATCH] improve the code to use existing constants. --- .../lib/dependabot/npm_and_yarn/helpers.rb | 53 ++++++++++++++----- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/npm_and_yarn/lib/dependabot/npm_and_yarn/helpers.rb b/npm_and_yarn/lib/dependabot/npm_and_yarn/helpers.rb index 50f933ad34e..f7fefb1fde9 100644 --- a/npm_and_yarn/lib/dependabot/npm_and_yarn/helpers.rb +++ b/npm_and_yarn/lib/dependabot/npm_and_yarn/helpers.rb @@ -271,39 +271,52 @@ def self.run_yarn_command(command, fingerprint: nil) # add shims for NPM. sig { params(command: String, fingerprint: T.nilable(String)).returns(String) } def self.run_npm_command(command, fingerprint: command) - SharedHelpers.run_shell_command( - "corepack npm #{command}", - fingerprint: "corepack npm #{fingerprint}" - ) + package_manager_run_command(NpmPackageManager::NAME, command, fingerprint: fingerprint) end # Run single pnpm command returning stdout/stderr sig { params(command: String, fingerprint: T.nilable(String)).returns(String) } def self.run_pnpm_command(command, fingerprint: nil) - SharedHelpers.run_shell_command( - "corepack pnpm #{command}", - fingerprint: "corepack pnpm #{fingerprint || command}" - ) + package_manager_run_command(PNPMPackageManager::NAME, command, fingerprint: fingerprint) end # Run single yarn command returning stdout/stderr sig { params(command: String, fingerprint: T.nilable(String)).returns(String) } def self.run_single_yarn_command(command, fingerprint: nil) + package_manager_run_command(YarnPackageManager::NAME, command, fingerprint: fingerprint) + end + + # Install the package manager for specified version by using corepack + # and prepare it for use by using corepack + sig { params(name: String, version: String).void } + def self.install(name, version) + package_manager_install(name, version) + package_manager_activate(name, version) + installed_version = package_manager_version(name) + + Dependabot.logger.info("Installed version of #{name}: #{installed_version}") + end + + sig { params(name: String, version: String).void } + def self.run_package_manager_command(name, version) SharedHelpers.run_shell_command( - "corepack yarn #{command}", - fingerprint: "corepack yarn #{fingerprint || command}" + "corepack prepare #{name}@#{version} --activate", + fingerprint: "corepack prepare --activate" ) end # Install the package manager for specified version by using corepack - # and prepare it for use by using corepack sig { params(name: String, version: String).void } - def self.install_package_manager(name, version) + def self.package_manager_install(name, version) SharedHelpers.run_shell_command( "corepack install #{name}@#{version} --global --cache-only", fingerprint: "corepack install @ --global --cache-only" ) + end + # Prepare the package manager for use by using corepack + sig { params(name: String, version: String).void } + def self.package_manager_activate(name, version) SharedHelpers.run_shell_command( "corepack prepare #{name}@#{version} --activate", fingerprint: "corepack prepare --activate" @@ -313,9 +326,21 @@ def self.install_package_manager(name, version) # Get the version of the package manager by using corepack sig { params(name: String).returns(String) } def self.package_manager_version(name) + package_manager_run_command(name, "-v") + end + + # Run single yarn command returning stdout/stderr + sig do + params( + name: String, + command: String, + fingerprint: T.nilable(String) + ).returns(String) + end + def self.package_manager_run_command(name, command, fingerprint: nil) SharedHelpers.run_shell_command( - "corepack #{name} -v", - fingerprint: "corepack -v" + "corepack #{name} #{command}", + fingerprint: "corepack #{name} #{fingerprint || command}" ) end