tauri to manage and kill ws-server

This commit is contained in:
polv committed 2026-06-25 15:18:23 +07:00
1 parent 126a3749f9
commit 3e3d2dcbcb
5 files changed
+64 -20

No files matched your search

+4 -3
View File
@@ -19,9 +19,10 @@
"concurrently": "^10.0.3" "concurrently": "^10.0.3"
}, },
"scripts": { "scripts": {
"dev": "concurrently 'pnpm:dev:*'", "dev": "tauri dev",
"dev:server": "node server.js", "browser": "concurrently 'pnpm:b:*'",
"dev:tauri": "tauri dev", "b:server": "node server.js",
"b:vite": "vitepress dev docs --open",
"vite": "vitepress dev docs", "vite": "vitepress dev docs",
"build": "vitepress build docs", "build": "vitepress build docs",
"preview": "vitepress preview docs" "preview": "vitepress preview docs"
+1 -1
View File
@@ -1,3 +1,3 @@
fn main() { fn main() {
tauri_build::build() tauri_build::build()
} }
+55 -13
View File
@@ -1,16 +1,58 @@
use tauri::Manager;
use std::{process::{Child, Command}, sync::Mutex};
struct State {
child: Mutex<Option<Child>>,
}
#[cfg_attr(mobile, tauri::mobile_entry_point)] #[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() { pub fn run() {
tauri::Builder::default() tauri::Builder::default()
.setup(|app| { .manage(State {
if cfg!(debug_assertions) { child: Mutex::new(None),
app.handle().plugin( })
tauri_plugin_log::Builder::default() .setup(|app| {
.level(log::LevelFilter::Info) if cfg!(debug_assertions) {
.build(), app.handle().plugin(
)?; tauri_plugin_log::Builder::default()
} .level(log::LevelFilter::Info)
Ok(()) .build(),
}) )?;
.run(tauri::generate_context!()) }
.expect("error while running tauri application");
#[cfg(target_os = "windows")]
{
use std::{env, process::Stdio};
let cwd = env::current_dir()?;
let p = cwd.join("..").join("server.js");
println!("{:?}", p);
let child = Command::new("node")
.arg(p)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()
.expect("spawn ws-server");
let state = app.handle().state::<State>();
*state.child.lock().unwrap() = Some(child);
}
Ok(())
})
.build(tauri::generate_context!())
.expect("error while running tauri application")
.run(|app_handle, event| match event {
// On exit handler should be avoided for mobile
#[cfg(target_os = "windows")]
tauri::RunEvent::ExitRequested { .. } => {
if let Some(mut child) = app_handle.state::<State>().child.lock().unwrap().take() {
let _ = child.kill().expect("ws-server not killed");
}
}
_ => {}
});
} }
+1 -1
View File
@@ -2,5 +2,5 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
fn main() { fn main() {
app_lib::run(); app_lib::run();
} }
+3 -2
View File
@@ -13,9 +13,10 @@
"windows": [ "windows": [
{ {
"title": "yt2press", "title": "yt2press",
"width": 800, "width": 1024,
"height": 600, "height": 768,
"resizable": true, "resizable": true,
"maximized": true,
"fullscreen": false "fullscreen": false
} }
], ],