feat(cli): expose a function to kill dev child

This commit is contained in:
amrbashir
2023-01-28 04:33:19 +02:00
parent e3bfb01411
commit 7b8c23aa37
10 changed files with 63 additions and 7 deletions

View File

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

View File

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

View File

@@ -6,3 +6,4 @@
/* eslint-disable */
export function run(args: Array<string>, binName: string | undefined | null): Promise<void>
export function killDevApp(): Promise<void>

View File

@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
const { run, logError } = require('./index')
const { run, killDevApp, logError } = require('./index')
module.exports.run = (args, binName) => {
return new Promise((resolve, reject) => {
@@ -16,4 +16,16 @@ module.exports.run = (args, binName) => {
})
}
module.exports.killDevApp = () => {
return new Promise((resolve, reject) => {
killDevApp(res => {
if (res instanceof Error) {
reject(res)
} else {
resolve(res)
}
})
})
}
module.exports.logError = logError

View File

@@ -25,6 +25,23 @@ pub fn run(args: Vec<String>, bin_name: Option<String>, callback: JsFunction) ->
Ok(())
}
#[napi_derive::napi]
pub fn kill_dev_app(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::kill_dev_app() {
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);