#!/usr/bin/env python3 """Sync H5 links.json + icons into weapp/ for WeChat DevTools.""" from __future__ import annotations import json import shutil import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] # weapp/ PROJ = ROOT.parent # navmini/ SRC_JSON = PROJ / "src" / "data" / "links.json" SRC_ICONS = PROJ / "public" / "icons" OUT_JSON = ROOT / "data" / "links.json" OUT_ICONS = ROOT / "icons" def main() -> int: if not SRC_JSON.is_file(): print(f"missing {SRC_JSON}", file=sys.stderr) return 1 data = json.loads(SRC_JSON.read_text(encoding="utf-8")) OUT_ICONS.mkdir(parents=True, exist_ok=True) OUT_JSON.parent.mkdir(parents=True, exist_ok=True) for old in OUT_ICONS.glob("*"): if old.is_file(): old.unlink() needed: list[str] = ["_brand"] for cat in data.get("categories", []): for group in cat.get("groups", []): for link in group.get("links", []): lid = link["id"] needed.append(lid) # 小程序本地路径(相对小程序根目录) link["icon"] = f"/icons/{lid}.webp" missing = [] for lid in needed: src = SRC_ICONS / f"{lid}.webp" dst = OUT_ICONS / f"{lid}.webp" if not src.is_file(): missing.append(lid) continue shutil.copy2(src, dst) if missing: print("warn missing icons:", ", ".join(missing), file=sys.stderr) data["brandIcon"] = "/icons/_brand.webp" OUT_JSON.write_text( json.dumps(data, ensure_ascii=False, indent=2) + "\n", encoding="utf-8", ) icon_count = len(list(OUT_ICONS.glob("*.webp"))) print(f"synced data/links.json + {icon_count} icons -> {ROOT}") return 0 if __name__ == "__main__": raise SystemExit(main())