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

@@ -308,7 +308,7 @@ fn run() -> Result<()> {
runner.execute(Step::Vim, "Neovim", || vim::upgrade_neovim(&base_dirs, &ctx))?; 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::Vim, "voom", || vim::run_voom(&base_dirs, run_type))?;
runner.execute(Step::Node, "npm", || node::run_npm_upgrade(&ctx))?; 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::Deno, "deno", || node::deno_upgrade(&ctx))?;
runner.execute(Step::Composer, "composer", || generic::run_composer_update(&ctx))?; runner.execute(Step::Composer, "composer", || generic::run_composer_update(&ctx))?;
runner.execute(Step::Krew, "krew", || generic::run_krew_upgrade(run_type))?; runner.execute(Step::Krew, "krew", || generic::run_krew_upgrade(run_type))?;

View File

@@ -45,16 +45,10 @@ impl NPM {
Ok(()) 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")] #[cfg(target_os = "linux")]
{ pub fn should_use_sudo(&self) -> Result<bool> {
let npm_root = npm.root()?; let npm_root = self.root()?;
if !npm_root.exists() { if !npm_root.exists() {
return Err(SkipStep(format!("NPM root at {} doesn't exist", npm_root.display(),)).into()); 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 metadata = std::fs::metadata(&npm_root)?;
let uid = Uid::effective(); let uid = Uid::effective();
if metadata.uid() != uid.as_raw() { Ok(metadata.uid() != uid.as_raw() && metadata.uid() == 0)
if metadata.uid() == 0 && (ctx.config().npm_use_sudo()) { }
use_sudo = true; }
#[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 { } else {
return Err(SkipStep(format!( 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())
"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", .into())
npm_root.display(), }
metadata.uid() } else {
)) Ok(false)
.into());
}
} }
} }
pub fn run_npm_upgrade(ctx: &ExecutionContext) -> Result<()> {
let npm = require("npm").map(NPM::new)?;
print_separator("Node Package Manager"); print_separator("Node Package Manager");
npm.upgrade(ctx.run_type(), use_sudo) #[cfg(target_os = "linux")]
{
npm.upgrade(ctx.run_type(), should_use_sudo(&npm, ctx)?)
} }
pub fn pnpm_global_update(run_type: RunType) -> Result<()> { #[cfg(not(target_os = "linux"))]
{
npm.upgrade(ctx.run_type(), false)
}
}
pub fn pnpm_global_update(ctx: &ExecutionContext) -> Result<()> {
let pnpm = require("pnpm")?; let pnpm = require("pnpm")?;
print_separator("Performant Node Package Manager"); 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<()> { pub fn deno_upgrade(ctx: &ExecutionContext) -> Result<()> {