2024-03-01 14:29:01 +03:00
|
|
|
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
|
2022-02-10 04:11:00 +03:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-07-04 15:09:00 +03:00
|
|
|
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]))?;
|
|
|
|
|
2024-02-03 23:18:34 +03:00
|
|
|
// we need to run in a separate thread so Node.js consumers
|
2022-07-04 15:09:00 +03:00
|
|
|
// can do work while `tauri dev` is running.
|
2024-08-13 14:53:04 +03:00
|
|
|
std::thread::spawn(move || {
|
|
|
|
let res = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
|
|
|
|
tauri_cli::try_run(args, bin_name)
|
|
|
|
})) {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(e) => {
|
|
|
|
return function.call(
|
|
|
|
Err(Error::new(
|
|
|
|
Status::GenericFailure,
|
|
|
|
"Tauri CLI unexpected panic",
|
|
|
|
)),
|
|
|
|
ThreadsafeFunctionCallMode::Blocking,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Ok(_) => function.call(Ok(true), ThreadsafeFunctionCallMode::Blocking),
|
|
|
|
Err(e) => function.call(
|
|
|
|
Err(Error::new(Status::GenericFailure, format!("{:#}", e))),
|
|
|
|
ThreadsafeFunctionCallMode::Blocking,
|
|
|
|
),
|
|
|
|
}
|
2022-07-04 15:09:00 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-02-10 04:11:00 +03:00
|
|
|
#[napi_derive::napi]
|
2022-07-04 15:09:00 +03:00
|
|
|
pub fn log_error(error: String) {
|
|
|
|
log::error!("{}", error);
|
2022-02-10 04:11:00 +03:00
|
|
|
}
|