Files
topgrade/src/steps/node.rs

65 lines
1.5 KiB
Rust
Raw Normal View History

#![allow(unused_imports)]
use crate::error::SkipStep;
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 anyhow::Result;
use log::debug;
2018-08-19 14:45:23 +03:00
use directories::BaseDirs;
use std::path::PathBuf;
use std::process::Command;
struct NPM {
command: PathBuf,
}
impl NPM {
fn new(command: PathBuf) -> Self {
Self { command }
}
#[cfg(not(target_os = "macos"))]
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()
.map(PathBuf::from)
2018-08-19 14:45:23 +03:00
}
fn upgrade(&self, run_type: RunType) -> Result<()> {
2018-12-31 22:00:34 +02:00
run_type.execute(&self.command).args(&["update", "-g"]).check_run()?;
2018-08-19 14:45:23 +03:00
Ok(())
}
}
pub fn run_npm_upgrade(_base_dirs: &BaseDirs, run_type: RunType) -> Result<()> {
2019-03-10 21:48:49 +02:00
let npm = require("npm").map(NPM::new)?;
#[cfg(not(target_os = "macos"))]
{
let npm_root = npm.root()?;
if !npm_root.is_descendant_of(_base_dirs.home_dir()) {
return Err(SkipStep.into());
}
2018-08-19 14:45:23 +03:00
}
2019-03-10 21:48:49 +02:00
print_separator("Node Package Manager");
npm.upgrade(run_type)
}
2018-08-19 14:45:23 +03:00
pub fn yarn_global_update(run_type: RunType) -> Result<()> {
2019-03-10 21:48:49 +02:00
let yarn = require("yarn")?;
2018-08-19 14:45:23 +03:00
2020-05-10 06:45:39 +03:00
let output = Command::new(&yarn).arg("version").string_output()?;
if output.contains("Hadoop") {
debug!("Yarn is Hadoop yarn");
return Err(SkipStep.into());
}
2019-03-10 21:48:49 +02:00
print_separator("Yarn");
run_type.execute(&yarn).args(&["global", "upgrade", "-s"]).check_run()
2018-08-19 14:45:23 +03:00
}