nomadro/apps/miniprogram/xhs/scripts/assemble.py
eric 3e5b7c2098 Monorepo super-optimize: src layout, workspace scripts, docs.
Collapse portal under src/, unify pnpm workspace/scripts, thin VIP topic re-exports, drop orphan vip scaffolds and one-shot scripts, ignore xhs/release, document root commands.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-01 00:34:04 +08:00

121 lines
3.8 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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"
)
html = (SRC / "index.html").read_text(encoding="utf-8")
# release 只保留打包后的 styles.css + app.js(tool 可多文件预览)
import re
html = re.sub(
r'(?is)<link\s+rel="stylesheet"[^>]*>\s*',
"",
html,
count=0,
)
html = html.replace(
"</head>",
' <link rel="stylesheet" href="./styles.css" />\n</head>',
1,
)
html = re.sub(r'(?is)<script\s+src="[^"]+"></script>\s*', "", html)
html = html.replace(
"</body>",
' <script src="./app.js"></script>\n</body>',
1,
)
(OUT / "index.html").write_text(html, encoding="utf-8")
(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())