#!/usr/bin/env node import { isDeepStrictEqual } from "node:util"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { createRequire } from "node:module"; const CONTRACT_KEYS = new Set([ "templateDefinition", "visualTemplateDefinition", "visualTemplateManifest", "templatePackageManifest", "installation", "templateKey", ]); const VOLATILE_KEYS = new Set(["generatedAt"]); /** * 只允许自动重发“模板契约字段变化、品牌内容未变”的发布快照。 * 一旦草稿内容与已发布快照不同,必须由管理员显式审查后发布,避免误发草稿。 */ export function classifyContractRefresh(published, current) { if (!published || !current) throw new Error("发布快照不能为空"); const keys = new Set([...Object.keys(published), ...Object.keys(current)]); const changedKeys = [...keys] .filter((key) => !VOLATILE_KEYS.has(key)) .filter((key) => !isDeepStrictEqual(published[key], current[key])); if (changedKeys.length === 0) return { status: "current", changedKeys, blockingKeys: [] }; const blockingKeys = changedKeys.filter((key) => !CONTRACT_KEYS.has(key)); return { status: blockingKeys.length ? "content-diverged" : "contract-stale", changedKeys, blockingKeys, }; } async function loadServices() { const apiRequire = createRequire(new URL("../apps/api/package.json", import.meta.url)); const { PrismaService } = await import("../apps/api/dist/prisma.service.js"); const { BrandReleaseService } = await import("../apps/api/dist/brand-release.service.js"); const { BrandReleaseApiService } = await import("../apps/api/dist/brand-release-api.service.js"); // 触发运行时依赖解析,避免发布脚本在错误的工作目录下才失败。 apiRequire("@prisma/client"); const prisma = new PrismaService(); const snapshots = new BrandReleaseService(prisma); return { prisma, snapshots, releases: new BrandReleaseApiService(prisma, snapshots), }; } async function main() { const apply = process.argv.includes("--apply"); const actorUsername = process.env.TEMPLATE_CATALOG_ACTOR ?? "admin"; const note = process.env.TEMPLATE_CONTRACT_REPUBLISH_NOTE ?? "模板契约迁移:刷新已发布页面方案兼容性"; const { prisma, snapshots, releases } = await loadServices(); try { const actor = await prisma.adminUser.findFirst({ where: { username: actorUsername, isEnabled: true }, select: { id: true }, }); if (!actor) throw new Error(`找不到启用的模板迁移操作人:${actorUsername}`); const brands = await prisma.brandProfile.findMany({ select: { id: true, slug: true }, orderBy: { slug: "asc" }, }); const results = []; for (const brand of brands) { const release = await prisma.brandRelease.findFirst({ where: { brandId: brand.id, status: "published" }, orderBy: { version: "desc" }, select: { version: true, snapshot: true }, }); if (!release) { results.push({ slug: brand.slug, status: "unpublished" }); continue; } const current = await snapshots.buildSnapshot(brand.id); const classification = classifyContractRefresh(release.snapshot, current); const result = { slug: brand.slug, releaseVersion: release.version, ...classification, }; results.push(result); if (!apply || classification.status !== "contract-stale") continue; const readiness = await releases.readiness(brand.id); if (!readiness.ready) { result.status = "not-ready"; result.blockingKeys = readiness.checks .filter((check) => !check.passed) .map((check) => check.key); continue; } const published = await releases.publish(brand.id, actor.id, note); result.status = "republished"; result.newReleaseVersion = published.version; } console.log(JSON.stringify({ apply, actor: actorUsername, results }, null, 2)); if (apply && results.some((item) => ["content-diverged", "not-ready"].includes(item.status))) process.exitCode = 1; } finally { await prisma.$disconnect(); } } if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { main().catch((error) => { console.error(error instanceof Error ? error.message : String(error)); process.exitCode = 1; }); }