use core::panic;
use std::io::Write;
use std::process::Command;
use std::{env, io};

fn cmd(cmd: &mut Command) {
    let output = cmd.output().unwrap();

    let _ = io::stdout().write_all(&output.stdout);
    if !output.status.success() {
        let _ = io::stderr().write_all(&output.stderr);
        panic!("\nFailed to run command\n");
    }
}

fn main() {
    let out_dir = env::var("OUT_DIR").unwrap();

    cmd(Command::new("bun").args(["install"]));
    cmd(Command::new("bun").args([
        "run",
        "scripts/build.ts",
        "--outdir",
        out_dir.as_str(),
        "--globs",
        "static/**/*.html",
    ]));

    println!("Build completed successfully!");

    println!("cargo::rerun-if-changed=build.rs");
    println!("cargo::rerun-if-changed=package.json");
    println!("cargo::rerun-if-changed=static");
}
