Invoke NPM only if the global installation dir is in the home directory (fix #21)
This commit is contained in:
24
src/main.rs
24
src/main.rs
@@ -6,6 +6,7 @@ extern crate termion;
|
|||||||
|
|
||||||
mod git;
|
mod git;
|
||||||
mod linux;
|
mod linux;
|
||||||
|
mod npm;
|
||||||
mod report;
|
mod report;
|
||||||
mod steps;
|
mod steps;
|
||||||
mod terminal;
|
mod terminal;
|
||||||
@@ -16,7 +17,7 @@ use git::Git;
|
|||||||
use report::{Report, Reporter};
|
use report::{Report, Reporter};
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::env::home_dir;
|
use std::env::home_dir;
|
||||||
use std::path::PathBuf;
|
use std::path::{Path, PathBuf};
|
||||||
use std::process::ExitStatus;
|
use std::process::ExitStatus;
|
||||||
use steps::*;
|
use steps::*;
|
||||||
use terminal::Terminal;
|
use terminal::Terminal;
|
||||||
@@ -46,6 +47,19 @@ fn home_path(p: &str) -> PathBuf {
|
|||||||
path
|
path
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_ancestor(ancestor: &Path, path: &Path) -> bool {
|
||||||
|
let mut p = path;
|
||||||
|
while let Some(parent) = p.parent() {
|
||||||
|
if parent == ancestor {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
p = parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
fn tpm() -> Option<PathBuf> {
|
fn tpm() -> Option<PathBuf> {
|
||||||
let mut path = home_dir().unwrap();
|
let mut path = home_dir().unwrap();
|
||||||
@@ -129,9 +143,13 @@ fn main() -> Result<(), Error> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(npm) = which("npm") {
|
if let Ok(npm) = which("npm").map(npm::NPM::new) {
|
||||||
|
if let Ok(npm_root) = npm.root() {
|
||||||
|
if is_ancestor(&home_dir().unwrap(), &npm_root) {
|
||||||
terminal.print_separator("Node Package Manager");
|
terminal.print_separator("Node Package Manager");
|
||||||
run_npm(&npm).report("Node Package Manager", &mut reports);
|
npm.upgrade().report("Node Package Manager", &mut reports);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Ok(apm) = which("apm") {
|
if let Ok(apm) = which("apm") {
|
||||||
|
|||||||
32
src/npm.rs
Normal file
32
src/npm.rs
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
use super::Check;
|
||||||
|
use failure;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::process::Command;
|
||||||
|
|
||||||
|
pub struct NPM {
|
||||||
|
command: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NPM {
|
||||||
|
pub fn new(command: PathBuf) -> Self {
|
||||||
|
Self { command }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub 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)?))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn upgrade(&self) -> Result<(), failure::Error> {
|
||||||
|
Command::new(&self.command)
|
||||||
|
.args(&["update", "-g"])
|
||||||
|
.spawn()?
|
||||||
|
.wait()?
|
||||||
|
.check()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
10
src/steps.rs
10
src/steps.rs
@@ -73,16 +73,6 @@ pub fn run_vim(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_npm(npm: &PathBuf) -> Result<(), failure::Error> {
|
|
||||||
Command::new(&npm)
|
|
||||||
.args(&["update", "-g"])
|
|
||||||
.spawn()?
|
|
||||||
.wait()?
|
|
||||||
.check()?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn run_apm(apm: &PathBuf) -> Result<(), failure::Error> {
|
pub fn run_apm(apm: &PathBuf) -> Result<(), failure::Error> {
|
||||||
Command::new(&apm)
|
Command::new(&apm)
|
||||||
.args(&["upgrade", "--confirm=false"])
|
.args(&["upgrade", "--confirm=false"])
|
||||||
|
|||||||
Reference in New Issue
Block a user