Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Allow running NPM as sudo (fix #690) (#723)
Browse files Browse the repository at this point in the history
* Allow running NPM as sudo (fix #690)

* asd

* fix
  • Loading branch information
r-darwish authored Jun 9, 2021
1 parent f8648c9 commit f39899c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
4 changes: 4 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,7 @@
# to upgrade it. Use this only if you installed Topgrade by using a package
# manager such as Scoop to Cargo
#self_rename = true

[npm]
# Use sudo if the NPM directory isn't owned by the current user
#use_sudo = true
17 changes: 17 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ pub struct Windows {
open_remotes_in_new_terminal: Option<bool>,
}

#[derive(Deserialize, Default, Debug)]
#[serde(deny_unknown_fields)]
#[allow(clippy::upper_case_acronyms)]
pub struct NPM {
use_sudo: Option<bool>,
}

#[derive(Deserialize, Default, Debug)]
#[serde(deny_unknown_fields)]
pub struct Brew {
Expand Down Expand Up @@ -204,6 +211,7 @@ pub struct ConfigFile {
linux: Option<Linux>,
git: Option<Git>,
windows: Option<Windows>,
npm: Option<NPM>,
vagrant: Option<Vagrant>,
}

Expand Down Expand Up @@ -713,6 +721,15 @@ impl Config {
.unwrap_or(false)
}

#[cfg(target_os = "linux")]
pub fn npm_use_sudo(&self) -> bool {
self.config_file
.npm
.as_ref()
.and_then(|npm| npm.use_sudo)
.unwrap_or(false)
}

#[cfg(target_os = "linux")]
str_value!(linux, emerge_sync_flags);

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ fn run() -> Result<()> {
runner.execute(Step::Vim, "vim", || vim::upgrade_vim(&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::Node, "npm", || node::run_npm_upgrade(&base_dirs, run_type))?;
runner.execute(Step::Node, "npm", || node::run_npm_upgrade(&ctx))?;
runner.execute(Step::Node, "yarn", || node::yarn_global_update(run_type))?;
runner.execute(Step::Deno, "deno", || node::deno_upgrade(&ctx))?;
runner.execute(Step::Composer, "composer", || generic::run_composer_update(&ctx))?;
Expand Down
34 changes: 24 additions & 10 deletions src/steps/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,25 @@ impl NPM {
.map(|s| PathBuf::from(s.trim()))
}

fn upgrade(&self, run_type: RunType) -> Result<()> {
run_type.execute(&self.command).args(&["update", "-g"]).check_run()?;
fn upgrade(&self, run_type: RunType, use_sudo: bool) -> Result<()> {
if use_sudo {
run_type
.execute("sudo")
.arg(&self.command)
.args(&["update", "-g"])
.check_run()?;
} else {
run_type.execute(&self.command).args(&["update", "-g"]).check_run()?;
}

Ok(())
}
}

pub fn run_npm_upgrade(_base_dirs: &BaseDirs, run_type: RunType) -> Result<()> {
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")]
{
Expand All @@ -53,17 +63,21 @@ pub fn run_npm_upgrade(_base_dirs: &BaseDirs, run_type: RunType) -> Result<()> {
let uid = Uid::effective();

if metadata.uid() != uid.as_raw() {
return Err(SkipStep(format!(
"NPM root at {} is owned by {} which is not the current user",
npm_root.display(),
metadata.uid()
))
.into());
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());
}
}
}

print_separator("Node Package Manager");
npm.upgrade(run_type)
npm.upgrade(ctx.run_type(), use_sudo)
}

pub fn yarn_global_update(run_type: RunType) -> Result<()> {
Expand Down

0 comments on commit f39899c

Please sign in to comment.