You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.5 KiB
67 lines
1.5 KiB
use crate::command_runner::CommandRunner;
|
|
use crate::symbols::Symbol;
|
|
use std::error::Error;
|
|
use std::fmt;
|
|
use std::path::Path;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Install<'a, T: AsRef<Path>, C: CommandRunner> {
|
|
target: T,
|
|
command_runner: &'a C,
|
|
}
|
|
|
|
impl<'a, T: AsRef<Path>, C: CommandRunner> Install<'a, T, C> {
|
|
pub fn new(target: T, command_runner: &'a C) -> Self {
|
|
Install {
|
|
target,
|
|
command_runner,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T: AsRef<Path>, C: CommandRunner> fmt::Display for Install<'_, T, C> {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(
|
|
f,
|
|
"npm install in {}",
|
|
self.target.as_ref().to_str().unwrap()
|
|
)
|
|
}
|
|
}
|
|
|
|
impl<T: AsRef<Path>, C: CommandRunner> Symbol for Install<'_, T, C> {
|
|
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
|
|
if !self.target.as_ref().exists() {
|
|
return Ok(false);
|
|
}
|
|
let result = self.command_runner.run_with_args(
|
|
"sh",
|
|
args![
|
|
"-c",
|
|
format!("cd '{}' && npm ls", self.target.as_ref().to_str().unwrap()),
|
|
],
|
|
)?;
|
|
Ok(
|
|
result.status.success()
|
|
&& !String::from_utf8(result.stdout)
|
|
.unwrap()
|
|
.contains("(empty)"),
|
|
)
|
|
}
|
|
|
|
fn execute(&self) -> Result<(), Box<dyn Error>> {
|
|
self.command_runner.run_successfully(
|
|
"sh",
|
|
args![
|
|
"-c",
|
|
format!(
|
|
"cd '{}' && npm install --production --unsafe-perm",
|
|
self.target.as_ref().to_str().unwrap()
|
|
),
|
|
],
|
|
)
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {}
|