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.

75 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. Self {
  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 result = self
  36. .command_runner
  37. .run_with_args(
  38. "sh",
  39. args![
  40. "-c",
  41. format!("cd '{}' && npm ls", self.target.as_ref().to_str().unwrap()),
  42. ],
  43. )
  44. .await?;
  45. Ok(
  46. result.status.success()
  47. && !String::from_utf8(result.stdout)
  48. .unwrap()
  49. .contains("(empty)"),
  50. )
  51. }
  52. async fn execute(&self) -> Result<(), Box<dyn Error>> {
  53. self
  54. .command_runner
  55. .run_successfully(
  56. "sh",
  57. args![
  58. "-c",
  59. format!(
  60. "cd '{}' && npm install --production --unsafe-perm",
  61. self.target.as_ref().to_str().unwrap()
  62. ),
  63. ],
  64. )
  65. .await
  66. }
  67. }
  68. #[cfg(test)]
  69. mod test {}