feat: 收口模板升级与阶段七持续回归
This commit is contained in:
1 parent
ec16fff70e
commit
71c528de34
29 files changed
+1962
-304
No files matched your search
@@ -0,0 +1,223 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const ts = require("typescript");
|
||||
|
||||
const root = path.resolve(__dirname, "..");
|
||||
const packagesRoot = path.join(root, "packages");
|
||||
const configRoot = path.join(packagesRoot, "config/src");
|
||||
const industrySource = fs.readFileSync(path.join(configRoot, "brand-templates.ts"), "utf8");
|
||||
const visualSource = fs.readFileSync(path.join(configRoot, "visual-templates.ts"), "utf8");
|
||||
const capabilitySource = fs.readFileSync(path.join(configRoot, "capability-contracts.ts"), "utf8");
|
||||
const platformPackage = JSON.parse(
|
||||
fs.readFileSync(path.join(root, "apps/taro/package.json"), "utf8"),
|
||||
);
|
||||
const rendererSource = fs.readFileSync(
|
||||
path.join(root, "apps/admin/src/features/brand/component-renderers.jsx"),
|
||||
"utf8",
|
||||
);
|
||||
function exportedObject(source, name) {
|
||||
const file = ts.createSourceFile("config.ts", source, ts.ScriptTarget.Latest, true);
|
||||
for (const statement of file.statements) {
|
||||
if (!ts.isVariableStatement(statement)) continue;
|
||||
for (const declaration of statement.declarationList.declarations) {
|
||||
if (
|
||||
ts.isIdentifier(declaration.name) &&
|
||||
declaration.name.text === name &&
|
||||
declaration.initializer
|
||||
) {
|
||||
const initializer = declaration.initializer;
|
||||
const object = ts.isSatisfiesExpression(initializer) ? initializer.expression : initializer;
|
||||
if (ts.isAsExpression(object)) return object.expression;
|
||||
return object;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new Error(`Could not find exported object ${name}`);
|
||||
}
|
||||
function propertyName(property) {
|
||||
if (!ts.isPropertyAssignment(property)) return null;
|
||||
if (ts.isIdentifier(property.name) || ts.isStringLiteral(property.name))
|
||||
return property.name.text;
|
||||
return null;
|
||||
}
|
||||
function propertyValue(object, key) {
|
||||
if (!ts.isObjectLiteralExpression(object)) return null;
|
||||
return object.properties.find((property) => propertyName(property) === key)?.initializer ?? null;
|
||||
}
|
||||
function objectKeys(object) {
|
||||
return ts.isObjectLiteralExpression(object)
|
||||
? object.properties.map(propertyName).filter(Boolean)
|
||||
: [];
|
||||
}
|
||||
function stringArray(expression) {
|
||||
return expression && ts.isArrayLiteralExpression(expression)
|
||||
? expression.elements.filter(ts.isStringLiteral).map((element) => element.text)
|
||||
: [];
|
||||
}
|
||||
const brandTemplateObject = exportedObject(industrySource, "brandTemplates");
|
||||
const visualTemplateObject = exportedObject(visualSource, "visualTemplates");
|
||||
const capabilityObject = exportedObject(capabilitySource, "capabilityRegistry");
|
||||
const industries = objectKeys(brandTemplateObject);
|
||||
const visualKeys = objectKeys(visualTemplateObject);
|
||||
const capabilityKeys = objectKeys(capabilityObject);
|
||||
const industryCapabilities = Object.fromEntries(
|
||||
(ts.isObjectLiteralExpression(brandTemplateObject) ? brandTemplateObject.properties : [])
|
||||
.map((property) => {
|
||||
const key = propertyName(property);
|
||||
const definition = ts.isPropertyAssignment(property) ? property.initializer : null;
|
||||
return [key, stringArray(propertyValue(definition, "capabilities"))];
|
||||
})
|
||||
.filter(([key]) => key),
|
||||
);
|
||||
const rendererGlob = rendererSource.match(/template-\*\/taro\/index\.tsx/);
|
||||
const packages = fs
|
||||
.readdirSync(packagesRoot, { withFileTypes: true })
|
||||
.filter(
|
||||
(entry) =>
|
||||
entry.isDirectory() &&
|
||||
entry.name.startsWith("template-") &&
|
||||
fs.existsSync(path.join(packagesRoot, entry.name, "template.json")),
|
||||
)
|
||||
.map((entry) => ({
|
||||
directory: entry.name,
|
||||
package: JSON.parse(
|
||||
fs.readFileSync(path.join(packagesRoot, entry.name, "package.json"), "utf8"),
|
||||
),
|
||||
manifest: JSON.parse(
|
||||
fs.readFileSync(path.join(packagesRoot, entry.name, "template.json"), "utf8"),
|
||||
),
|
||||
}))
|
||||
.sort((left, right) => left.manifest.packageKey.localeCompare(right.manifest.packageKey));
|
||||
|
||||
const results = [];
|
||||
const errors = [];
|
||||
const versionMajorMinor = (value) =>
|
||||
String(value ?? "")
|
||||
.replace(/^[^0-9]*/, "")
|
||||
.split(".")
|
||||
.slice(0, 2)
|
||||
.join(".");
|
||||
const platformRuntimeVersions = Object.fromEntries(
|
||||
Object.entries({ ...platformPackage.dependencies, ...platformPackage.devDependencies }).filter(
|
||||
([name]) => name.startsWith("@tarojs/") || name === "react" || name === "react-dom",
|
||||
),
|
||||
);
|
||||
for (const { directory, manifest, package: packageMetadata } of packages) {
|
||||
const key = manifest.packageKey;
|
||||
const packageIndustries = Array.isArray(manifest.supportedIndustryTemplates)
|
||||
? manifest.supportedIndustryTemplates
|
||||
: [];
|
||||
const visualKey = manifest.visualTemplateKey ?? key;
|
||||
const visualRegistered = visualKeys.includes(visualKey);
|
||||
const rendererPath = `${directory}/taro/index.tsx`;
|
||||
const rendererExists = fs.existsSync(path.join(packagesRoot, rendererPath));
|
||||
const rendererExport = rendererExists
|
||||
? /export\s+(?:async\s+)?function\s+TemplatePage\b|export\s+\{[^}]*\bTemplatePage\b/.test(
|
||||
fs.readFileSync(path.join(packagesRoot, rendererPath), "utf8"),
|
||||
)
|
||||
: false;
|
||||
const templateTaroVersion = packageMetadata.dependencies?.["@tarojs/taro"];
|
||||
const toolchainMismatches = Object.entries(platformRuntimeVersions).flatMap(([name, version]) => {
|
||||
const templateVersion =
|
||||
packageMetadata.dependencies?.[name] ?? packageMetadata.devDependencies?.[name];
|
||||
return templateVersion && versionMajorMinor(templateVersion) !== versionMajorMinor(version)
|
||||
? [`${name}: package ${templateVersion}, platform ${version}`]
|
||||
: [];
|
||||
});
|
||||
if (!templateTaroVersion) toolchainMismatches.push("@tarojs/taro: package dependency is missing");
|
||||
if (!packageMetadata.dependencies?.react)
|
||||
toolchainMismatches.push("react: package dependency is missing");
|
||||
const toolchainCompatible = toolchainMismatches.length === 0;
|
||||
const declaredCapabilities = Array.isArray(manifest.requiredCapabilities)
|
||||
? manifest.requiredCapabilities
|
||||
: [];
|
||||
const unknownCapabilities = declaredCapabilities.filter(
|
||||
(capability) => !capabilityKeys.includes(capability),
|
||||
);
|
||||
if (!industries.length) errors.push("No industry definitions found in brand-templates.ts");
|
||||
if (!capabilityKeys.length)
|
||||
errors.push("No capability definitions found in capability-contracts.ts");
|
||||
if (!rendererGlob) errors.push("Admin renderer package glob is not discoverable");
|
||||
if (!packageIndustries.length) errors.push(`${key}: supportedIndustryTemplates is empty`);
|
||||
if (unknownCapabilities.length)
|
||||
errors.push(`${key}: unknown capabilities ${unknownCapabilities.join(", ")}`);
|
||||
if (!toolchainCompatible)
|
||||
errors.push(`${key}: Taro/React package versions are incompatible with the platform toolchain`);
|
||||
for (const industry of packageIndustries) {
|
||||
if (!industries.includes(industry)) errors.push(`${key}: unknown industry ${industry}`);
|
||||
const incompatibleCapabilities = declaredCapabilities.filter(
|
||||
(capability) =>
|
||||
!unknownCapabilities.includes(capability) &&
|
||||
!industryCapabilities[industry]?.includes(capability),
|
||||
);
|
||||
const supported =
|
||||
industries.includes(industry) &&
|
||||
visualRegistered &&
|
||||
rendererExport &&
|
||||
toolchainCompatible &&
|
||||
!unknownCapabilities.length &&
|
||||
!incompatibleCapabilities.length;
|
||||
results.push({
|
||||
packageKey: key,
|
||||
version: manifest.version,
|
||||
industry,
|
||||
visualTemplateKey: visualKey,
|
||||
runtimeContract: manifest.runtimeContract,
|
||||
capabilities: declaredCapabilities,
|
||||
toolchain: {
|
||||
package: Object.fromEntries(
|
||||
Object.keys(platformRuntimeVersions).flatMap((name) => {
|
||||
const version =
|
||||
packageMetadata.dependencies?.[name] ?? packageMetadata.devDependencies?.[name];
|
||||
return version ? [[name, version]] : [];
|
||||
}),
|
||||
),
|
||||
platform: platformRuntimeVersions,
|
||||
compatible: toolchainCompatible,
|
||||
mismatches: toolchainMismatches,
|
||||
},
|
||||
renderer: rendererExists && rendererExport ? rendererPath : null,
|
||||
adminRendererDiscovery: Boolean(rendererGlob),
|
||||
status: supported ? "supported" : "rejected",
|
||||
reasons: [
|
||||
...(!industries.includes(industry) ? ["unknown industry"] : []),
|
||||
...(!visualRegistered ? ["platform visual renderer is not registered"] : []),
|
||||
...(!rendererExists ? ["Taro package renderer is missing"] : []),
|
||||
...(rendererExists && !rendererExport
|
||||
? ["Taro renderer does not export TemplatePage"]
|
||||
: []),
|
||||
...toolchainMismatches.map((mismatch) => `toolchain mismatch: ${mismatch}`),
|
||||
...incompatibleCapabilities.map(
|
||||
(capability) => `industry does not declare capability ${capability}`,
|
||||
),
|
||||
],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const report = {
|
||||
schemaVersion: 1,
|
||||
runtimeContract: "1.0",
|
||||
industries,
|
||||
platformVisualRenderers: visualKeys,
|
||||
capabilities: capabilityKeys,
|
||||
packages: results,
|
||||
};
|
||||
if (process.argv.includes("--json")) process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
|
||||
else {
|
||||
for (const item of results)
|
||||
process.stdout.write(
|
||||
`${item.status.padEnd(9)} ${item.packageKey}@${item.version} × ${item.industry} → ${item.visualTemplateKey}${item.reasons.length ? ` (${item.reasons.join(", ")})` : ""}\n`,
|
||||
);
|
||||
}
|
||||
if (errors.length) {
|
||||
process.stderr.write(`${errors.join("\n")}\n`);
|
||||
process.exitCode = 1;
|
||||
} else if (!packages.length) {
|
||||
process.stderr.write("No template packages found\n");
|
||||
process.exitCode = 1;
|
||||
} else if (results.some((item) => item.status === "rejected")) {
|
||||
process.exitCode = 1;
|
||||
}
|
||||
Reference in new issue
Block a user