2018-08-26 16:12:59 +03:00
|
|
|
use super::executor::Executor;
|
2018-12-05 11:34:08 +02:00
|
|
|
use super::terminal::print_separator;
|
2018-08-19 14:45:23 +03:00
|
|
|
use super::utils::{which, Check, PathExt};
|
|
|
|
|
use directories::BaseDirs;
|
|
|
|
|
use failure;
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
use std::process::Command;
|
|
|
|
|
|
|
|
|
|
struct NPM {
|
|
|
|
|
command: PathBuf,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl NPM {
|
|
|
|
|
fn new(command: PathBuf) -> Self {
|
|
|
|
|
Self { command }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn root(&self) -> Result<PathBuf, failure::Error> {
|
|
|
|
|
let output = Command::new(&self.command).args(&["root", "-g"]).output()?;
|
|
|
|
|
|
|
|
|
|
output.status.check()?;
|
|
|
|
|
|
|
|
|
|
Ok(PathBuf::from(&String::from_utf8(output.stdout)?))
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-26 16:12:59 +03:00
|
|
|
fn upgrade(&self, dry_run: bool) -> Result<(), failure::Error> {
|
|
|
|
|
Executor::new(&self.command, dry_run)
|
2018-08-19 14:45:23 +03:00
|
|
|
.args(&["update", "-g"])
|
|
|
|
|
.spawn()?
|
|
|
|
|
.wait()?
|
|
|
|
|
.check()?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[must_use]
|
2018-12-05 11:34:08 +02:00
|
|
|
pub fn run_npm_upgrade(base_dirs: &BaseDirs, dry_run: bool) -> 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-08-26 16:12:59 +03:00
|
|
|
let success = npm.upgrade(dry_run).is_ok();
|
2018-08-19 14:45:23 +03:00
|
|
|
return Some(("NPM", success));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[must_use]
|
2018-12-05 11:34:08 +02:00
|
|
|
pub fn yarn_global_update(dry_run: bool) -> 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
|
|
|
|
|
|
|
|
let success = || -> Result<(), failure::Error> {
|
2018-08-26 16:12:59 +03:00
|
|
|
Executor::new(&yarn, dry_run)
|
2018-08-19 14:45:23 +03:00
|
|
|
.args(&["global", "upgrade", "-s"])
|
|
|
|
|
.spawn()?
|
|
|
|
|
.wait()?
|
|
|
|
|
.check()?;
|
|
|
|
|
Ok(())
|
2018-12-11 16:00:19 +02:00
|
|
|
}()
|
|
|
|
|
.is_ok();
|
2018-08-19 14:45:23 +03:00
|
|
|
|
|
|
|
|
return Some(("yarn", success));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
}
|