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
33pub fn run_command_sync(command: &str, args: &[String]) -> ProcessResult {
34 match std::process::Command::new(command).args(args).output() {
35 Ok(output) => {
36 let code = output.status.code().unwrap_or(-1);
37 let signal = if output.status.signal().is_some() {
38 output.status.signal()
39 } else {
40 None
41 };
42 ProcessResult {
43 stdout: output.stdout,
44 stderr: output.stderr,
45 code,
46 signal,
47 error: None,
48 }
49 }
50 Err(e) => ProcessResult {
51 stdout: Vec::new(),
52 stderr: Vec::new(),
53 code: -1,
54 signal: None,
55 error: Some(format!("spawn failed: {e}")),
56 },
57 }
58}