108 lines
4.4 KiB
JavaScript
108 lines
4.4 KiB
JavaScript
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
|
|
const root = path.resolve(__dirname, "..");
|
|
const packageRoot = path.join(root, "templates");
|
|
const requiredPages = ["home", "offerings", "detail", "inquiry", "success", "records", "profile"];
|
|
const errors = [];
|
|
|
|
for (const entry of fs.readdirSync(packageRoot, { withFileTypes: true })) {
|
|
if (!entry.isDirectory()) continue;
|
|
const packageDir = path.join(packageRoot, entry.name);
|
|
const manifestPath = path.join(packageDir, "template.json");
|
|
if (!fs.existsSync(manifestPath)) {
|
|
errors.push(`${entry.name}: template.json is missing`);
|
|
continue;
|
|
}
|
|
let manifest;
|
|
try {
|
|
manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
|
} catch (error) {
|
|
errors.push(`${entry.name}: invalid template.json (${error.message})`);
|
|
continue;
|
|
}
|
|
if (manifest.packageKey !== entry.name)
|
|
errors.push(`${entry.name}: packageKey must match directory name`);
|
|
if (!manifest.version) errors.push(`${entry.name}: version is required`);
|
|
if (manifest.runtimeContract !== "1.0") errors.push(`${entry.name}: unsupported runtimeContract`);
|
|
for (const page of requiredPages) {
|
|
if (typeof manifest.entrypoints?.[page] !== "string" || !manifest.entrypoints[page]) {
|
|
errors.push(`${entry.name}: entrypoints.${page} is required`);
|
|
}
|
|
}
|
|
if (
|
|
!Array.isArray(manifest.supportedIndustryTemplates) ||
|
|
!manifest.supportedIndustryTemplates.length
|
|
) {
|
|
errors.push(`${entry.name}: supportedIndustryTemplates must not be empty`);
|
|
}
|
|
if (!Array.isArray(manifest.requiredCapabilities))
|
|
errors.push(`${entry.name}: requiredCapabilities must be an array`);
|
|
if (!fs.existsSync(path.join(packageDir, "README.md")))
|
|
errors.push(`${entry.name}: README.md is missing`);
|
|
}
|
|
|
|
if (errors.length) {
|
|
console.error(errors.join("\n"));
|
|
process.exit(1);
|
|
}
|
|
console.log(
|
|
`Template packages validated: ${fs.readdirSync(packageRoot, { withFileTypes: true }).filter((entry) => entry.isDirectory()).length}`,
|
|
);
|
|
|
|
const workspacePackageRoot = path.join(root, "packages");
|
|
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 manifestPath = path.join(workspacePackageRoot, entry.name, "template.json");
|
|
if (!fs.existsSync(manifestPath)) errors.push(`${entry.name}: template.json is missing`);
|
|
else {
|
|
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
|
|
if (
|
|
manifest.packageKey !== entry.name &&
|
|
manifest.packageKey !== entry.name.replace(/^template-/, "")
|
|
)
|
|
errors.push(`${entry.name}: packageKey must match directory name or template key`);
|
|
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 (manifest.ownedPaths !== undefined && !Array.isArray(manifest.ownedPaths))
|
|
errors.push(`${entry.name}: ownedPaths must be an array`);
|
|
if (
|
|
manifest.renderers !== undefined &&
|
|
(typeof manifest.renderers !== "object" || Array.isArray(manifest.renderers))
|
|
)
|
|
errors.push(`${entry.name}: renderers must be an object`);
|
|
if (
|
|
manifest.runtimeOwnedPages !== undefined &&
|
|
(!Array.isArray(manifest.runtimeOwnedPages) ||
|
|
manifest.runtimeOwnedPages.some(
|
|
(page) => !["detail", "inquiry", "success", "records"].includes(page),
|
|
))
|
|
)
|
|
errors.push(`${entry.name}: runtimeOwnedPages contains an unsupported page`);
|
|
for (const item of manifest.navigation || []) {
|
|
for (const asset of [item.iconPath, item.selectedIconPath]) {
|
|
if (asset && !fs.existsSync(path.join(workspacePackageRoot, entry.name, "miniapp", asset)))
|
|
errors.push(`${entry.name}: navigation asset is missing: ${asset}`);
|
|
}
|
|
}
|
|
for (const page of requiredPages)
|
|
if (typeof manifest.entrypoints?.[page] !== "string")
|
|
errors.push(`${entry.name}: entrypoints.${page} is required`);
|
|
}
|
|
}
|
|
if (errors.length) {
|
|
console.error(errors.join("\n"));
|
|
process.exit(1);
|
|
}
|
|
console.log(`Workspace template packages validated: ${workspaceTemplates.length}`);
|