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.

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