Jelajahi Sumber

fix(cli.js): revert `run` command to be nonblocking

Lucas Nogueira 3 tahun lalu
induk
melakukan
f65eb4f84d

+ 5 - 0
.changes/revert-cli-async.md

@@ -0,0 +1,5 @@
+---
+"cli.js": patch
+---
+
+Revert the `run` command to run in a separate thread.

+ 1 - 0
tooling/cli/Cargo.lock

@@ -2805,6 +2805,7 @@ dependencies = [
 name = "tauri-cli-node"
 version = "0.0.0"
 dependencies = [
+ "log",
  "napi",
  "napi-build",
  "napi-derive",

+ 1 - 0
tooling/cli/node/Cargo.toml

@@ -11,6 +11,7 @@ crate-type = ["cdylib"]
 napi = { version = "2.5", default-features = false, features = ["napi4"] }
 napi-derive = "2.5"
 tauri-cli = { path = ".." }
+log = "0.4.17"
 
 [build-dependencies]
 napi-build = "2.0"

+ 2 - 1
tooling/cli/node/index.d.ts

@@ -3,4 +3,5 @@
 
 /* auto-generated by NAPI-RS */
 
-export function run(args: Array<string>, binName?: string | undefined | null): void
+export function run(args: Array<string>, binName: string | undefined | null, callback: (...args: any[]) => any): void
+export function logError(error: string): void

+ 2 - 1
tooling/cli/node/index.js

@@ -236,6 +236,7 @@ if (!nativeBinding) {
   throw new Error(`Failed to load native binding`)
 }
 
-const { run } = nativeBinding
+const { run, logError } = nativeBinding
 
 module.exports.run = run
+module.exports.logError = logError

+ 3 - 1
tooling/cli/node/main.js

@@ -1,4 +1,4 @@
-const { run } = require('./index')
+const { run, logError } = require('./index')
 
 module.exports.run = (args, binName) => {
   return new Promise((resolve, reject) => {
@@ -11,3 +11,5 @@ module.exports.run = (args, binName) => {
     })
   })
 }
+
+module.exports.logError = logError

+ 25 - 2
tooling/cli/node/src/lib.rs

@@ -2,7 +2,30 @@
 // SPDX-License-Identifier: Apache-2.0
 // SPDX-License-Identifier: MIT
 
+use napi::{
+  threadsafe_function::{ErrorStrategy, ThreadsafeFunction, ThreadsafeFunctionCallMode},
+  Error, JsFunction, Result, Status,
+};
+
+#[napi_derive::napi]
+pub fn run(args: Vec<String>, bin_name: Option<String>, callback: JsFunction) -> Result<()> {
+  let function: ThreadsafeFunction<bool, ErrorStrategy::CalleeHandled> = callback
+    .create_threadsafe_function(0, |ctx| ctx.env.get_boolean(ctx.value).map(|v| vec![v]))?;
+
+  // we need to run in a separate thread so Node.js (e.g. vue-cli-plugin-tauri) consumers
+  // can do work while `tauri dev` is running.
+  std::thread::spawn(move || match tauri_cli::try_run(args, bin_name) {
+    Ok(_) => function.call(Ok(true), ThreadsafeFunctionCallMode::Blocking),
+    Err(e) => function.call(
+      Err(Error::new(Status::GenericFailure, format!("{:#}", e))),
+      ThreadsafeFunctionCallMode::Blocking,
+    ),
+  });
+
+  Ok(())
+}
+
 #[napi_derive::napi]
-pub fn run(args: Vec<String>, bin_name: Option<String>) {
-  tauri_cli::run(args, bin_name);
+pub fn log_error(error: String) {
+  log::error!("{}", error);
 }

+ 4 - 1
tooling/cli/node/tauri.js

@@ -43,4 +43,7 @@ if (binStem === 'node' || binStem === 'nodejs') {
   arguments.unshift(bin)
 }
 
-cli.run(arguments, binName)
+cli.run(arguments, binName).catch((err) => {
+  cli.logError(err.message)
+  process.exit(1)
+})

+ 2 - 2
tooling/cli/node/test/jest/__tests__/template.spec.js

@@ -23,7 +23,7 @@ describe('[CLI] cli.js template', () => {
       await move(outPath, cacheOutPath)
     }
 
-    cli.run(['init', '--directory', process.cwd(), '--force', '--tauri-path', resolve(currentDirName, '../../../../../..'), '--ci'])
+    await cli.run(['init', '--directory', process.cwd(), '--force', '--tauri-path', resolve(currentDirName, '../../../../../..'), '--ci'])
 
     if (outExists) {
       await move(cacheOutPath, outPath)
@@ -39,7 +39,7 @@ describe('[CLI] cli.js template', () => {
     const config = readFileSync(configPath).toString()
     writeFileSync(configPath, config.replace('com.tauri.dev', 'com.tauri.test'))
 
-    cli.run(['build', '--verbose'])
+    await cli.run(['build', '--verbose'])
     process.chdir(cwd)
   })
 })

+ 5 - 2
tooling/cli/src/lib.rs

@@ -73,7 +73,7 @@ fn format_error<I: IntoApp>(err: clap::Error) -> clap::Error {
   err.format(&mut app)
 }
 
-/// Run the Tauri CLI with the passed arguments.
+/// Run the Tauri CLI with the passed arguments, exiting if an error occurrs.
 ///
 /// The passed arguments should have the binary argument(s) stripped out before being passed.
 ///
@@ -96,7 +96,10 @@ where
   }
 }
 
-fn try_run<I, A>(args: I, bin_name: Option<String>) -> Result<()>
+/// Run the Tauri CLI with the passed arguments.
+///
+/// It is similar to [`run`], but instead of exiting on an error, it returns a result.
+pub fn try_run<I, A>(args: I, bin_name: Option<String>) -> Result<()>
 where
   I: IntoIterator<Item = A>,
   A: Into<OsString> + Clone,