import assert from "node:assert/strict"; import { getTemplatePackageManifestForVisualKey } from "@personal-brand/config"; import { PrismaService } from "../dist/prisma.service.js"; import { TemplateWorkspaceService } from "../dist/template-workspace.service.js"; // 显式创建独立品牌;不注册模板样例,不导入内容,也不发布。 const username = process.argv[2]; if (!username) throw new Error("Usage: node --env-file=.env scripts/create-atelier-brand.mjs "); const prisma = new PrismaService(); try { const admin = await prisma.adminUser.findUniqueOrThrow({ where: { username } }); assert.ok(admin.isEnabled && admin.role === "admin", "An enabled administrator is required"); const slug = "atelier-studio"; const existingBrands = await prisma.brandProfile.findMany({ where: { slug: { not: slug } }, orderBy: { id: "asc" }, }); const brand = await prisma.$transaction(async (tx) => { let current = await tx.brandProfile.findUnique({ where: { slug } }); if (current) { assert.equal(current.templateKey, "personal", "Existing brand has a different industry"); } else { current = await tx.brandProfile.create({ data: { slug, name: "独立工坊 · 品牌小程序", templateKey: "personal", }, }); } await tx.brandMembership.upsert({ where: { adminUserId_brandId: { adminUserId: admin.id, brandId: current.id } }, create: { adminUserId: admin.id, brandId: current.id, role: "owner" }, update: {}, }); return current; }); const manifest = getTemplatePackageManifestForVisualKey("atelier-solopreneur"); if (!manifest) throw new Error("atelier-solopreneur package manifest is unavailable"); const version = await prisma.templatePackageVersion.findUnique({ where: { templatePackageId_version: { templatePackageId: ( await prisma.templatePackage.findUniqueOrThrow({ where: { packageKey: "atelier-solopreneur" }, select: { id: true }, }) ).id, version: manifest.version, }, }, }); if (!version || version.status !== "published") throw new Error(`Published package version not found: atelier-solopreneur@${manifest.version}`); const installation = await prisma.brandTemplateInstallation.findUnique({ where: { brandId: brand.id }, include: { templatePackageVersion: true }, }); if (installation) { assert.equal(installation.templatePackageVersionId, version.id); } else { await new TemplateWorkspaceService(prisma).install(brand.id, "personal", version.id); } assert.deepEqual( await prisma.brandProfile.findMany({ where: { slug: { not: slug } }, orderBy: { id: "asc" } }), existingBrands, ); console.log( JSON.stringify({ slug, name: brand.name, template: "atelier-solopreneur", administrator: username, sampleImported: false, publishedByScript: false, }), ); } finally { await prisma.$disconnect(); }