127 lines
4.8 KiB
JavaScript
127 lines
4.8 KiB
JavaScript
#!/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;
|
|
}
|