Allow running NPM as sudo (fix #690) (#723)

* Allow running NPM as sudo (fix #690)

* asd

* fix
This commit is contained in:
Roey Darwish Dror
2021-06-09 10:52:48 +03:00
committed by GitHub
parent f8648c9bc7
commit f39899c3ff
4 changed files with 46 additions and 11 deletions

View File

@@ -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

View File

@@ -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 {
@@ -204,6 +211,7 @@ pub struct ConfigFile {
linux: Option<Linux>,
git: Option<Git>,
windows: Option<Windows>,
npm: Option<NPM>,
vagrant: Option<Vagrant>,
}
@@ -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);

View File

@@ -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))?;

View File

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