generate-design-mapper-examples.mjs 2.11 KB
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";

import { enhanceFigmaFrame, loadComponentManifest } from "../packages/ai/design-mapper/src/index.mjs";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const root = path.resolve(__dirname, "..");

// Load manifests (vui + element-plus fallback chain)
const vuiManifest = loadComponentManifest(path.join(root, "packages/vui/vue/meta/index.json"));
if (!vuiManifest) {
	console.error("Failed to load vui manifest");
	process.exit(1);
}

const elManifest = loadComponentManifest(path.join(root, "contracts/ai/element-plus.manifest.json"));
if (!elManifest) {
	console.error("Failed to load element-plus manifest");
	process.exit(1);
}

const manifests = [vuiManifest, elManifest];

const examplesDir = path.join(root, "contracts/ai/examples");
const targets = ["allocation-confirmation.figma-frame.json", "user-management-list.figma-frame.json"];

for (const target of targets) {
	const framePath = path.join(examplesDir, target);
	if (!fs.existsSync(framePath)) {
		console.warn(`Skip: ${target} not found`);
		continue;
	}

	const figmaFrame = JSON.parse(fs.readFileSync(framePath, "utf8"));
	const { enhancedFrame, suggestions } = enhanceFigmaFrame(figmaFrame, manifests);

	const outFile = target.replace(".figma-frame.json", ".design-mapper-suggestions.json");
	const outPath = path.join(examplesDir, outFile);

	const output = {
		$meta: {
			generator: "scripts/generate-design-mapper-examples.mjs",
			source: target,
			manifests: [vuiManifest.packageName, elManifest.packageName],
		},
		suggestions,
		enhancedFrame,
	};

	fs.writeFileSync(outPath, `${JSON.stringify(output, null, 2)}\n`, "utf8");

	// Print summary
	const s = suggestions.summary;
	const td = s.tierDistribution;
	console.log(
		`[${suggestions.frameTitle}] elements=${suggestions.totalElements}, mapped=${s.mapped}, high=${s.highConfidence}, medium=${s.mediumConfidence}, low=${s.lowConfidence}, unmapped=${s.unmapped}`,
	);
	console.log(`  tier: vui=${td.vui}, element-plus=${td.elementPlus}, html=${td.html}`);
	console.log(`  → ${outFile}`);
}

console.log("Done.");