diff --git a/src/main.rs b/src/main.rs index ec9a7c66..9ddb1d0b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -308,7 +308,7 @@ fn run() -> Result<()> { runner.execute(Step::Vim, "Neovim", || vim::upgrade_neovim(&base_dirs, &ctx))?; runner.execute(Step::Vim, "voom", || vim::run_voom(&base_dirs, run_type))?; runner.execute(Step::Node, "npm", || node::run_npm_upgrade(&ctx))?; - runner.execute(Step::Pnpm, "pnpm", || node::pnpm_global_update(run_type))?; + runner.execute(Step::Pnpm, "pnpm", || node::pnpm_global_update(&ctx))?; runner.execute(Step::Deno, "deno", || node::deno_upgrade(&ctx))?; runner.execute(Step::Composer, "composer", || generic::run_composer_update(&ctx))?; runner.execute(Step::Krew, "krew", || generic::run_krew_upgrade(run_type))?; diff --git a/src/steps/node.rs b/src/steps/node.rs index b6bb6e02..94ed623e 100644 --- a/src/steps/node.rs +++ b/src/steps/node.rs @@ -45,16 +45,10 @@ impl NPM { Ok(()) } -} - -pub fn run_npm_upgrade(ctx: &ExecutionContext) -> Result<()> { - let npm = require("npm").map(NPM::new)?; - #[allow(unused_mut)] - let mut use_sudo = false; #[cfg(target_os = "linux")] - { - let npm_root = npm.root()?; + pub fn should_use_sudo(&self) -> Result { + let npm_root = self.root()?; if !npm_root.exists() { return Err(SkipStep(format!("NPM root at {} doesn't exist", npm_root.display(),)).into()); } @@ -62,29 +56,56 @@ pub fn run_npm_upgrade(ctx: &ExecutionContext) -> Result<()> { let metadata = std::fs::metadata(&npm_root)?; let uid = Uid::effective(); - if metadata.uid() != uid.as_raw() { - if metadata.uid() == 0 && (ctx.config().npm_use_sudo()) { - use_sudo = true; - } else { - return Err(SkipStep(format!( - "NPM root at {} is owned by {} which is not the current user. Set use_sudo = true under the NPM section in your configuration to run NPM as sudo", - npm_root.display(), - metadata.uid() - )) - .into()); - } - } + Ok(metadata.uid() != uid.as_raw() && metadata.uid() == 0) } - - print_separator("Node Package Manager"); - npm.upgrade(ctx.run_type(), use_sudo) } -pub fn pnpm_global_update(run_type: RunType) -> Result<()> { +#[cfg(target_os = "linux")] +fn should_use_sudo(npm: &NPM, ctx: &ExecutionContext) -> Result { + if npm.should_use_sudo()? { + if ctx.config().npm_use_sudo() { + Ok(true) + } else { + Err(SkipStep("NPM root is owned by another user which is not the current user. Set use_sudo = true under the NPM section in your configuration to run NPM as sudo".to_string()) + .into()) + } + } else { + Ok(false) + } +} + +pub fn run_npm_upgrade(ctx: &ExecutionContext) -> Result<()> { + let npm = require("npm").map(NPM::new)?; + + print_separator("Node Package Manager"); + #[cfg(target_os = "linux")] + { + npm.upgrade(ctx.run_type(), should_use_sudo(&npm, ctx)?) + } + + #[cfg(not(target_os = "linux"))] + { + npm.upgrade(ctx.run_type(), false) + } +} + +pub fn pnpm_global_update(ctx: &ExecutionContext) -> Result<()> { let pnpm = require("pnpm")?; print_separator("Performant Node Package Manager"); - run_type.execute(&pnpm).args(["update", "-g"]).check_run() + #[cfg(target_os = "linux")] + if should_use_sudo(&require("npm").map(NPM::new)?, ctx)? { + ctx.run_type() + .execute("sudo") + .arg(pnpm) + .args(["update", "-g"]) + .check_run() + } else { + ctx.run_type().execute(&pnpm).args(["update", "-g"]).check_run() + } + + #[cfg(not(target_os = "linux"))] + ctx.run_type().execute(&pnpm).args(["update", "-g"]).check_run() } pub fn deno_upgrade(ctx: &ExecutionContext) -> Result<()> {