mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-18 23:43:55 +01:00
Filesystem Sync (#142)
This commit is contained in:
72
src-tauri/yaak-plugins/src/nodejs.rs
Normal file
72
src-tauri/yaak-plugins/src/nodejs.rs
Normal file
@@ -0,0 +1,72 @@
|
||||
use std::net::SocketAddr;
|
||||
use crate::error::Result;
|
||||
use log::info;
|
||||
use serde;
|
||||
use serde::Deserialize;
|
||||
use tauri::path::BaseDirectory;
|
||||
use tauri::{AppHandle, Manager, Runtime};
|
||||
use tauri_plugin_shell::process::CommandEvent;
|
||||
use tauri_plugin_shell::ShellExt;
|
||||
use tokio::sync::watch::Receiver;
|
||||
|
||||
#[derive(Deserialize, Default)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
struct PortFile {
|
||||
port: i32,
|
||||
}
|
||||
|
||||
pub async fn start_nodejs_plugin_runtime<R: Runtime>(
|
||||
app: &AppHandle<R>,
|
||||
addr: SocketAddr,
|
||||
kill_rx: &Receiver<bool>,
|
||||
) -> Result<()> {
|
||||
let plugin_runtime_main = app
|
||||
.path()
|
||||
.resolve("vendored/plugin-runtime", BaseDirectory::Resource)?
|
||||
.join("index.cjs");
|
||||
|
||||
// HACK: Remove UNC prefix for Windows paths to pass to sidecar
|
||||
let plugin_runtime_main = dunce::simplified(plugin_runtime_main.as_path())
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
|
||||
info!("Starting plugin runtime main={}", plugin_runtime_main);
|
||||
|
||||
let cmd = app
|
||||
.shell()
|
||||
.sidecar("yaaknode")?
|
||||
.env("PORT", addr.port().to_string())
|
||||
.args(&[plugin_runtime_main]);
|
||||
|
||||
let (mut child_rx, child) = cmd.spawn()?;
|
||||
info!("Spawned plugin runtime");
|
||||
|
||||
let mut kill_rx = kill_rx.clone();
|
||||
|
||||
tokio::spawn(async move {
|
||||
while let Some(event) = child_rx.recv().await {
|
||||
match event {
|
||||
CommandEvent::Stderr(line) => {
|
||||
print!("{}", String::from_utf8(line).unwrap());
|
||||
}
|
||||
CommandEvent::Stdout(line) => {
|
||||
print!("{}", String::from_utf8(line).unwrap());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Check on child
|
||||
tokio::spawn(async move {
|
||||
kill_rx
|
||||
.wait_for(|b| *b == true)
|
||||
.await
|
||||
.expect("Kill channel errored");
|
||||
info!("Killing plugin runtime");
|
||||
child.kill().expect("Failed to kill plugin runtime");
|
||||
info!("Killed plugin runtime");
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user