Skip to content

Commit

Permalink
Support sudo with pnpm (fix r-darwish#759) (r-darwish#772)
Browse files Browse the repository at this point in the history
* Support sudo with pnpm

* Clippy

* format

* Clippy

* Clippy
  • Loading branch information
r-darwish authored Oct 25, 2021
1 parent fb18af1 commit 58491c4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))?;
Expand Down
65 changes: 43 additions & 22 deletions src/steps/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,46 +45,67 @@ 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());
}

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)
}
}

#[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");
npm.upgrade(ctx.run_type(), use_sudo)
#[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(run_type: RunType) -> Result<()> {
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<()> {
Expand Down

0 comments on commit 58491c4

Please sign in to comment.