2020-01-28 16:27:43 +02:00
|
|
|
#![allow(unused_imports)]
|
2020-04-03 11:47:51 +03:00
|
|
|
|
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};
|
2020-10-01 15:59:06 -04:00
|
|
|
use crate::{error::SkipStep, execution_context::ExecutionContext};
|
2019-12-11 23:05:38 +02:00
|
|
|
use anyhow::Result;
|
2018-08-19 14:45:23 +03:00
|
|
|
use directories::BaseDirs;
|
2021-01-14 09:49:18 +02:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
struct NPM {
|
|
|
|
|
command: PathBuf,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl NPM {
|
|
|
|
|
fn new(command: PathBuf) -> Self {
|
|
|
|
|
Self { command }
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-14 09:49:18 +02:00
|
|
|
#[cfg(target_os = "linux")]
|
2019-12-11 23:05:38 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2019-12-11 23:05:38 +02: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(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-28 16:27:43 +02:00
|
|
|
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)?;
|
2020-01-28 16:27:43 +02:00
|
|
|
|
2021-01-14 09:49:18 +02:00
|
|
|
#[cfg(target_os = "linux")]
|
2020-01-28 16:27:43 +02:00
|
|
|
{
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-14 09:49:18 +02:00
|
|
|
let metadata = std::fs::metadata(&npm_root)?;
|
|
|
|
|
let uid = Uid::effective();
|
|
|
|
|
|
|
|
|
|
if metadata.uid() != uid.as_raw() {
|
2020-08-21 23:04:36 +03:00
|
|
|
return Err(SkipStep(format!(
|
2021-01-14 09:49:18 +02:00
|
|
|
"NPM root at {} is owned by {} which is not the current user",
|
|
|
|
|
npm_root.display(),
|
2021-01-15 06:59:13 +02:00
|
|
|
metadata.uid()
|
2020-08-21 23:04:36 +03:00
|
|
|
))
|
|
|
|
|
.into());
|
2020-01-28 16:27:43 +02:00
|
|
|
}
|
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
|
|
|
|
2019-12-11 23:05:38 +02: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-23 15:58:41 +03:00
|
|
|
let output = Command::new(&yarn).arg("--version").string_output()?;
|
2020-04-03 11:47:51 +03:00
|
|
|
if output.contains("Hadoop") {
|
2020-08-21 23:04:36 +03:00
|
|
|
return Err(SkipStep(String::from("Installed yarn is Hadoop's yarn")).into());
|
2020-04-03 11:47:51 +03:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
2020-10-01 15:59:06 -04:00
|
|
|
|
|
|
|
|
pub fn deno_upgrade(ctx: &ExecutionContext) -> Result<()> {
|
|
|
|
|
let deno = require("deno")?;
|
|
|
|
|
|
|
|
|
|
print_separator("Deno");
|
|
|
|
|
ctx.run_type().execute(&deno).arg("upgrade").check_run()
|
|
|
|
|
}
|