A library for writing host-specific, single-binary configuration management and deployment tools
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.

49 lines
1.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
  1. use std::error::Error;
  2. use std::fmt;
  3. use std::path::Path;
  4. use command_runner::CommandRunner;
  5. use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
  6. pub struct NpmInstall<'a, C: 'a + CommandRunner> {
  7. target: &'a str,
  8. command_runner: &'a C
  9. }
  10. impl<'a, C: CommandRunner> NpmInstall<'a, C> {
  11. pub fn new(target: &'a str, command_runner: &'a C) -> Self {
  12. NpmInstall { target, command_runner }
  13. }
  14. }
  15. impl<'a, C: CommandRunner> fmt::Display for NpmInstall<'a, C> {
  16. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  17. write!(f, "npm install in {}", self.target)
  18. }
  19. }
  20. impl<'a, C: CommandRunner> Symbol for NpmInstall<'a, C> {
  21. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  22. if !Path::new(self.target).exists() {
  23. return Ok(false);
  24. }
  25. let result = try!(self.command_runner.run_with_args("sh", &["-c", &format!("cd '{}' && npm ls", self.target)]));
  26. Ok(result.status.success() && !String::from_utf8(result.stdout).unwrap().contains("(empty)"))
  27. }
  28. fn execute(&self) -> Result<(), Box<dyn Error>> {
  29. self.command_runner.run_successfully("sh", &["-c", &format!("cd '{}' && npm install --production --unsafe-perm", self.target)])
  30. }
  31. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  32. Box::new(SymbolAction::new(runner, self))
  33. }
  34. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> where Self: 'b {
  35. Box::new(OwnedSymbolAction::new(runner, *self))
  36. }
  37. }
  38. #[cfg(test)]
  39. mod test {
  40. }