Generate types script

This commit is contained in:
Louis Knight-Webb
2025-06-20 20:02:55 +01:00
parent 708bb9ad5b
commit ffeac255b1

View File

@@ -1,17 +1,17 @@
use std::{env, fs, path::Path};
use ts_rs::TS; // make sure this is in [build-dependencies]
use ts_rs::TS; // in [build-dependencies]
fn main() {
// Where the combined types.ts will live
// 1. Make sure ../shared exists
let shared_path = Path::new("../shared");
fs::create_dir_all(shared_path).expect("cannot create ../shared");
println!("Generating TypeScript types…");
// Tell ts-rs where to drop its per-type files (well still roll our own big one)
// 2. Let ts-rs write its per-type files here (handy for debugging)
env::set_var("TS_RS_EXPORT_DIR", shared_path.to_str().unwrap());
// Collect every declaration at *runtime* (so no const-eval issues)
// 3. Grab every Rust type you want on the TS side
let decls = [
vibe_kanban::models::ApiResponse::<()>::decl(),
vibe_kanban::models::config::Config::decl(),
@@ -43,16 +43,29 @@ fn main() {
vibe_kanban::models::task_attempt::BranchStatus::decl(),
];
// Header banner
// 4. Friendly banner
const HEADER: &str =
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs).\n\
// Do not edit this file manually.\n\
// Auto-generated from Rust backend types using ts-rs\n\n";
// Smash it all together
let consolidated = format!("{HEADER}{}", decls.join("\n\n"));
// 5. Add `export` if its missing, then join
let body = decls
.into_iter()
.map(|d| {
let trimmed = d.trim_start();
if trimmed.starts_with("export") {
d
} else {
format!("export {trimmed}")
}
})
.collect::<Vec<_>>()
.join("\n\n");
fs::write(shared_path.join("types.ts"), consolidated).expect("unable to write types.ts");
// 6. Write the consolidated types.ts
fs::write(shared_path.join("types.ts"), format!("{HEADER}{body}"))
.expect("unable to write types.ts");
println!("✅ TypeScript types generated in ../shared/");
}