pipa/runtime/
process_task.rs1use std::os::unix::process::ExitStatusExt;
2
3use super::context::JSContext;
4use super::extension::MacroTaskExtension;
5
6#[derive(Debug)]
7pub struct ProcessResult {
8 pub stdout: Vec<u8>,
9 pub stderr: Vec<u8>,
10 pub code: i32,
11 pub signal: Option<i32>,
12 pub error: Option<String>,
13}
14
15pub struct ProcessExtension;
16
17impl ProcessExtension {
18 pub fn new() -> Self {
19 ProcessExtension
20 }
21}
22
23impl MacroTaskExtension for ProcessExtension {
24 fn tick(&mut self, _ctx: &mut JSContext) -> Result<bool, String> {
25 Ok(false)
26 }
27
28 fn has_pending(&self) -> bool {
29 false
30 }
31
32 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
33 self
34 }
35}
36
37pub fn run_command_sync(command: &str, args: &[String]) -> ProcessResult {
38 match std::process::Command::new(command).args(args).output() {
39 Ok(output) => {
40 let code = output.status.code().unwrap_or(-1);
41 let signal = if output.status.signal().is_some() {
42 output.status.signal()
43 } else {
44 None
45 };
46 ProcessResult {
47 stdout: output.stdout,
48 stderr: output.stderr,
49 code,
50 signal,
51 error: None,
52 }
53 }
54 Err(e) => ProcessResult {
55 stdout: Vec::new(),
56 stderr: Vec::new(),
57 code: -1,
58 signal: None,
59 error: Some(format!("spawn failed: {e}")),
60 },
61 }
62}