feat: 增加模板自动一致性校验
This commit is contained in:
1 parent
cddf836f4f
commit
553a07c4ec
4 files changed
+145
-2
No files matched your search
@@ -361,7 +361,7 @@ Phase 0 执行记录(2026-10-16):
|
||||
|
||||
- [ ] 扩展 packages/template-contract,定义 package schema、rendererRef、preview/bootstrap、资源和兼容矩阵;
|
||||
- [x] 实现 `template:pack`,生成稳定清单和 checksum;当前覆盖 3 个仓库模板,测试重复导出目录和 archive checksum 一致;
|
||||
- [ ] 实现 package-discovered template:conformance;
|
||||
- [x] 实现 package-discovered `template:conformance`,自动发现模板并串联真实 compiler、doctor 和产物契约;
|
||||
- [ ] 为每个当前模板补齐真实 manifest、可见字段、编辑绑定、空态和 fixture;
|
||||
- [ ] 把版本漂移、缺资源、未知 binding、未消费字段和不兼容 renderer 变成阻断 issue;
|
||||
- [ ] 记录旧原生 miniapp 和 Taro 目录的迁移边界。
|
||||
|
||||
+2
-1
@@ -20,9 +20,10 @@
|
||||
"validate:templates": "node scripts/validate-template-packages.cjs && pnpm --filter @personal-brand/template-contract test",
|
||||
"generate:template-catalog": "node scripts/generate-template-catalog.cjs --write",
|
||||
"template:dev": "node packages/template-compiler/src/cli.mjs --template",
|
||||
"template:check": "pnpm validate:templates && node --test scripts/template-pack.test.mjs && pnpm test:template-compiler && pnpm --filter @personal-brand/miniapp test:visual-fixture",
|
||||
"template:check": "pnpm validate:templates && node --test scripts/template-pack.test.mjs && node --test scripts/template-conformance.test.mjs && pnpm test:template-compiler && pnpm --filter @personal-brand/miniapp test:visual-fixture",
|
||||
"validate:template-release": "node scripts/validate-template-release.cjs",
|
||||
"template:pack": "node scripts/template-pack.cjs",
|
||||
"template:conformance": "node scripts/template-conformance.cjs",
|
||||
"test:template-compiler": "pnpm --filter @personal-brand/template-compiler test",
|
||||
"clean": "turbo run clean && rimraf node_modules .turbo",
|
||||
"preview:food": "pnpm --filter @personal-brand/taro dev:h5",
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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\)\./);
|
||||
});
|
||||
Reference in new issue
Block a user