Files
operating-room-monitor-server/scripts/bundle_voice_confirmation_single_html.py
2026-05-21 15:48:03 +08:00

47 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python3
"""将 voice-confirmation-web 下的 index.html + voice_app.js 打成单文件 dist/index.html。"""
from __future__ import annotations
import re
import shutil
import stat
from pathlib import Path
def main() -> None:
root = Path(__file__).resolve().parents[1] / "voice-confirmation-web"
index = root / "index.html"
app_js = root / "voice_app.js"
out_dir = root / "dist"
out_file = out_dir / "index.html"
if not index.is_file() or not app_js.is_file():
raise SystemExit(f"missing {index} or {app_js}")
html = index.read_text(encoding="utf-8")
js = app_js.read_text(encoding="utf-8")
# 将外链 defer script 替换为内联(保留其它 script 标签)
pat = re.compile(
r'<script\s+src=["\']voice_app\.js["\']\s+defer\s*>\s*</script>',
re.IGNORECASE,
)
if not pat.search(html):
raise SystemExit("index.html must contain <script src=\"voice_app.js\" defer></script>")
def _repl(_m):
return "<script defer>\n" + js + "\n</script>"
inlined = pat.sub(_repl, html, count=1)
out_dir.mkdir(parents=True, exist_ok=True)
out_file.write_text(inlined, encoding="utf-8")
for name in ("start_http.sh", "start_http.bat"):
src = root / name
dst = out_dir / name
shutil.copy2(src, dst)
sh = out_dir / "start_http.sh"
sh.chmod(sh.stat().st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
print(f"wrote {out_file} (+ start_http.* in dist/)")
if __name__ == "__main__":
main()