Files
topgrade/src/steps/node.rs

102 lines
2.9 KiB
Rust
Raw Normal View History

#![allow(unused_imports)]
2019-01-01 22:22:07 +02:00
use crate::executor::{CommandExt, RunType};
2018-12-15 21:52:21 +02:00
use crate::terminal::print_separator;
2019-03-10 21:48:49 +02:00
use crate::utils::{require, PathExt};
use crate::{error::SkipStep, execution_context::ExecutionContext};
use anyhow::Result;
2018-08-19 14:45:23 +03:00
use directories::BaseDirs;
use log::debug;
#[cfg(unix)]
use nix::unistd::Uid;
#[cfg(unix)]
use std::os::unix::prelude::MetadataExt;
2018-08-19 14:45:23 +03:00
use std::path::PathBuf;
use std::process::Command;
2021-04-06 09:52:34 +03:00
#[allow(clippy::upper_case_acronyms)]
2018-08-19 14:45:23 +03:00
struct NPM {
command: PathBuf,
}
impl NPM {
fn new(command: PathBuf) -> Self {
Self { command }
}
#[cfg(target_os = "linux")]
fn root(&self) -> Result<PathBuf> {
2019-01-01 22:22:07 +02:00
Command::new(&self.command)
2018-12-11 16:43:26 +02:00
.args(&["root", "-g"])
2019-01-01 22:22:07 +02:00
.check_output()
2021-01-15 06:59:13 +02:00
.map(|s| PathBuf::from(s.trim()))
2018-08-19 14:45:23 +03:00
}
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()?;
}
2018-08-19 14:45:23 +03:00
Ok(())
}
}
pub fn run_npm_upgrade(ctx: &ExecutionContext) -> Result<()> {
2019-03-10 21:48:49 +02:00
let npm = require("npm").map(NPM::new)?;
#[allow(unused_mut)]
let mut use_sudo = false;
#[cfg(target_os = "linux")]
{
let npm_root = npm.root()?;
2021-01-15 06:59:13 +02:00
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());
}
}
2018-08-19 14:45:23 +03:00
}
2019-03-10 21:48:49 +02:00
print_separator("Node Package Manager");
npm.upgrade(ctx.run_type(), use_sudo)
2019-03-10 21:48:49 +02:00
}
2018-08-19 14:45:23 +03:00
2021-06-27 14:48:46 +02:00
pub fn pnpm_global_update(run_type: RunType) -> Result<()> {
let pnpm = require("pnpm")?;
print_separator("Performant Node Package Manager");
run_type.execute(&pnpm).args(&["update", "-g"]).check_run()
}
pub fn deno_upgrade(ctx: &ExecutionContext) -> Result<()> {
let deno = require("deno")?;
let deno_dir = ctx.base_dirs().home_dir().join(".deno");
if !deno.canonicalize()?.is_descendant_of(&deno_dir) {
let skip_reason = SkipStep("Deno installed outside of .deno directory".to_string());
return Err(skip_reason.into());
}
print_separator("Deno");
ctx.run_type().execute(&deno).arg("upgrade").check_run()
}