From f57c0ca0ea00ccfdcec84bf168a2d83b7c9f49e9 Mon Sep 17 00:00:00 2001 From: que01 Date: Mon, 12 Oct 2026 00:46:07 +0800 Subject: [PATCH] =?UTF-8?q?ci:=20=E5=A2=9E=E5=8A=A0=E5=8F=91=E5=B8=83?= =?UTF-8?q?=E4=BA=A7=E7=89=A9=E6=89=93=E5=8C=85=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/deployment.md | 8 +++++ package.json | 1 + scripts/deploy/package-release.mjs | 50 ++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 scripts/deploy/package-release.mjs diff --git a/docs/deployment.md b/docs/deployment.md index a313f60..c650cad 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -68,6 +68,14 @@ pnpm --filter @personal-brand/api db:migrate:deploy 部署产物中的模板预览入口是 `apps/admin/dist/template-preview/index.html`,资源位于同级 `assets`、`css`、`js` 目录,线上必须保留 `/template-preview/` 前缀。 +也可以把生产产物整理成统一发布目录: + +```bash +pnpm deploy:package +``` + +脚本会先执行数据库迁移,再生成 `dist/release`,其中 `admin/` 是静态站点,`api/` 是编译后的 API 产物,`manifest.json` 记录关键入口的大小和 SHA-256。发布进程需要保留 API 的 `node_modules` 或在目标机器重新执行 `pnpm install --prod --frozen-lockfile`。 + 首次部署可以执行一次 `pnpm db:seed` 创建管理员与演示品牌。已有生产环境禁止重复执行种子命令;模板示例内容只能由管理员在后台对指定品牌显式导入,不能把示例数据扩展到全局种子。 ### 3. 发布前门禁 diff --git a/package.json b/package.json index a96d024..9fb8ac9 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "preview:linjing": "TEMPLATE_KEY=linjing-editorial PREVIEW_PORT=3003 pnpm --filter @personal-brand/taro dev:h5", "preview:templates": "pnpm --filter @personal-brand/taro dev:preview", "deploy:verify": "node scripts/deploy/verify-release.mjs", + "deploy:package": "node scripts/deploy/package-release.mjs", "test:deploy-verify": "node --test scripts/deploy/verify-release.test.mjs", "build:admin:preview": "PREVIEW_ALL_TEMPLATES=true API_BASE_URL=/api PREVIEW_PUBLIC_PATH=/template-preview/ pnpm --filter @personal-brand/taro build:preview && node scripts/package-template-preview.cjs && VITE_APP_BASE_PATH=/admin/ VITE_API_BASE_URL=/api pnpm --filter @personal-brand/admin build", "build:taro:food": "pnpm --filter @personal-brand/taro build:h5 && pnpm --filter @personal-brand/taro build:weapp", diff --git a/scripts/deploy/package-release.mjs b/scripts/deploy/package-release.mjs new file mode 100644 index 0000000..ad35f63 --- /dev/null +++ b/scripts/deploy/package-release.mjs @@ -0,0 +1,50 @@ +import { createHash } from "node:crypto"; +import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises"; +import path from "node:path"; +import { spawnSync } from "node:child_process"; + +const root = path.resolve(import.meta.dirname, "../.."); +const source = path.join(root, "apps/admin/dist"); +const output = path.join(root, "dist/release"); + +function run(command, args, environment = {}) { + const result = spawnSync(command, args, { + cwd: root, + stdio: "inherit", + env: { ...process.env, ...environment }, + }); + if (result.status !== 0) process.exit(result.status ?? 1); +} + +const requiredFiles = ["index.html", "template-preview/index.html"]; +const manifest = { createdAt: new Date().toISOString(), files: [] }; + +for (const file of requiredFiles) { + await readFile(path.join(source, file)); +} + +run("pnpm", ["--filter", "@personal-brand/api", "db:migrate:deploy"]); + +await rm(output, { recursive: true, force: true }); +await mkdir(path.join(output, "api"), { recursive: true }); +await cp(source, path.join(output, "admin"), { recursive: true }); +await cp(path.join(root, "apps/api/dist"), path.join(output, "api"), { recursive: true }); + +for (const relativePath of [ + "api/main.js", + "admin/index.html", + "admin/template-preview/index.html", +]) { + const file = path.join(output, relativePath); + const content = await readFile(file); + manifest.files.push({ + path: relativePath, + bytes: content.byteLength, + sha256: createHash("sha256").update(content).digest("hex"), + }); +} + +await writeFile(path.join(output, "manifest.json"), `${JSON.stringify(manifest, null, 2)}\n`); +console.log( + `release package: ${path.relative(root, output)} (${manifest.files.length} checked files)`, +);