fix: 增加模板契约发布快照重发工具
This commit is contained in:
1 parent
ea81c754ed
commit
193858c727
2 files changed
+165
No files matched your search
@@ -0,0 +1,114 @@
|
||||
#!/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;
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { classifyContractRefresh } from "./republish-stale-template-contracts.mjs";
|
||||
|
||||
function snapshot(overrides = {}) {
|
||||
return {
|
||||
generatedAt: "2026-10-20T00:00:00.000Z",
|
||||
templateKey: "doors",
|
||||
templateDefinition: { key: "doors" },
|
||||
visualTemplateDefinition: { supportedIndustryTemplates: ["personal", "doors"] },
|
||||
visualTemplateManifest: { pages: [] },
|
||||
templatePackageManifest: { supportedIndustryTemplates: ["personal", "doors"] },
|
||||
installation: { templateVersion: "1.0.6" },
|
||||
brand: { name: "林境门作" },
|
||||
offerings: [{ name: "入户门" }],
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
test("detects a contract-only stale publication", () => {
|
||||
const current = snapshot({
|
||||
generatedAt: "2026-10-20T01:00:00.000Z",
|
||||
visualTemplateDefinition: { supportedIndustryTemplates: ["doors"] },
|
||||
templatePackageManifest: { supportedIndustryTemplates: ["doors"] },
|
||||
installation: { templateVersion: "1.0.7" },
|
||||
});
|
||||
assert.deepEqual(classifyContractRefresh(snapshot(), current), {
|
||||
status: "contract-stale",
|
||||
changedKeys: ["visualTemplateDefinition", "templatePackageManifest", "installation"],
|
||||
blockingKeys: [],
|
||||
});
|
||||
});
|
||||
|
||||
test("ignores generated timestamps when the publication is current", () => {
|
||||
assert.deepEqual(
|
||||
classifyContractRefresh(snapshot(), snapshot({ generatedAt: "2026-10-21T00:00:00.000Z" })),
|
||||
{ status: "current", changedKeys: [], blockingKeys: [] },
|
||||
);
|
||||
});
|
||||
|
||||
test("blocks automatic publication when draft content has diverged", () => {
|
||||
const current = snapshot({
|
||||
installation: { templateVersion: "1.0.7" },
|
||||
brand: { name: "尚未发布的品牌名" },
|
||||
});
|
||||
assert.deepEqual(classifyContractRefresh(snapshot(), current), {
|
||||
status: "content-diverged",
|
||||
changedKeys: ["installation", "brand"],
|
||||
blockingKeys: ["brand"],
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user