Pārlūkot izejas kodu

fix(cli.rs): remove startup delay in `tauri dev` (#3999)

* fix(cli.rs): remove startup delay in `tauri dev`

* change timeout [skip ci]

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Amr Bashir 3 gadi atpakaļ
vecāks
revīzija
bbabc8cd1e

+ 7 - 0
.changes/cli.rs-dev-update-check-delay.md

@@ -0,0 +1,7 @@
+---
+"cli.rs": patch
+"cli.js": patch
+---
+
+* Remove startup delay in `tauri dev` caused by checking for a newer cli version. The check is now done upon process exit.
+* Add `TAURI_SKIP_UPDATE_CHECK` env variable to skip checking for a newer CLI version.

+ 22 - 28
tooling/cli/src/dev.rs

@@ -71,6 +71,8 @@ pub fn command(options: Options) -> Result<()> {
   let r = command_internal(options);
   if r.is_err() {
     kill_before_dev_process();
+    #[cfg(not(debug_assertions))]
+    let _ = check_for_updates();
   }
   r
 }
@@ -78,21 +80,6 @@ pub fn command(options: Options) -> Result<()> {
 fn command_internal(options: Options) -> Result<()> {
   let logger = Logger::new("tauri:dev");
 
-  #[cfg(not(debug_assertions))]
-  match check_for_updates() {
-    Ok((msg, sleep)) => {
-      if sleep {
-        logger.log(msg);
-        std::thread::sleep(std::time::Duration::from_secs(3));
-      } else {
-        logger.log(msg);
-      }
-    }
-    Err(e) => {
-      logger.log(e.to_string());
-    }
-  };
-
   let tauri_path = tauri_dir();
   set_current_dir(&tauri_path).with_context(|| "failed to change current working directory")?;
   let merge_config = if let Some(config) = &options.config {
@@ -160,6 +147,8 @@ fn command_internal(options: Options) -> Result<()> {
 
       let _ = ctrlc::set_handler(move || {
         kill_before_dev_process();
+        #[cfg(not(debug_assertions))]
+        let _ = check_for_updates();
         exit(130);
       });
     }
@@ -297,20 +286,21 @@ fn command_internal(options: Options) -> Result<()> {
 }
 
 #[cfg(not(debug_assertions))]
-fn check_for_updates() -> Result<(String, bool)> {
-  let current_version = crate::info::cli_current_version()?;
-  let current = semver::Version::parse(&current_version)?;
-
-  let upstream_version = crate::info::cli_upstream_version()?;
-  let upstream = semver::Version::parse(&upstream_version)?;
-  if upstream.gt(&current) {
-    let message = format!(
-      "🚀 A new version of Tauri CLI is avaliable! [{}]",
-      upstream.to_string()
-    );
-    return Ok((message, true));
+fn check_for_updates() -> Result<()> {
+  if std::env::var_os("TAURI_SKIP_UPDATE_CHECK") != Some("true".into()) {
+    let current_version = crate::info::cli_current_version()?;
+    let current = semver::Version::parse(&current_version)?;
+
+    let upstream_version = crate::info::cli_upstream_version()?;
+    let upstream = semver::Version::parse(&upstream_version)?;
+    if current < upstream {
+      println!(
+        "🚀 A new version of Tauri CLI is avaliable! [{}]",
+        upstream.to_string()
+      );
+    };
   }
-  Ok(("🎉 Tauri CLI is up-to-date!".into(), false))
+  Ok(())
 }
 
 fn lookup<F: FnMut(FileType, PathBuf)>(dir: &Path, mut f: F) {
@@ -534,6 +524,8 @@ fn start_app(
     if exit_on_panic {
       if !manually_killed_app.load(Ordering::Relaxed) {
         kill_before_dev_process();
+        #[cfg(not(debug_assertions))]
+        let _ = check_for_updates();
         exit(status.code().unwrap_or(0));
       }
     } else {
@@ -551,6 +543,8 @@ fn start_app(
       //    - and error is not a cargo compilation error (using stderr heuristics)
       if status.success() || (status.code() == Some(101) && !is_cargo_compile_error) {
         kill_before_dev_process();
+        #[cfg(not(debug_assertions))]
+        let _ = check_for_updates();
         exit(status.code().unwrap_or(1));
       }
     }

+ 1 - 0
tooling/cli/src/info.rs

@@ -99,6 +99,7 @@ pub(crate) fn cli_upstream_version() -> Result<String> {
   let upstream_metadata = match ureq::get(
     "https://raw.githubusercontent.com/tauri-apps/tauri/dev/tooling/cli/metadata.json",
   )
+  .timeout(std::time::Duration::from_secs(3))
   .call()
   {
     Ok(r) => r,