use std::path::Path;
pub fn build(manifest_dir: &str) {
let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR not set");
let output = Path::new(&out_dir).join("index.html");
let prebuilt = Path::new(manifest_dir).join("prebuilt.html");
let client_dir = Path::new(manifest_dir).join("client");
if client_dir.exists() {
emit_rerun_triggers(&client_dir);
if run_pnpm_build(manifest_dir) {
let dist_index = client_dir.join("dist").join("index.html");
if dist_index.exists() {
std::fs::copy(&dist_index, &output).expect("failed to copy client dist/index.html");
std::fs::copy(&output, &prebuilt).ok();
return;
}
}
}
if prebuilt.exists() {
std::fs::copy(&prebuilt, &output).expect("failed to copy prebuilt client UI");
return;
}
println!("cargo:warning=pnpm not found and no prebuilt UI; showing placeholder");
std::fs::write(&output, PLACEHOLDER_HTML).expect("failed to write placeholder client HTML");
}
fn emit_rerun_triggers(client_dir: &Path) {
println!(
"cargo:rerun-if-changed={}",
client_dir.join("src").display()
);
println!(
"cargo:rerun-if-changed={}",
client_dir.join("index.html").display()
);
println!(
"cargo:rerun-if-changed={}",
client_dir.join("vite.config.ts").display()
);
println!(
"cargo:rerun-if-changed={}",
client_dir.join("package.json").display()
);
}
fn run_pnpm_build(manifest_dir: &str) -> bool {
match std::process::Command::new("pnpm")
.args(["--filter", "client", "build"])
.current_dir(manifest_dir)
.status()
{
Ok(status) if status.success() => true,
Ok(_) => {
println!("cargo:warning=pnpm build exited with non-zero status");
false
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
println!(
"cargo:warning=pnpm not found; React client not built \
(install from https://pnpm.io/installation, then `pnpm install`)"
);
false
}
Err(err) => {
println!("cargo:warning=failed to launch pnpm: {err}");
false
}
}
}
const PLACEHOLDER_HTML: &str = r#"<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>MOADIM</title></head>
<body><p>Client not built. Install pnpm (https://pnpm.io/installation), run `pnpm install` at the repo root, and rebuild from source, or reinstall a release that bundles the prebuilt client.</p></body>
</html>"#;