auric 0.1.0

CLI for the `auric` MVC SPA framework
use clap::Parser;
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};

#[derive(Debug, Parser)]
pub struct Opts {
    /// Port to listen on
    #[arg(long = "port")]
    pub port: Option<u16>,

    /// Open a browser tab once the initial build is complete
    #[arg(long = "open", default_value_t = false)]
    pub open: bool,
}

pub async fn run(port: Option<u16>, open: bool) -> anyhow::Result<()> {
    let port = port.unwrap_or(4200);

    // trunk serve
    let mut command = format!("==> trunk serve --port {port}");
    let mut args = vec!["--port".to_string(), format!("{port}")];
    if open {
        command = format!("{command} --open");
        args.push("--open".to_string());
    }
    eprintln!("{command}");
    let mut cmd = Command::new("trunk");
    let child = cmd.arg("serve").args(args.as_slice()).stdout(Stdio::piped()).spawn()?;
    let stdout = child.stdout.unwrap();
    let reader = BufReader::new(stdout);
    for res in reader.lines() {
        match res {
            Ok(line) => {
                eprintln!("{line}");
            }
            Err(_) => break,
        }
    }

    Ok(())
}