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