Files
dcalc/design/src-web/sync-components.py
T
2026-08-04 17:57:01 +08:00

29 lines
971 B
Python

from __future__ import annotations
import json
import re
from pathlib import Path
ROOT = Path(__file__).resolve().parent
TEMPLATE = re.compile(r'<template\s+id="([^"]+)">\s*(.*?)\s*</template>', 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")