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.

70 lines
1.6 KiB

7 years ago
7 years ago
7 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
5 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
5 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
7 years ago
5 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,
  14. 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<dyn Error>> {
  25. if !Path::new(self.target).exists() {
  26. return Ok(false);
  27. }
  28. let result = self
  29. .command_runner
  30. .run_with_args("sh", &["-c", &format!("cd '{}' && npm ls", self.target)])?;
  31. Ok(
  32. result.status.success()
  33. && !String::from_utf8(result.stdout)
  34. .unwrap()
  35. .contains("(empty)"),
  36. )
  37. }
  38. fn execute(&self) -> Result<(), Box<dyn Error>> {
  39. self.command_runner.run_successfully(
  40. "sh",
  41. &[
  42. "-c",
  43. &format!(
  44. "cd '{}' && npm install --production --unsafe-perm",
  45. self.target
  46. ),
  47. ],
  48. )
  49. }
  50. fn as_action<'b>(&'b self, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> {
  51. Box::new(SymbolAction::new(runner, self))
  52. }
  53. fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
  54. where
  55. Self: 'b,
  56. {
  57. Box::new(OwnedSymbolAction::new(runner, *self))
  58. }
  59. }
  60. #[cfg(test)]
  61. mod test {}