use std::{error::Error, path::{Path, PathBuf}, process::Command};
fn main() -> Result<(), Box<dyn Error>> {
println!("cargo:rerun-if-changed=src/browser.ts");
let manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let infile = manifest_dir.join("src").join("browser.ts");
let outfile = manifest_dir.join("src").join("generated").join("browser.js");
let need_compiling = !outfile.exists()
|| infile.metadata()?.modified()? > outfile.metadata()?.modified()?;
if !need_compiling {
return Ok(());
}
let local_tsc = manifest_dir
.join("node_modules")
.join("typescript")
.join("bin")
.join("tsc");
let tsc = if local_tsc.exists() {
&local_tsc
} else {
Path::new("tsc")
};
let status = Command::new(tsc)
.current_dir(&manifest_dir)
.arg("--pretty")
.status();
match status {
Err(e) => {
eprintln!("Error executing `tsc`.");
if !local_tsc.exists() {
eprintln!("You might need to run `npm install` in the `lib` folder");
}
Err(e)?;
}
Ok(status) if !status.success() => {
Err("`tsc` reported errors.")?;
}
Ok(_) => {}
}
Ok(())
}