Files
persionalBrand/scripts/validate-template-packages.cjs
T

59 lines
2.6 KiB
JavaScript

const fs = require("node:fs");
const path = require("node:path");
const root = path.resolve(__dirname, "..");
require(path.join(root, "scripts/generate-template-catalog.cjs"));
const workspacePackageRoot = path.join(root, "packages");
const requiredPages = ["home", "offerings", "detail", "inquiry", "success", "records", "profile"];
const errors = [];
const workspaceTemplates = fs
.readdirSync(workspacePackageRoot, { withFileTypes: true })
.filter(
(entry) =>
entry.isDirectory() &&
entry.name.startsWith("template-") &&
fs.existsSync(path.join(workspacePackageRoot, entry.name, "template.json")),
);
for (const entry of workspaceTemplates) {
const packageDir = path.join(workspacePackageRoot, entry.name);
const manifestPath = path.join(packageDir, "template.json");
let manifest;
try {
manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
} catch (error) {
errors.push(`${entry.name}: invalid template.json (${error.message})`);
continue;
}
if (![entry.name, entry.name.replace(/^template-/, "")].includes(manifest.packageKey))
errors.push(`${entry.name}: packageKey must match directory name or template key`);
if (!manifest.version) errors.push(`${entry.name}: version is required`);
if (manifest.runtimeContract !== "1.0") errors.push(`${entry.name}: unsupported runtimeContract`);
if (!manifest.displayName) errors.push(`${entry.name}: displayName is required`);
if (!Array.isArray(manifest.navigation) || !manifest.navigation.length)
errors.push(`${entry.name}: navigation is required`);
if (!Array.isArray(manifest.dataSlots) || !manifest.dataSlots.length)
errors.push(`${entry.name}: dataSlots is required`);
if (!Array.isArray(manifest.assetSlots))
errors.push(`${entry.name}: assetSlots must be an array`);
if (!manifest.admin || !Array.isArray(manifest.admin.sections) || !manifest.admin.sections.length)
errors.push(`${entry.name}: admin.sections is required`);
if (!Array.isArray(manifest.pages) || !manifest.pages.length)
errors.push(`${entry.name}: pages is required`);
for (const page of requiredPages)
if (typeof manifest.entrypoints?.[page] !== "string")
errors.push(`${entry.name}: entrypoints.${page} is required`);
for (const item of manifest.navigation || []) {
for (const asset of [item.iconPath, item.selectedIconPath]) {
if (asset && !fs.existsSync(path.join(packageDir, "miniapp", asset)))
errors.push(`${entry.name}: navigation asset is missing: ${asset}`);
}
}
}
if (errors.length) {
console.error(errors.join("\n"));
process.exit(1);
}
console.log(`Workspace template packages validated: ${workspaceTemplates.length}`);