2019-01-01 22:22:07 +02:00
|
|
|
use crate::error::Error;
|
|
|
|
|
use crate::executor::{CommandExt, RunType};
|
2018-12-15 21:52:21 +02:00
|
|
|
use crate::terminal::print_separator;
|
2019-01-01 22:22:07 +02:00
|
|
|
use crate::utils::{which, PathExt};
|
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 }
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-11 16:43:26 +02:00
|
|
|
fn root(&self) -> Result<PathBuf, Error> {
|
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
|
|
|
}
|
|
|
|
|
|
2018-12-31 13:26:17 +02:00
|
|
|
fn upgrade(&self, run_type: RunType) -> Result<(), Error> {
|
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(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[must_use]
|
2018-12-31 13:26:17 +02:00
|
|
|
pub fn run_npm_upgrade(base_dirs: &BaseDirs, run_type: RunType) -> Option<(&'static str, bool)> {
|
2018-08-19 14:45:23 +03:00
|
|
|
if let Some(npm) = which("npm").map(NPM::new) {
|
|
|
|
|
if let Ok(npm_root) = npm.root() {
|
|
|
|
|
if npm_root.is_descendant_of(base_dirs.home_dir()) {
|
2018-12-05 11:34:08 +02:00
|
|
|
print_separator("Node Package Manager");
|
2018-12-31 13:26:17 +02:00
|
|
|
let success = npm.upgrade(run_type).is_ok();
|
2018-08-19 14:45:23 +03:00
|
|
|
return Some(("NPM", success));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[must_use]
|
2018-12-31 13:26:17 +02:00
|
|
|
pub fn yarn_global_update(run_type: RunType) -> Option<(&'static str, bool)> {
|
2018-08-19 14:45:23 +03:00
|
|
|
if let Some(yarn) = which("yarn") {
|
2018-12-05 11:34:08 +02:00
|
|
|
print_separator("Yarn");
|
2018-08-19 14:45:23 +03:00
|
|
|
|
2018-12-11 16:43:26 +02:00
|
|
|
let success = || -> Result<(), Error> {
|
2018-12-31 22:00:34 +02:00
|
|
|
run_type.execute(&yarn).args(&["global", "upgrade", "-s"]).check_run()?;
|
2018-08-19 14:45:23 +03:00
|
|
|
Ok(())
|
2018-12-11 16:00:19 +02:00
|
|
|
}()
|
|
|
|
|
.is_ok();
|
2018-08-19 14:45:23 +03:00
|
|
|
|
|
|
|
|
return Some(("yarn", success));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|