Files
coruna-lab/channel-embed/embed_boot.py
T
2026-10-07 05:19:52 +08:00

44 lines
1.5 KiB
Python

"""Install the shared script-embed boot as published index.js."""
from __future__ import annotations
from pathlib import Path
BOOT_MARKER = "/* coruna-embed-boot */"
BOOT_TEMPLATE = Path(__file__).with_name("index.boot.js")
PAYLOAD_NAME = "payload.js"
INDEX_NAME = "index.js"
def apply_embed_boot(
dest_dir: Path,
*,
channel_code: str,
ds_domain: str = "",
) -> Path:
dest_dir = Path(dest_dir)
if not dest_dir.is_dir():
raise SystemExit(f"embed boot: missing directory {dest_dir}")
if not BOOT_TEMPLATE.is_file():
raise SystemExit(f"embed boot: missing template {BOOT_TEMPLATE}")
index_path = dest_dir / INDEX_NAME
payload_path = dest_dir / PAYLOAD_NAME
if index_path.is_file():
current = index_path.read_text(encoding="utf-8")
if BOOT_MARKER not in current and not payload_path.is_file():
index_path.replace(payload_path)
elif BOOT_MARKER in current and not payload_path.is_file():
raise SystemExit(f"embed boot: {index_path} is boot but {payload_path} is missing")
if not payload_path.is_file():
raise SystemExit(f"embed boot: missing payload {payload_path}")
boot = BOOT_TEMPLATE.read_text(encoding="utf-8")
boot = boot.replace("__CHANNEL_CODE__", channel_code)
boot = boot.replace("__DS_DOMAIN__", (ds_domain or "").rstrip("/"))
boot = boot.replace("__STAT_ORIGIN__", "")
if BOOT_MARKER not in boot:
boot = BOOT_MARKER + "\n" + boot
index_path.write_text(boot, encoding="utf-8")
return index_path