use std::error::Error; use std::fmt; use std::path::Path; use command_runner::CommandRunner; use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner}; pub struct NpmInstall<'a, C: 'a + CommandRunner> { target: &'a str, command_runner: &'a C, } impl<'a, C: CommandRunner> NpmInstall<'a, C> { pub fn new(target: &'a str, command_runner: &'a C) -> Self { NpmInstall { target, command_runner, } } } impl<'a, C: CommandRunner> fmt::Display for NpmInstall<'a, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "npm install in {}", self.target) } } impl<'a, C: CommandRunner> Symbol for NpmInstall<'a, C> { fn target_reached(&self) -> Result> { if !Path::new(self.target).exists() { return Ok(false); } let result = self .command_runner .run_with_args("sh", &["-c", &format!("cd '{}' && npm ls", self.target)])?; Ok( result.status.success() && !String::from_utf8(result.stdout) .unwrap() .contains("(empty)"), ) } fn execute(&self) -> Result<(), Box> { self.command_runner.run_successfully( "sh", &[ "-c", &format!( "cd '{}' && npm install --production --unsafe-perm", self.target ), ], ) } fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box { Box::new(SymbolAction::new(runner, self)) } fn into_action<'b>(self: Box, runner: &'b dyn SymbolRunner) -> Box where Self: 'b, { Box::new(OwnedSymbolAction::new(runner, *self)) } } #[cfg(test)] mod test {}