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

This commit is contained in:
Lucas Nogueira
2022-07-04 09:09:00 -03:00
parent 3fbaee454d
commit f65eb4f84d
10 changed files with 50 additions and 10 deletions

View File

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

View File

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

View File

@@ -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"

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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>) {
tauri_cli::run(args, bin_name);
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 log_error(error: String) {
log::error!("{}", error);
}

View File

@@ -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)
})

View File

@@ -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)
})
})

View File

@@ -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,