feat: 增加模板自动一致性校验

This commit is contained in:
que01 committed 2026-10-16 14:12:46 +08:00
1 parent cddf836f4f
commit 553a07c4ec
4 files changed
+145 -2

No files matched your search

+126
View File
@@ -0,0 +1,126 @@
#!/usr/bin/env node
const fs = require("node:fs");
const os = require("node:os");
const path = require("node:path");
const { spawnSync } = require("node:child_process");
const root = path.resolve(__dirname, "..");
const packagesRoot = path.join(root, "packages");
const compiler = path.join(packagesRoot, "template-compiler/src/cli.mjs");
const doctor = path.join(root, "scripts/template-doctor.mjs");
function readJson(file) {
return JSON.parse(fs.readFileSync(file, "utf8"));
}
function run(command, args) {
const result = spawnSync(command, args, { cwd: root, encoding: "utf8", stdio: "inherit" });
if (result.error) throw result.error;
if (result.status !== 0)
throw new Error(`${path.basename(command)} failed for ${args.join(" ")}`);
}
function runQuiet(command, args) {
const result = spawnSync(command, args, { cwd: root, encoding: "utf8", stdio: "ignore" });
if (result.error) throw result.error;
if (result.status !== 0)
throw new Error(`${path.basename(command)} failed for ${args.join(" ")}`);
}
function discoverTemplates(requestedKey) {
const templates = fs
.readdirSync(packagesRoot, { withFileTypes: true })
.filter(
(entry) =>
entry.isDirectory() &&
entry.name.startsWith("template-") &&
fs.existsSync(path.join(packagesRoot, entry.name, "template.json")),
)
.map((entry) => {
const directory = path.join(packagesRoot, entry.name);
const manifest = readJson(path.join(directory, "template.json"));
return { directory, manifest };
})
.filter(({ manifest }) => !requestedKey || manifest.packageKey === requestedKey)
.sort((left, right) => left.manifest.packageKey.localeCompare(right.manifest.packageKey));
if (!templates.length)
throw new Error(
requestedKey ? `No matching templates found: ${requestedKey}` : "No template packages found",
);
return templates;
}
function validateGeneratedOutput(template, output) {
const app = readJson(path.join(output, "app.json"));
const runtime = readJson(path.join(output, "template.runtime.json"));
if (runtime.packageKey !== template.manifest.packageKey)
throw new Error(`${template.manifest.packageKey}: generated runtime packageKey mismatch`);
if (runtime.version !== template.manifest.version)
throw new Error(`${template.manifest.packageKey}: generated runtime version mismatch`);
if (!Array.isArray(app.pages) || app.pages.length === 0)
throw new Error(`${template.manifest.packageKey}: generated app.json has no pages`);
if (app.tabBar?.list?.length !== template.manifest.navigation.length)
throw new Error(`${template.manifest.packageKey}: generated tabBar does not match manifest`);
for (const page of app.pages) {
for (const extension of [".js", ".json", ".wxml", ".wxss"]) {
if (!fs.existsSync(path.join(output, page + extension)))
throw new Error(
`${template.manifest.packageKey}: generated page is incomplete: ${page}${extension}`,
);
}
}
}
function runTemplate(template) {
const output = fs.mkdtempSync(path.join(os.tmpdir(), "personal-brand-template-conformance-"));
const report = path.join(output, "doctor.json");
try {
run(process.execPath, [
compiler,
"--template",
template.manifest.packageKey,
"--env",
"dev",
"--out",
output,
]);
validateGeneratedOutput(template, output);
runQuiet(process.execPath, [
doctor,
"--template",
template.manifest.packageKey,
"--output",
report,
]);
const doctorReport = readJson(report);
const result = doctorReport.templates?.[0];
if (!result || result.staticStatus !== "pass")
throw new Error(`${template.manifest.packageKey}: template-doctor static status is not pass`);
console.log(
`Conformance passed: ${template.manifest.packageKey}@${template.manifest.version} (${result.regions.length} regions)`,
);
} finally {
fs.rmSync(output, { recursive: true, force: true });
}
}
try {
const args = process.argv.slice(2);
if (args[0] === "--") args.shift();
const templateFlag = args.indexOf("--template");
if (templateFlag !== -1 && !args[templateFlag + 1])
throw new Error("--template requires a value");
const unknown = args.filter(
(argument, index) => argument.startsWith("--") && index !== templateFlag,
);
if (unknown.length) throw new Error(`Unknown argument: ${unknown[0]}`);
const requestedKey = templateFlag === -1 ? undefined : args[templateFlag + 1];
run("node", [path.join(root, "scripts/validate-template-packages.cjs")]);
const templates = discoverTemplates(requestedKey);
for (const template of templates) runTemplate(template);
console.log(`Template conformance passed: ${templates.length} package(s).`);
} catch (error) {
console.error(error instanceof Error ? error.message : String(error));
process.exitCode = 1;
}
+16
View File
@@ -0,0 +1,16 @@
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import path from "node:path";
import { fileURLToPath } from "node:url";
import test from "node:test";
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const script = path.join(root, "scripts/template-conformance.cjs");
test("template:conformance automatically discovers all template packages", () => {
const output = execFileSync(process.execPath, [script], { cwd: root, encoding: "utf8" });
assert.match(output, /Conformance passed: acheng-campus-food@1\.0\.0/);
assert.match(output, /Conformance passed: atelier-solopreneur@1\.1\.0/);
assert.match(output, /Conformance passed: linjing-editorial@1\.0\.0/);
assert.match(output, /Template conformance passed: 3 package\(s\)\./);
});