from __future__ import annotations import json import re from pathlib import Path ROOT = Path(__file__).resolve().parent TEMPLATE = re.compile(r'\s*(.*?)\s*', re.S) for component in sorted(path for path in ROOT.iterdir() if path.is_dir()): source = component / "index.html" if not source.is_file(): continue fragments = dict(TEMPLATE.findall(source.read_text(encoding="utf-8"))) if not fragments: raise RuntimeError(f"No templates found in {source}") key = f"{ROOT.name}/{component.name}/index.html" payload = json.dumps(fragments, ensure_ascii=False, separators=(",", ":")) script = ( "(() => {\n" " window.componentRegistry ||= {};\n" f" window.componentRegistry[{json.dumps(key)}] = {payload};\n" "})();\n" ) (component / "component.js").write_text(script, encoding="utf-8", newline="\n") print(f"Updated {component.name}/component.js")