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.

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