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.8 KiB

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