refactor: 重构模板叠加式编译产物

This commit is contained in:
que01 committed 2026-09-26 11:21:29 +08:00
1 parent c46297b13b
commit 25d1fe983c
93 files changed
+870 -2467

No files matched your search

+34 -94
View File
@@ -3,55 +3,9 @@ const path = require("node:path");
const root = path.resolve(__dirname, "..");
require(path.join(root, "scripts/generate-template-catalog.cjs"));
const packageRoot = path.join(root, "templates");
const workspacePackageRoot = path.join(root, "packages");
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(
@@ -60,57 +14,43 @@ const workspaceTemplates = fs
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 (!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 (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}`);
}
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}`);
}
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);