#!/usr/bin/env python3 """ Generate TypeScript constants from audio_splitter/defaults.py. """ import importlib.util import json import sys from pathlib import Path # Get the directory where this script is located SCRIPT_DIR = Path(__file__).parent.absolute() # The defaults.py is in the same directory as the script DEFAULTS_PATH = SCRIPT_DIR / "defaults.py" # The frontend source is at SCRIPT_DIR.parent (i.e., /app) # The generated file should be at /app/src/constants/generated.ts FRONTEND_ROOT = SCRIPT_DIR.parent OUTPUT_PATH = FRONTEND_ROOT / "src" / "constants" / "generated.ts" def load_defaults_module(): spec = importlib.util.spec_from_file_location("defaults", DEFAULTS_PATH) if spec is None: raise RuntimeError(f"Could not load spec for {DEFAULTS_PATH}") module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module def format_value(value): """Convert Python value to TypeScript literal.""" if value is None: return "null" if isinstance(value, bool): return str(value).lower() if isinstance(value, str): # Use json.dumps to produce a properly escaped string literal return json.dumps(value) if isinstance(value, (int, float)): return str(value) if isinstance(value, list): return f"[{', '.join(format_value(v) for v in value)}]" return repr(value) def main(): print(f"Generating TypeScript defaults from {DEFAULTS_PATH}") defaults = load_defaults_module() constants = {name: value for name, value in vars(defaults).items() if name.startswith("DEFAULT_")} if not constants: print("No DEFAULT_* constants found.") sys.exit(1) lines = [ "// ============================================================================", "// GENERATED FILE – DO NOT EDIT MANUALLY.", "// This file is generated from audio_splitter/defaults.py.", "// Run `npm run generate` or `python scripts/generate_ts_defaults.py` to update.", "// ============================================================================", "", ] for name, value in sorted(constants.items()): ts_value = format_value(value) lines.append(f"export const {name} = {ts_value};") OUTPUT_PATH.parent.mkdir(parents=True, exist_ok=True) OUTPUT_PATH.write_text("\n".join(lines) + "\n") print(f"✅ Generated {OUTPUT_PATH}") print(f" {len(constants)} constants exported.") if __name__ == "__main__": main()