feat: 完善模板驱动的品牌内容工作台
This commit is contained in:
1 parent
77224ea51f
commit
dd55af8d73
46 files changed
+2204
-295
No files matched your search
@@ -0,0 +1,53 @@
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const crypto = require("node:crypto");
|
||||
|
||||
const root = path.resolve(__dirname, "..");
|
||||
const packageRoot = path.join(root, "packages");
|
||||
const outputPath = path.join(root, "packages/config/src/generated-template-packages.js");
|
||||
const declarationPath = path.join(root, "packages/config/src/generated-template-packages.d.ts");
|
||||
const templates = fs
|
||||
.readdirSync(packageRoot, { withFileTypes: true })
|
||||
.filter(
|
||||
(entry) =>
|
||||
entry.isDirectory() &&
|
||||
entry.name.startsWith("template-") &&
|
||||
fs.existsSync(path.join(packageRoot, entry.name, "template.json")),
|
||||
)
|
||||
.map((entry) => {
|
||||
const manifest = JSON.parse(
|
||||
fs.readFileSync(path.join(packageRoot, entry.name, "template.json"), "utf8"),
|
||||
);
|
||||
return { ...manifest, visualTemplateKey: manifest.packageKey };
|
||||
})
|
||||
.sort((left, right) => left.packageKey.localeCompare(right.packageKey));
|
||||
|
||||
const canonical = (value) => {
|
||||
if (Array.isArray(value)) return value.map(canonical);
|
||||
if (!value || typeof value !== "object") return value;
|
||||
return Object.fromEntries(
|
||||
Object.keys(value)
|
||||
.sort()
|
||||
.map((key) => [key, canonical(value[key])]),
|
||||
);
|
||||
};
|
||||
const fingerprint = crypto
|
||||
.createHash("sha256")
|
||||
.update(JSON.stringify(canonical(templates)))
|
||||
.digest("hex");
|
||||
const content = `// prettier-ignore\nexport const generatedTemplatePackageRegistry = ${JSON.stringify(templates, null, 2)};\nexport const generatedTemplatePackageRegistryHash =\n "${fingerprint}";\n`;
|
||||
const declaration = `import type { TemplatePackageManifest } from "./index.js";\nexport declare const generatedTemplatePackageRegistry: readonly TemplatePackageManifest[];\nexport declare const generatedTemplatePackageRegistryHash: string;\n`;
|
||||
const write = process.argv.includes("--write");
|
||||
if (write) {
|
||||
fs.writeFileSync(outputPath, content);
|
||||
fs.writeFileSync(declarationPath, declaration);
|
||||
} else {
|
||||
const current = fs.existsSync(outputPath) ? fs.readFileSync(outputPath, "utf8") : "";
|
||||
const generatedHash = current.match(
|
||||
/generatedTemplatePackageRegistryHash\s*=\s*"([a-f0-9]+)"/,
|
||||
)?.[1];
|
||||
if (generatedHash !== fingerprint) {
|
||||
console.error("Generated template catalog is stale. Run pnpm generate:template-catalog.");
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user