use std::process::Command;
fn main() {
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");
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;
}
if !std::path::Path::new("web/src").exists() {
if !build_dir.exists() {
std::fs::create_dir_all(build_dir)
.expect("Failed to create placeholder web/build directory");
}
return;
}
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"
};
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");
}
}