use std::path::Path; use std::process::Command; fn run_npm(args: &[&str], web_dir: &Path) { let is_windows = cfg!(target_os = "windows"); if is_windows { let mut cmd_args = vec!["/c", "npm"]; cmd_args.extend_from_slice(args); let status = Command::new("cmd") .args(&cmd_args) .current_dir(web_dir) .status() .unwrap_or_else(|e| panic!("failed to spawn npm {}: {}", args.join(" "), e)); if !status.success() { panic!("npm {} failed with status {}", args.join(" "), status); } } else { let status = Command::new("npm") .args(args) .current_dir(web_dir) .status() .unwrap_or_else(|e| panic!("failed to spawn npm {}: {}", args.join(" "), e)); if !status.success() { panic!("npm {} failed with status {}", args.join(" "), status); } } } fn main() { println!("cargo:rerun-if-env-changed=SKIP_FRONTEND_BUILD"); let web_dir = Path::new("web"); if web_dir.exists() { println!("cargo:rerun-if-changed=web/src"); println!("cargo:rerun-if-changed=web/index.html"); println!("cargo:rerun-if-changed=web/package.json"); println!("cargo:rerun-if-changed=web/vite.config.ts"); println!("cargo:rerun-if-changed=web/tailwind.config.js"); println!("cargo:rerun-if-changed=web/postcss.config.js"); println!("cargo:rerun-if-changed=web/tsconfig.json"); } if std::env::var("SKIP_FRONTEND_BUILD").is_ok() { println!("cargo:warning=SKIP_FRONTEND_BUILD is set, skipping frontend build"); return; } if !web_dir.exists() { println!("cargo:warning=web/ directory not found, skipping frontend build"); return; } println!("cargo:warning=building frontend (npm install)..."); run_npm(&["install"], web_dir); println!("cargo:warning=building frontend (npm run build)..."); run_npm(&["run", "build"], web_dir); println!("cargo:warning=frontend build complete, output in static/"); }