Skip to content

Commit

Permalink
improve the code to use existing constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
kbukum1 committed Nov 16, 2024
1 parent 9b1a441 commit 882a4ec
Showing 1 changed file with 39 additions and 14 deletions.
53 changes: 39 additions & 14 deletions npm_and_yarn/lib/dependabot/npm_and_yarn/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>@<version> --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"
Expand All @@ -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 <name> -v"
"corepack #{name} #{command}",
fingerprint: "corepack #{name} #{fingerprint || command}"
)
end

Expand Down

0 comments on commit 882a4ec

Please sign in to comment.