Sfoglia il codice sorgente

feat(cli): add `--no-watch` argument to the dev command, closes #4617 (#4793)

Lucas Fernandes Nogueira 3 anni fa
parent
commit
0983d7ce7f
3 ha cambiato i file con 35 aggiunte e 9 eliminazioni
  1. 6 0
      .changes/no-watch.md
  2. 9 4
      tooling/cli/src/dev.rs
  3. 20 5
      tooling/cli/src/interface/rust.rs

+ 6 - 0
.changes/no-watch.md

@@ -0,0 +1,6 @@
+---
+"cli.rs": minor
+"cli.js": minor
+---
+
+Added `--no-watch` argument to the `dev` command to disable the file watcher.

+ 9 - 4
tooling/cli/src/dev.rs

@@ -58,6 +58,9 @@ pub struct Options {
   pub release_mode: bool,
   /// Command line arguments passed to the runner
   pub args: Vec<String>,
+  /// Disable the file watcher
+  #[clap(long)]
+  pub no_watch: bool,
 }
 
 pub fn command(options: Options) -> Result<()> {
@@ -234,14 +237,16 @@ fn command_internal(mut options: Options) -> Result<()> {
   let mut interface = AppInterface::new(config.lock().unwrap().as_ref().unwrap())?;
 
   let exit_on_panic = options.exit_on_panic;
+  let no_watch = options.no_watch;
   interface.dev(options.into(), move |status, reason| {
-    on_dev_exit(status, reason, exit_on_panic)
+    on_dev_exit(status, reason, exit_on_panic, no_watch)
   })
 }
 
-fn on_dev_exit(status: ExitStatus, reason: ExitReason, exit_on_panic: bool) {
-  if !matches!(reason, ExitReason::TriggeredKill)
-    && (exit_on_panic || matches!(reason, ExitReason::NormalExit))
+fn on_dev_exit(status: ExitStatus, reason: ExitReason, exit_on_panic: bool, no_watch: bool) {
+  if no_watch
+    || (!matches!(reason, ExitReason::TriggeredKill)
+      && (exit_on_panic || matches!(reason, ExitReason::NormalExit)))
   {
     kill_before_dev_process();
     #[cfg(not(debug_assertions))]

+ 20 - 5
tooling/cli/src/interface/rust.rs

@@ -11,7 +11,7 @@ use std::{
   str::FromStr,
   sync::{
     atomic::{AtomicBool, Ordering},
-    mpsc::channel,
+    mpsc::{channel, sync_channel},
     Arc, Mutex,
   },
   time::{Duration, Instant},
@@ -50,6 +50,7 @@ pub struct Options {
   pub features: Option<Vec<String>>,
   pub args: Vec<String>,
   pub config: Option<String>,
+  pub no_watch: bool,
 }
 
 impl From<crate::build::Options> for Options {
@@ -61,6 +62,7 @@ impl From<crate::build::Options> for Options {
       features: options.features,
       args: options.args,
       config: options.config,
+      no_watch: true,
     }
   }
 }
@@ -74,6 +76,7 @@ impl From<crate::dev::Options> for Options {
       features: options.features,
       args: options.args,
       config: options.config,
+      no_watch: options.no_watch,
     }
   }
 }
@@ -215,11 +218,23 @@ impl Interface for Rust {
     let on_exit = Arc::new(on_exit);
 
     let on_exit_ = on_exit.clone();
-    let child = self.run_dev(options.clone(), move |status, reason| {
-      on_exit_(status, reason)
-    })?;
 
-    self.run_dev_watcher(child, options, on_exit)
+    if options.no_watch {
+      let (tx, rx) = sync_channel(1);
+      self.run_dev(options, move |status, reason| {
+        tx.send(()).unwrap();
+        on_exit_(status, reason)
+      })?;
+
+      rx.recv().unwrap();
+      Ok(())
+    } else {
+      let child = self.run_dev(options.clone(), move |status, reason| {
+        on_exit_(status, reason)
+      })?;
+
+      self.run_dev_watcher(child, options, on_exit)
+    }
   }
 }