check-ai-artifacts.mjs 3.16 KB
#!/usr/bin/env node

/**
 * AI 产物专项 fitness 检查
 *
 * 验证 AI 相关的 schema 和 manifest 文件可解析、结构完整。
 */

import fs from "node:fs";
import path from "node:path";
import process from "node:process";

import { requireWorkspaceRoot } from "./workspace-root.mjs";

const repoRoot = requireWorkspaceRoot();
const failures = [];
let totalChecks = 0;

function relativePath(file) {
	return path.relative(repoRoot, file).replaceAll("\\", "/");
}

function check(condition, file, message) {
	totalChecks++;
	if (!condition) {
		failures.push(`${relativePath(file)}: ${message}`);
	}
}

// ── Schema 文件可解析性检查 ──

const schemaFiles = [
	path.join(repoRoot, "contracts", "ai", "project-context.schema.json"),
	path.join(repoRoot, "contracts", "ai", "ai-direct-context-bundle.schema.json"),
	path.join(repoRoot, "contracts", "ai", "component-manifest.schema.json"),
	path.join(repoRoot, "contracts", "ai", "component-manifest-index.schema.json"),
	path.join(repoRoot, "contracts", "ai", "component-manifest-item.schema.json"),
];

console.log("[ai-artifacts] Checking schema files...");

for (const schemaFile of schemaFiles) {
	if (fs.existsSync(schemaFile)) {
		totalChecks++;
		try {
			JSON.parse(fs.readFileSync(schemaFile, "utf8"));
		} catch (error) {
			failures.push(`${relativePath(schemaFile)}: schema JSON parse error: ${error.message}`);
		}
	}
}

// ── Manifest 文件可解析性 + 结构检查 ──

const manifestFiles = [
	path.join(repoRoot, "contracts", "ai", "element-plus.manifest.json"),
	path.join(repoRoot, "packages", "vui", "vue", "meta", "index.json"),
];

console.log("[ai-artifacts] Checking manifest files...");

for (const manifestFile of manifestFiles) {
	if (fs.existsSync(manifestFile)) {
		totalChecks++;
		try {
			const manifest = JSON.parse(fs.readFileSync(manifestFile, "utf8"));
			check("packageName" in manifest, relativePath(manifestFile), "manifest missing 'packageName'");
			check("components" in manifest, relativePath(manifestFile), "manifest missing 'components'");
			if (Array.isArray(manifest.components)) {
				for (const comp of manifest.components) {
					const componentName = comp.exportName || comp.name || "?";
					check("exportName" in comp, relativePath(manifestFile), `component "${componentName}" missing 'exportName'`);
					if ("manifest" in comp) {
						check(
							typeof comp.manifest === "string" && comp.manifest.length > 0,
							relativePath(manifestFile),
							`component "${componentName}" missing 'manifest' path`,
						);
					} else {
						check(
							"figmaMapping" in comp,
							relativePath(manifestFile),
							`component "${componentName}" missing 'figmaMapping'`,
						);
					}
				}
			}
		} catch (error) {
			failures.push(`${relativePath(manifestFile)}: manifest JSON parse error: ${error.message}`);
		}
	}
}

// ── 输出 ──

console.log("\n[ai-artifacts] Results");
console.log(`  Total checks: ${totalChecks}`);

if (failures.length > 0) {
	console.error(`\n[ai-artifacts] FAILED (${failures.length} issue(s)):`);
	for (const f of failures) {
		console.error(`  ✗ ${f}`);
	}
	process.exit(1);
}

console.log("\n[ai-artifacts] All checks passed.");
process.exit(0);