tauri to manage and kill ws-server
This commit is contained in:
1 parent
126a3749f9
commit
3e3d2dcbcb
3 files changed
+51
-7
No files matched your search
+4
-3
@@ -19,9 +19,10 @@
|
||||
"concurrently": "^10.0.3"
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "concurrently 'pnpm:dev:*'",
|
||||
"dev:server": "node server.js",
|
||||
"dev:tauri": "tauri dev",
|
||||
"dev": "tauri dev",
|
||||
"browser": "concurrently 'pnpm:b:*'",
|
||||
"b:server": "node server.js",
|
||||
"b:vite": "vitepress dev docs --open",
|
||||
"vite": "vitepress dev docs",
|
||||
"build": "vitepress build docs",
|
||||
"preview": "vitepress preview docs"
|
||||
|
||||
+44
-2
@@ -1,6 +1,17 @@
|
||||
use tauri::Manager;
|
||||
|
||||
use std::{process::{Child, Command}, sync::Mutex};
|
||||
|
||||
struct State {
|
||||
child: Mutex<Option<Child>>,
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.manage(State {
|
||||
child: Mutex::new(None),
|
||||
})
|
||||
.setup(|app| {
|
||||
if cfg!(debug_assertions) {
|
||||
app.handle().plugin(
|
||||
@@ -9,8 +20,39 @@ pub fn run() {
|
||||
.build(),
|
||||
)?;
|
||||
}
|
||||
|
||||
#[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(())
|
||||
})
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
.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");
|
||||
}
|
||||
|
||||
}
|
||||
_ => {}
|
||||
});
|
||||
}
|
||||
@@ -13,9 +13,10 @@
|
||||
"windows": [
|
||||
{
|
||||
"title": "yt2press",
|
||||
"width": 800,
|
||||
"height": 600,
|
||||
"width": 1024,
|
||||
"height": 768,
|
||||
"resizable": true,
|
||||
"maximized": true,
|
||||
"fullscreen": false
|
||||
}
|
||||
],
|
||||
|
||||
Reference in new issue
Block a user