diff --git a/plugins/shell/guest-js/index.ts b/plugins/shell/guest-js/index.ts index 1ed2ac5d..36ff695f 100644 --- a/plugins/shell/guest-js/index.ts +++ b/plugins/shell/guest-js/index.ts @@ -339,6 +339,20 @@ class Child { pid: this.pid }) } + + /** + * Waits for the child to exit completely, returning the status that it exited with. + * + * @returns A promise with ExitStatus. + * + * @since 2.0.2 + */ + async wait(): Promise { + await invoke('plugin:shell|wait', { + cmd: 'waitChild', + pid: this.pid + }) + } } interface CommandEvents { diff --git a/plugins/shell/src/commands.rs b/plugins/shell/src/commands.rs index 3345bb3a..771788b0 100644 --- a/plugins/shell/src/commands.rs +++ b/plugins/shell/src/commands.rs @@ -13,7 +13,7 @@ use tauri::{ use crate::{ open::Program, - process::{CommandEvent, TerminatedPayload}, + process::{CommandEvent, ExitStatus, TerminatedPayload}, scope::ExecuteArgs, Shell, }; @@ -302,6 +302,19 @@ pub fn kill( Ok(()) } +#[tauri::command] +pub async fn wait( + _window: Window, + shell: State<'_, Shell>, + pid: ChildId, +) -> crate::Result { + if let Some(child) = shell.children.lock().unwrap().get(&pid) { + child.wait().await + } else { + Err(crate::Error::UnknowChildProcess) + } +} + #[tauri::command] pub async fn open( _window: Window, diff --git a/plugins/shell/src/error.rs b/plugins/shell/src/error.rs index 652421b8..5ef03b81 100644 --- a/plugins/shell/src/error.rs +++ b/plugins/shell/src/error.rs @@ -33,6 +33,8 @@ pub enum Error { /// Utf8 error. #[error(transparent)] Utf8(#[from] std::string::FromUtf8Error), + #[error("Child process does'nt exist")] + UnknowChildProcess, } impl Serialize for Error { diff --git a/plugins/shell/src/process/mod.rs b/plugins/shell/src/process/mod.rs index 44f037b0..c0cc6d1e 100644 --- a/plugins/shell/src/process/mod.rs +++ b/plugins/shell/src/process/mod.rs @@ -84,6 +84,14 @@ impl CommandChild { pub fn pid(&self) -> u32 { self.inner.id() } + + /// Waits for the child to exit completely, returning the status that it exited with. + pub async fn wait(self) -> crate::Result { + let exitstatus = self.inner.wait()?; + Ok(ExitStatus { + code: exitstatus.code(), + }) + } } /// Describes the result of a process after it has terminated.