diff --git a/Cargo.lock b/Cargo.lock index 1b175b15..d91d01ae 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1853,6 +1853,7 @@ dependencies = [ "regex", "rust-ini", "self_update", + "semver", "serde", "shellexpand", "strum 0.24.0", diff --git a/Cargo.toml b/Cargo.toml index 13a34f46..fa4cdcfd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,6 +34,7 @@ tokio = { version = "1.5.0", features = ["process", "rt-multi-thread"] } futures = "0.3.14" regex = "1.5.3" sys-info = "0.9" +semver = "1.0" [target.'cfg(target_os = "macos")'.dependencies] notify-rust = "4.5.0" diff --git a/src/steps/node.rs b/src/steps/node.rs index 60ffd2bd..2541e7cd 100644 --- a/src/steps/node.rs +++ b/src/steps/node.rs @@ -10,6 +10,7 @@ use directories::BaseDirs; use log::debug; #[cfg(unix)] use nix::unistd::Uid; +use semver::Version; use crate::executor::{CommandExt, RunType}; use crate::terminal::print_separator; @@ -32,23 +33,42 @@ impl NPM { #[cfg(target_os = "linux")] fn root(&self) -> Result { + let version = self.version()?; + let args = if version < Version::new(8, 11, 0) { + ["root", "-g"] + } else { + ["root", "--location=global"] + }; Command::new(&self.command) - .args(&["root", "-g"]) + .args(args) .check_output() .map(|s| PathBuf::from(s.trim())) } + fn version(&self) -> Result { + let version_str = Command::new(&self.command) + .args(&["--version"]) + .check_output() + .map(|s| s.trim().to_owned()); + Version::parse(&version_str?).map_err(|err| err.into()) + } + fn upgrade(&self, run_type: RunType, use_sudo: bool) -> Result<()> { print_separator("Node Package Manager"); - + let version = self.version()?; + let args = if version < Version::new(8, 11, 0) || self.pnpm.is_some() { + ["update", "-g"] + } else { + ["update", "--location=global"] + }; if use_sudo { run_type .execute("sudo") .arg(self.pnpm.as_ref().unwrap_or(&self.command)) - .args(&["update", "-g"]) + .args(args) .check_run()?; } else { - run_type.execute(&self.command).args(&["update", "-g"]).check_run()?; + run_type.execute(&self.command).args(args).check_run()?; } Ok(())