npm/cli 8.11.0 deprecated -g flags (#949)

This commit is contained in:
Mark Nefedov
2022-06-02 11:28:20 +03:00
committed by GitHub
parent 717674e33e
commit 4a7de60e59
3 changed files with 26 additions and 4 deletions

1
Cargo.lock generated
View File

@@ -1853,6 +1853,7 @@ dependencies = [
"regex", "regex",
"rust-ini", "rust-ini",
"self_update", "self_update",
"semver",
"serde", "serde",
"shellexpand", "shellexpand",
"strum 0.24.0", "strum 0.24.0",

View File

@@ -34,6 +34,7 @@ tokio = { version = "1.5.0", features = ["process", "rt-multi-thread"] }
futures = "0.3.14" futures = "0.3.14"
regex = "1.5.3" regex = "1.5.3"
sys-info = "0.9" sys-info = "0.9"
semver = "1.0"
[target.'cfg(target_os = "macos")'.dependencies] [target.'cfg(target_os = "macos")'.dependencies]
notify-rust = "4.5.0" notify-rust = "4.5.0"

View File

@@ -10,6 +10,7 @@ use directories::BaseDirs;
use log::debug; use log::debug;
#[cfg(unix)] #[cfg(unix)]
use nix::unistd::Uid; use nix::unistd::Uid;
use semver::Version;
use crate::executor::{CommandExt, RunType}; use crate::executor::{CommandExt, RunType};
use crate::terminal::print_separator; use crate::terminal::print_separator;
@@ -32,23 +33,42 @@ impl NPM {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
fn root(&self) -> Result<PathBuf> { fn root(&self) -> Result<PathBuf> {
let version = self.version()?;
let args = if version < Version::new(8, 11, 0) {
["root", "-g"]
} else {
["root", "--location=global"]
};
Command::new(&self.command) Command::new(&self.command)
.args(&["root", "-g"]) .args(args)
.check_output() .check_output()
.map(|s| PathBuf::from(s.trim())) .map(|s| PathBuf::from(s.trim()))
} }
fn version(&self) -> Result<Version> {
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<()> { fn upgrade(&self, run_type: RunType, use_sudo: bool) -> Result<()> {
print_separator("Node Package Manager"); 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 { if use_sudo {
run_type run_type
.execute("sudo") .execute("sudo")
.arg(self.pnpm.as_ref().unwrap_or(&self.command)) .arg(self.pnpm.as_ref().unwrap_or(&self.command))
.args(&["update", "-g"]) .args(args)
.check_run()?; .check_run()?;
} else { } else {
run_type.execute(&self.command).args(&["update", "-g"]).check_run()?; run_type.execute(&self.command).args(args).check_run()?;
} }
Ok(()) Ok(())