71 lines
2.8 KiB
JavaScript
71 lines
2.8 KiB
JavaScript
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const { pathToFileURL } = require("node:url");
|
|
|
|
const root = path.resolve(__dirname, "..");
|
|
require(path.join(root, "scripts/generate-template-catalog.cjs"));
|
|
const workspacePackageRoot = path.join(root, "packages");
|
|
const contractEntry = path.join(workspacePackageRoot, "template-contract/src/index.ts");
|
|
const workspaceTemplates = fs
|
|
.readdirSync(workspacePackageRoot, { withFileTypes: true })
|
|
.filter(
|
|
(entry) =>
|
|
entry.isDirectory() &&
|
|
entry.name.startsWith("template-") &&
|
|
fs.existsSync(path.join(workspacePackageRoot, entry.name, "template.json")),
|
|
);
|
|
|
|
async function main() {
|
|
// manifest 规则只在 template-contract 中维护;这里只补充依赖文件系统的检查。
|
|
const { validateTemplatePackageManifest } = await import(pathToFileURL(contractEntry).href);
|
|
const errors = [];
|
|
for (const entry of workspaceTemplates) {
|
|
const packageDir = path.join(workspacePackageRoot, entry.name);
|
|
let manifest;
|
|
try {
|
|
manifest = JSON.parse(fs.readFileSync(path.join(packageDir, "template.json"), "utf8"));
|
|
} catch (error) {
|
|
errors.push(`${entry.name}: invalid template.json (${error.message})`);
|
|
continue;
|
|
}
|
|
let packageJson;
|
|
try {
|
|
packageJson = JSON.parse(fs.readFileSync(path.join(packageDir, "package.json"), "utf8"));
|
|
} catch (error) {
|
|
errors.push(entry.name + ": invalid package.json (" + error.message + ")");
|
|
continue;
|
|
}
|
|
for (const error of validateTemplatePackageManifest(manifest))
|
|
errors.push(`${entry.name}: ${error}`);
|
|
if (![entry.name, entry.name.replace(/^template-/, "")].includes(manifest.packageKey))
|
|
errors.push(`${entry.name}: packageKey must match directory name or template key`);
|
|
if (packageJson.name !== "@personal-brand/template-" + manifest.packageKey)
|
|
errors.push(entry.name + ": package name must match manifest packageKey");
|
|
if (packageJson.version !== manifest.version)
|
|
errors.push(
|
|
entry.name +
|
|
": package version " +
|
|
packageJson.version +
|
|
" != manifest " +
|
|
manifest.version,
|
|
);
|
|
for (const page of manifest.pages || []) {
|
|
if (!fs.existsSync(path.join(packageDir, "miniapp", page + ".js")))
|
|
errors.push(`${entry.name}: declared page is missing: ${page}`);
|
|
}
|
|
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}`);
|
|
}
|
|
|
|
main();
|