refactor: 退役运行时视觉模块并泛化模板门禁
This commit is contained in:
1 parent
25d1fe983c
commit
e2e2340bfe
56 files changed
+290
-1037
No files matched your search
@@ -1,40 +1,194 @@
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const root = path.resolve(__dirname, "..");
|
||||
const fixtureFiles = [
|
||||
"linjing-door-visual.json",
|
||||
"doors-visual.json",
|
||||
"acheng-campus-food-visual.json",
|
||||
];
|
||||
const fixtures = fixtureFiles.map((file) =>
|
||||
JSON.parse(fs.readFileSync(path.join(__dirname, "fixtures", file), "utf8")),
|
||||
);
|
||||
for (const fixture of fixtures) {
|
||||
const failures = [];
|
||||
for (const [state, definition] of Object.entries(fixture.states)) {
|
||||
const wxmlPath = path.join(root, `${definition.page}.wxml`);
|
||||
if (!fs.existsSync(wxmlPath)) {
|
||||
failures.push(`${state}: missing ${definition.page}.wxml`);
|
||||
continue;
|
||||
}
|
||||
let content = fs.readFileSync(wxmlPath, "utf8");
|
||||
const componentRefs = [...content.matchAll(/<([a-z][a-z0-9-]*)\s/g)]
|
||||
.map((match) => match[1])
|
||||
.filter((name) => name.includes("-"));
|
||||
for (const ref of componentRefs) {
|
||||
for (const componentRoot of ["components/modules", "components/templates"]) {
|
||||
const candidate = path.join(root, componentRoot, ref, `${ref}.wxml`);
|
||||
if (fs.existsSync(candidate)) content += fs.readFileSync(candidate, "utf8");
|
||||
}
|
||||
}
|
||||
for (const anchor of definition.anchors)
|
||||
if (!content.includes(anchor)) failures.push(`${state}: missing anchor ${anchor}`);
|
||||
}
|
||||
if (failures.length) {
|
||||
console.error(failures.join("\n"));
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(
|
||||
`Visual fixture ${fixture.templateKey} passed: ${Object.keys(fixture.states).length} states at ${fixture.viewport.width}x${fixture.viewport.height}.`,
|
||||
const os = require("node:os");
|
||||
const { execFileSync } = require("node:child_process");
|
||||
|
||||
const workspaceRoot = path.resolve(__dirname, "../../..");
|
||||
const packagesRoot = path.join(workspaceRoot, "packages");
|
||||
const compiler = path.join(packagesRoot, "template-compiler/src/cli.mjs");
|
||||
const pageExtensions = [".js", ".json", ".wxml", ".wxss"];
|
||||
|
||||
function readJson(filePath) {
|
||||
return JSON.parse(fs.readFileSync(filePath, "utf8"));
|
||||
}
|
||||
|
||||
function listTemplatePackages() {
|
||||
return fs
|
||||
.readdirSync(packagesRoot, { withFileTypes: true })
|
||||
.filter((entry) => entry.isDirectory() && entry.name.startsWith("template-"))
|
||||
.map((entry) => path.join(packagesRoot, entry.name))
|
||||
.filter((directory) => fs.existsSync(path.join(directory, "template.json")))
|
||||
.sort();
|
||||
}
|
||||
|
||||
function resolveComponent(output, fromFile, reference) {
|
||||
const base = reference.startsWith("/")
|
||||
? path.join(output, reference)
|
||||
: reference.startsWith(".")
|
||||
? path.resolve(path.dirname(path.join(output, fromFile)), reference)
|
||||
: path.join(output, "miniprogram_npm", reference);
|
||||
return [".json", ".js", ".wxml"].every((extension) => fs.existsSync(base + extension));
|
||||
}
|
||||
|
||||
function resolveRequire(output, fromFile, reference) {
|
||||
const base = path.resolve(path.dirname(path.join(output, fromFile)), reference);
|
||||
return [base, base + ".js", path.join(base, "index.js")].some(
|
||||
(candidate) => fs.existsSync(candidate) && fs.statSync(candidate).isFile(),
|
||||
);
|
||||
}
|
||||
|
||||
function listFiles(directory, base = directory, files = []) {
|
||||
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
|
||||
if (entry.name === "node_modules" || entry.name === "miniprogram_npm") continue;
|
||||
const absolutePath = path.join(directory, entry.name);
|
||||
if (entry.isDirectory()) listFiles(absolutePath, base, files);
|
||||
else files.push(path.relative(base, absolutePath).split(path.sep).join("/"));
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
function templateComponentNames(directory) {
|
||||
const componentRoot = path.join(directory, "miniapp/components/templates");
|
||||
if (!fs.existsSync(componentRoot)) return [];
|
||||
return fs
|
||||
.readdirSync(componentRoot, { withFileTypes: true })
|
||||
.filter((entry) => entry.isDirectory())
|
||||
.map((entry) => entry.name);
|
||||
}
|
||||
|
||||
function validateManifestOutput(manifest, output, otherComponentNames) {
|
||||
const failures = [];
|
||||
const appPath = path.join(output, "app.json");
|
||||
if (!fs.existsSync(appPath)) return ["missing generated app.json"];
|
||||
const app = readJson(appPath);
|
||||
const entrypointPages = Object.values(manifest.entrypoints || {});
|
||||
const declaredPages = [...new Set([...(manifest.pages || []), ...entrypointPages])];
|
||||
|
||||
for (const page of declaredPages) {
|
||||
for (const extension of pageExtensions) {
|
||||
if (!fs.existsSync(path.join(output, page + extension)))
|
||||
failures.push(`declared page ${page} is missing ${extension}`);
|
||||
}
|
||||
if (!app.pages.includes(page)) failures.push(`declared page ${page} is absent from app.json`);
|
||||
}
|
||||
|
||||
for (const page of app.pages || []) {
|
||||
for (const extension of pageExtensions) {
|
||||
if (!fs.existsSync(path.join(output, page + extension)))
|
||||
failures.push(`app page ${page} is missing ${extension}`);
|
||||
}
|
||||
}
|
||||
|
||||
const tabs = app.tabBar?.list || [];
|
||||
if (tabs.length !== manifest.navigation.length)
|
||||
failures.push(
|
||||
`app.json tabBar has ${tabs.length} entries, manifest declares ${manifest.navigation.length}`,
|
||||
);
|
||||
for (const tab of tabs) {
|
||||
if (!app.pages.includes(tab.pagePath))
|
||||
failures.push(`tabBar page is absent from app.json: ${tab.pagePath}`);
|
||||
for (const icon of [tab.iconPath, tab.selectedIconPath]) {
|
||||
if (icon && !fs.existsSync(path.join(output, icon)))
|
||||
failures.push(`tabBar icon is missing: ${icon}`);
|
||||
}
|
||||
}
|
||||
|
||||
for (const file of listFiles(output)) {
|
||||
if (!/\.(js|json|wxml)$/.test(file)) continue;
|
||||
const source = fs.readFileSync(path.join(output, file), "utf8");
|
||||
if (file.endsWith(".json") && (file.startsWith("pages/") || file.startsWith("components/"))) {
|
||||
let definition;
|
||||
try {
|
||||
definition = JSON.parse(source);
|
||||
} catch (error) {
|
||||
failures.push(`${file}: invalid JSON (${error.message})`);
|
||||
continue;
|
||||
}
|
||||
for (const [name, reference] of Object.entries(definition.usingComponents || {})) {
|
||||
if (reference.startsWith("plugin://")) continue;
|
||||
if (
|
||||
reference.startsWith("/miniprogram_npm/") &&
|
||||
!fs.existsSync(path.join(output, "miniprogram_npm"))
|
||||
)
|
||||
continue;
|
||||
if (!resolveComponent(output, file, reference))
|
||||
failures.push(`${file}: component ${name} cannot be resolved (${reference})`);
|
||||
}
|
||||
}
|
||||
if (file.endsWith(".js")) {
|
||||
for (const match of source.matchAll(/require\(\s*["'](\.{1,2}\/[^"']+)["']\s*\)/g)) {
|
||||
if (!resolveRequire(output, file, match[1]))
|
||||
failures.push(`${file}: require("${match[1]}") cannot be resolved`);
|
||||
}
|
||||
}
|
||||
if (file.startsWith("pages/") || file.startsWith("components/")) {
|
||||
for (const componentName of otherComponentNames) {
|
||||
if (source.includes(componentName))
|
||||
failures.push(`${file}: references another template component ${componentName}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
return failures;
|
||||
}
|
||||
|
||||
function validateTemplate(directory) {
|
||||
const manifest = readJson(path.join(directory, "template.json"));
|
||||
const output = fs.mkdtempSync(path.join(os.tmpdir(), "personal-brand-template-"));
|
||||
try {
|
||||
let compilerOutput = "";
|
||||
try {
|
||||
compilerOutput = execFileSync(
|
||||
process.execPath,
|
||||
[compiler, "--template", manifest.packageKey, "--out", output],
|
||||
{ cwd: workspaceRoot, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] },
|
||||
);
|
||||
} catch (error) {
|
||||
const details = [error.stdout, error.stderr].filter(Boolean).join("\n").trim();
|
||||
return [`compiler failed${details ? `: ${details}` : ""}`];
|
||||
}
|
||||
const otherComponentNames = listTemplatePackages()
|
||||
.filter((candidate) => path.resolve(candidate) !== path.resolve(directory))
|
||||
.flatMap(templateComponentNames);
|
||||
const failures = validateManifestOutput(manifest, output, otherComponentNames);
|
||||
if (failures.length) return failures;
|
||||
return {
|
||||
compilerOutput: compilerOutput.trim(),
|
||||
pageCount: readJson(path.join(output, "app.json")).pages.length,
|
||||
tabCount: manifest.navigation.length,
|
||||
};
|
||||
} finally {
|
||||
fs.rmSync(output, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
const templateDirectories = listTemplatePackages();
|
||||
if (!templateDirectories.length) {
|
||||
console.error("No template packages found under packages/template-*");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const failures = [];
|
||||
for (const directory of templateDirectories) {
|
||||
const manifestPath = path.join(directory, "template.json");
|
||||
let manifest;
|
||||
try {
|
||||
manifest = readJson(manifestPath);
|
||||
} catch (error) {
|
||||
failures.push(`${path.basename(directory)}: invalid template.json (${error.message})`);
|
||||
continue;
|
||||
}
|
||||
const result = validateTemplate(directory);
|
||||
if (Array.isArray(result)) {
|
||||
failures.push(`${manifest.packageKey}:\n ${result.join("\n ")}`);
|
||||
continue;
|
||||
}
|
||||
console.log(
|
||||
`Template ${manifest.packageKey}@${manifest.version} passed: ${result.pageCount} pages, ${result.tabCount} tabs.`,
|
||||
);
|
||||
}
|
||||
|
||||
if (failures.length) {
|
||||
console.error(failures.join("\n"));
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`Validated ${templateDirectories.length} template packages.`);
|
||||
Reference in new issue
Block a user