Skip to main content

hyprstream_ts_codegen/
main.rs

1//! TypeScript codegen binary for hyprstream RPC schemas.
2//!
3//! Reads CGR (CodeGeneratorRequest) files and generates TypeScript clients
4//! with Cap'n Proto wire format serialization.
5//!
6//! Usage:
7//!   hyprstream-ts-codegen --input-dir codegen-out --output-dir src/rpc/generated/
8
9#![allow(clippy::expect_used, clippy::print_stdout, clippy::print_stderr)]
10
11use std::path::PathBuf;
12
13use clap::Parser;
14
15mod py_codegen;
16mod ts_codegen;
17
18#[derive(Parser)]
19#[command(name = "hyprstream-ts-codegen")]
20#[command(about = "Generate TypeScript clients from Cap'n Proto CGR files")]
21struct Cli {
22    /// Directory containing .cgr files
23    #[arg(long, default_value = "codegen-out")]
24    input_dir: PathBuf,
25
26    /// Output directory for generated TypeScript files
27    #[arg(long, default_value = "src/rpc/generated")]
28    output_dir: PathBuf,
29}
30
31fn main() {
32    let cli = Cli::parse();
33
34    if !cli.input_dir.exists() {
35        eprintln!(
36            "Error: Input directory '{}' does not exist.\n\
37             Run `cargo build -p hyprstream` first to generate CGR files.",
38            cli.input_dir.display()
39        );
40        std::process::exit(1);
41    }
42
43    // Collect all .cgr files
44    let mut cgr_files: Vec<(String, PathBuf)> = Vec::new();
45    let entries = std::fs::read_dir(&cli.input_dir).expect("Failed to read input directory");
46    for entry in entries {
47        let entry = entry.expect("Failed to read directory entry");
48        let path = entry.path();
49        if path.extension().is_some_and(|ext| ext == "cgr") {
50            let name = path
51                .file_stem()
52                .expect("CGR file has no stem")
53                .to_str()
54                .expect("CGR file name is not UTF-8")
55                .to_owned();
56            cgr_files.push((name, path));
57        }
58    }
59
60    if cgr_files.is_empty() {
61        eprintln!(
62            "No .cgr files found in '{}'.\n\
63             Run `cargo build -p hyprstream` first to generate CGR files.",
64            cli.input_dir.display()
65        );
66        std::process::exit(1);
67    }
68
69    cgr_files.sort_by(|a, b| a.0.cmp(&b.0));
70
71    // Parse all schemas
72    let mut schemas = Vec::new();
73    let mut parse_errors = Vec::new();
74    for (name, path) in &cgr_files {
75        match hyprstream_rpc_build::schema::cgr_reader::parse_from_cgr_path(path, name) {
76            Ok(schema) => {
77                println!(
78                    "  Parsed {name}: {} request variants, {} response variants, {} structs",
79                    schema.request_variants.len(),
80                    schema.response_variants.len(),
81                    schema.structs.len(),
82                );
83                schemas.push((name.clone(), schema));
84            }
85            Err(e) => {
86                eprintln!("  Error: Failed to parse {name}.cgr: {e}");
87                parse_errors.push(format!("{name}.cgr: {e}"));
88            }
89        }
90    }
91
92    if !parse_errors.is_empty() {
93        eprintln!(
94            "Refusing to generate partial TypeScript output; {} schema(s) failed to parse:",
95            parse_errors.len()
96        );
97        for error in parse_errors {
98            eprintln!("  {error}");
99        }
100        std::process::exit(1);
101    }
102
103    // Create output directory
104    std::fs::create_dir_all(&cli.output_dir).expect("Failed to create output directory");
105
106    // Generate TypeScript
107    ts_codegen::generate_all(&ts_codegen::TypeScriptBackend, &schemas, &cli.output_dir);
108
109    println!(
110        "\nGenerated {} service files in {}",
111        schemas.len(),
112        cli.output_dir.display()
113    );
114}