Skip to main content

hyperlane_cli/watch/
fn.rs

1use crate::*;
2
3/// Check if cargo-watch is installed
4///
5/// # Returns
6///
7/// - `bool`: True if cargo-watch is available
8async fn is_cargo_watch_installed() -> bool {
9    Command::new("cargo-watch")
10        .arg("--version")
11        .stdout(Stdio::null())
12        .stderr(Stdio::null())
13        .status()
14        .await
15        .is_ok_and(|status: ExitStatus| status.success())
16}
17
18/// Install cargo-watch using cargo install
19///
20/// # Returns
21///
22/// - `Result<(), std::io::Error>`: Success or error
23async fn install_cargo_watch() -> Result<(), std::io::Error> {
24    log::warn!("cargo-watch not found, installing...");
25    let mut cmd: Command = Command::new("cargo");
26    cmd.arg("install").arg("cargo-watch");
27    cmd.stdout(Stdio::inherit()).stderr(Stdio::inherit());
28    let status: ExitStatus = cmd.status().await?;
29    if !status.success() {
30        return Err(std::io::Error::other("failed to install cargo-watch"));
31    }
32    Ok(())
33}
34
35/// Execute watch command using cargo-watch
36///
37/// # Returns
38///
39/// - `Result<(), std::io::Error>`: Success or error
40pub async fn execute_watch() -> Result<(), std::io::Error> {
41    if !is_cargo_watch_installed().await {
42        install_cargo_watch().await?;
43    }
44    let mut cmd: Command = Command::new("cargo-watch");
45    cmd.arg("--clear")
46        .arg("--skip-local-deps")
47        .arg("-q")
48        .arg("-x")
49        .arg("run");
50    cmd.stdout(Stdio::inherit()).stderr(Stdio::inherit());
51    let status: ExitStatus = cmd.status().await?;
52    if !status.success() {
53        return Err(std::io::Error::other("cargo-watch failed"));
54    }
55    Ok(())
56}