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, T: AsRef, C: CommandRunner> { target: T, command_runner: &'a C, } impl<'a, T: AsRef, C: CommandRunner> NpmInstall<'a, T, C> { pub fn new(target: T, command_runner: &'a C) -> Self { NpmInstall { target, command_runner, } } } impl<'a, T: AsRef, C: CommandRunner> fmt::Display for NpmInstall<'a, T, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( f, "npm install in {}", self.target.as_ref().to_str().unwrap() ) } } impl<'a, T: AsRef, C: CommandRunner> Symbol for NpmInstall<'a, T, C> { fn target_reached(&self) -> Result> { 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> { self.command_runner.run_successfully( "sh", args![ "-c", format!( "cd '{}' && npm install --production --unsafe-perm", self.target.as_ref().to_str().unwrap() ), ], ) } 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 {}