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.

67 lines
1.5 KiB

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
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
  1. use crate::command_runner::CommandRunner;
  2. use crate::symbols::Symbol;
  3. use std::error::Error;
  4. use std::fmt;
  5. use std::path::Path;
  6. #[derive(Debug)]
  7. pub struct Install<'a, T: AsRef<Path>, C: CommandRunner> {
  8. target: T,
  9. command_runner: &'a C,
  10. }
  11. impl<'a, T: AsRef<Path>, C: CommandRunner> Install<'a, T, C> {
  12. pub fn new(target: T, command_runner: &'a C) -> Self {
  13. Install {
  14. target,
  15. command_runner,
  16. }
  17. }
  18. }
  19. impl<T: AsRef<Path>, C: CommandRunner> fmt::Display for Install<'_, T, C> {
  20. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  21. write!(
  22. f,
  23. "npm install in {}",
  24. self.target.as_ref().to_str().unwrap()
  25. )
  26. }
  27. }
  28. impl<T: AsRef<Path>, C: CommandRunner> Symbol for Install<'_, T, C> {
  29. fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
  30. if !self.target.as_ref().exists() {
  31. return Ok(false);
  32. }
  33. let result = self.command_runner.run_with_args(
  34. "sh",
  35. args![
  36. "-c",
  37. format!("cd '{}' && npm ls", self.target.as_ref().to_str().unwrap()),
  38. ],
  39. )?;
  40. Ok(
  41. result.status.success()
  42. && !String::from_utf8(result.stdout)
  43. .unwrap()
  44. .contains("(empty)"),
  45. )
  46. }
  47. fn execute(&self) -> Result<(), Box<dyn Error>> {
  48. self.command_runner.run_successfully(
  49. "sh",
  50. args![
  51. "-c",
  52. format!(
  53. "cd '{}' && npm install --production --unsafe-perm",
  54. self.target.as_ref().to_str().unwrap()
  55. ),
  56. ],
  57. )
  58. }
  59. }
  60. #[cfg(test)]
  61. mod test {}