#!/usr/bin/env python3 """Assemble xhs/release — 单一门户包(首页+导航+小册+社群+VIP),对齐微信小程序。""" from __future__ import annotations import shutil import sys from pathlib import Path from subprocess import check_call ROOT = Path(__file__).resolve().parents[1] SRC = ROOT / "tool" OUT = ROOT / "release" def main() -> int: # 数据由 build:xhs 链路预先写入(export → pack-hackrobot-book);此处不再 sync 覆盖 if not (SRC / "portal-data.js").is_file(): check_call([sys.executable, str(ROOT / "scripts" / "sync_data.py")]) if OUT.exists(): shutil.rmtree(OUT, ignore_errors=True) OUT.mkdir(parents=True) # 微信同源样式:hub 首页 + nav + hero 封面 + VIP landing + 公众号弹窗 css_parts = [] for name in ( "hub.css", "styles.css", "community-navbar.css", "weapp-hero.css", "reader-md.css", "ebook-book.css", "vip-landing.css", "woa-modal.css", "vip-offline.css", ): p = SRC / name if p.is_file(): css_parts.append(f"/* ===== {name} ===== */\n" + p.read_text(encoding="utf-8")) if not css_parts: print("FATAL missing styles", file=sys.stderr) return 1 data_js = (SRC / "portal-data.js").read_text(encoding="utf-8") # 兜底:微信分包图路径 → 离线 ./images/(export/pack 漏改时不白屏) data_js = data_js.replace("/packageBook/assets/book-images/", "./images/").replace( "/assets/book-images/", "./images/" ) md_js = (SRC / "book" / "markdown.js").read_text(encoding="utf-8") app_js = (SRC / "app.js").read_text(encoding="utf-8") bundle = ( "/* nomadro xhs portal — same modules as weapp: home/nav/book/community/vip */\n" + data_js + "\n" + md_js + "\n" + app_js + "\n" ) shutil.copy2(SRC / "index.html", OUT / "index.html") (OUT / "styles.css").write_text("\n\n".join(css_parts) + "\n", encoding="utf-8") (OUT / "app.js").write_text(bundle, encoding="utf-8") icons_src = SRC / "icons" if icons_src.is_dir(): shutil.copytree(icons_src, OUT / "icons") # 电子书离线配图(体积预算内 → tool/book/images) book_imgs = SRC / "book" / "images" if book_imgs.is_dir(): dest = OUT / "images" if dest.exists(): shutil.rmtree(dest) shutil.copytree(book_imgs, dest) nested = [p for p in OUT.rglob("index.html") if p != OUT / "index.html"] if nested: print("FATAL nested index.html:", nested, file=sys.stderr) return 1 if "PORTAL_DATA" not in bundle: print("FATAL missing PORTAL_DATA", file=sys.stderr) return 1 if "MarkdownLite" not in bundle: print("FATAL missing MarkdownLite (book/markdown.js from ebook/redmini)", file=sys.stderr) return 1 print(f"assembled portal {OUT}") total = 0 for p in sorted(OUT.rglob("*")): if p.is_file(): total += p.stat().st_size print(f" {p.relative_to(OUT).as_posix()}\t{p.stat().st_size}") print(f"total {total / 1024:.1f} KB") return 0 if __name__ == "__main__": raise SystemExit(main())