Files
tdc-rs/scripts/build-frontend.sh
T
adminandClaude Sonnet 5.5 e72345a958 fix(console): schema DDL 移出 web root,兜底行为抽进 lib 并锁进 CI
- static/migrate.sql ⇒ schema/migrate.sql(编译期内嵌不变,运行期不在 web root)
  ⇒ 大小写 / %2E / 尾斜杠 / 双斜杠等路径变体全部拿不到 DDL
- 404 与 SPA 兜底从二进制 crate 抽进 lib(src/routes/fallback.rs)
  ⇒ /api 与 /api/ 统一回 404 JSON,SPA 深链回 index.html
- 新增 tests/spa_fallback.rs:6 条断言锁住兜底行为
- README 整段重写为现行 18 实体 CRUD + 鉴权面 + 上游 SPA 托管
- build-frontend.sh 可执行;产物目录缺失时回落 npm 构建路径
- registry::validate() 挪到 PGlite::open 之前,注册表写坏即秒退
- mw.rs 注释点明两套 4xx 形状并存是刻意分歧

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-10-25 05:58:20 +08:00

77 lines
3.2 KiB
Bash
Executable File
Raw 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 bash
# 从上游仓库构建「完整版前端」并落到本仓 static/。
#
# 用法:
# UPSTREAM_REPO=/path/to/ssw-tdc ./scripts/build-frontend.sh
#
# ⚠️ 只写 static/index.html、static/assets/、static/logo.png ——
# **绝不碰 schema/migrate.sql**(编译期内嵌的 schema 唯一来源,
# 且**刻意不在 web root** —— 放进 static/ 就等于挂到 HTTP 上)。
# ⚠️ 旧产物按约束 13 挪进访达回收站(不 rm)。
#
# 🔴 **免 npm 模式**(默认走它):上游前端已经在上游装好了依赖,
# 且本机对「从外部拷贝来的代码跑 npm install」有权限闸门 ⇒ 允许直接用
# **已构建好的 dist** 落盘(`PREBUILT_DIST` 指向该目录)。
# `PREBUILT_DIST` 不存在时**自动回落**到 `SKIP_NPM=0`(跑 npm ci + vite build)。
#
# 环境变量:
# UPSTREAM_REPO 上游仓库根目录(默认 /tmp/sswtdc-probe)
# PREBUILT_DIST 已构建好的 dist 目录(默认 <repo>/.superpowers/sdd/.../spa-dist)
# SKIP_NPM 1 = 优先直接搬运 PREBUILT_DIST(默认);0 = 强制走 npm 构建
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
UPSTREAM_REPO="${UPSTREAM_REPO:-/tmp/sswtdc-probe}"
PREBUILT_DIST="${PREBUILT_DIST:-${ROOT}/.superpowers/sdd/2026-10-24-tdc-rs-align-v2-p0-p1/spa-dist}"
SKIP_NPM="${SKIP_NPM:-1}"
WORK="$(mktemp -d)"
STAMP="$(date +%Y%m%d-%H%M%S).$$"
# 「免 npm」模式下,产物目录不存在 ⇒ 回落去跑 npm 构建(而不是直接 exit 1)。
if [ "${SKIP_NPM}" = "1" ] && [ ! -d "${PREBUILT_DIST}" ]; then
echo "[build-frontend] 已构建产物不存在:${PREBUILT_DIST} ⇒ 回落到 npm 构建路径"
SKIP_NPM=0
fi
if [ "${SKIP_NPM}" = "1" ]; then
DIST="${PREBUILT_DIST}"
echo "[build-frontend] 直接搬运已构建产物:${DIST}"
else
if [ ! -d "${UPSTREAM_REPO}/frontend/web" ]; then
echo "上游前端不存在:${UPSTREAM_REPO}/frontend/web" >&2
exit 1
fi
cp -R "${UPSTREAM_REPO}/frontend/web/." "${WORK}/"
# 🔴 排除依赖树与上次构建残留(否则会拖进几 GB 的 node_modules)
rm -rf "${WORK}/node_modules" "${WORK}/dist"
cd "${WORK}"
# 🔴 `npm ci` 只认 package-lock.json(上游两个 lock 文件并存,pnpm-lock.yaml 不要用)
npm ci --no-audit --no-fund
# 🔴 `npx vite build` 而不是 `npm run build`(后者先跑 vue-tsc,上游有 18 个类型错误 ⇒ 必失败)
# 🔴 `VITE_SSW_APP=tdc` 是必需的(不设则前端走平台路由 /projects)
VITE_SSW_APP=tdc npx vite build
DIST="${WORK}/dist"
fi
if [ ! -f "${DIST}/index.html" ]; then
echo "产物里没有 index.html:${DIST}" >&2
exit 1
fi
# 旧产物挪进访达回收站(**不 rm**)
if [ -e "${ROOT}/static/index.html" ]; then
mv "${ROOT}/static/index.html" "${HOME}/.Trash/tdc-rs-static-index.html.${STAMP}"
fi
if [ -e "${ROOT}/static/logo.png" ]; then
mv "${ROOT}/static/logo.png" "${HOME}/.Trash/tdc-rs-static-logo.png.${STAMP}"
fi
if [ -d "${ROOT}/static/assets" ]; then
mv "${ROOT}/static/assets" "${HOME}/.Trash/tdc-rs-static-assets.${STAMP}"
fi
mkdir -p "${ROOT}/static"
cp -R "${DIST}/." "${ROOT}/static/"
echo "[build-frontend] 前端产物已落到 ${ROOT}/static"