Support sudo with pnpm (fix #759) (#772)

* Support sudo with pnpm

* Clippy

* format

* Clippy

* Clippy
This commit is contained in:
Roey Darwish Dror
2021-10-25 22:27:35 +03:00
committed by GitHub
parent fb18af12ac
commit 58491c4f8a
2 changed files with 47 additions and 26 deletions

View File

@@ -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<bool> {
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<bool> {
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<()> {