collet 0.1.0

Relentless agentic coding orchestrator with zero-drop agent loops
Documentation
use std::process::Command;

fn main() {
    // Watch Svelte source — trigger rebuild when any file changes.
    println!("cargo:rerun-if-changed=web/src");
    println!("cargo:rerun-if-changed=web/package.json");
    println!("cargo:rerun-if-changed=web/svelte.config.js");
    println!("cargo:rerun-if-changed=web/vite.config.ts");

    let build_dir = std::path::Path::new("web/build");

    // Skip web build if explicitly opted out (CI, minimal builds).
    if std::env::var("COLLET_SKIP_WEB_BUILD").is_ok() {
        println!("cargo:warning=Skipping web build (COLLET_SKIP_WEB_BUILD set)");
        if !build_dir.exists() {
            std::fs::create_dir_all(build_dir)
                .expect("Failed to create placeholder web/build directory");
        }
        return;
    }

    // Skip if web/src doesn't exist (minimal checkout / crates.io verification).
    if !std::path::Path::new("web/src").exists() {
        // rust-embed requires the folder to exist at compile time even when empty.
        if !build_dir.exists() {
            std::fs::create_dir_all(build_dir)
                .expect("Failed to create placeholder web/build directory");
        }
        return;
    }

    // Skip npm build when pre-built assets are already present.
    // This allows `cargo install` users to skip the Node.js requirement:
    // the published crate includes web/build/** so they never need npm.
    // Local developers can force a rebuild with: COLLET_FORCE_WEB_BUILD=1
    let has_prebuilt = build_dir.join("index.html").exists();
    if has_prebuilt && std::env::var("COLLET_FORCE_WEB_BUILD").is_err() {
        println!(
            "cargo:warning=Using pre-built web assets (set COLLET_FORCE_WEB_BUILD=1 to rebuild)"
        );
        return;
    }

    let npm = if cfg!(target_os = "windows") {
        "npm.cmd"
    } else {
        "npm"
    };

    // Ensure node_modules are installed.
    if !std::path::Path::new("web/node_modules").exists() {
        println!("cargo:warning=Installing web dependencies...");
        let status = Command::new(npm)
            .args(["install"])
            .current_dir("web")
            .status()
            .expect("npm install failed — is Node.js installed?");
        if !status.success() {
            panic!("npm install failed");
        }
    }

    println!("cargo:warning=Building web assets...");
    let status = Command::new(npm)
        .args(["run", "build"])
        .current_dir("web")
        .status()
        .expect("npm run build failed — is Node.js installed?");
    if !status.success() {
        panic!("npm run build failed");
    }
}