use crate::command_runner::CommandRunner; use crate::symbols::Symbol; use async_trait::async_trait; use std::error::Error; use std::fmt; use std::path::Path; #[derive(Debug)] pub struct Install<'a, T: AsRef, C: CommandRunner> { target: T, command_runner: &'a C, } impl<'a, T: AsRef, C: CommandRunner> Install<'a, T, C> { pub fn new(target: T, command_runner: &'a C) -> Self { Self { target, command_runner, } } } impl, 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() ) } } #[async_trait(?Send)] impl, C: CommandRunner> Symbol for Install<'_, T, C> { async 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 --prod", self.target.as_ref().to_str().unwrap() ), ], ) .await?; Ok( result.status.success() && !String::from_utf8(result.stdout) .unwrap() .contains("(empty)"), ) } async 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() ), ], ) .await } } #[cfg(test)] mod test {}