generate-allocation-confirmation-example.mjs 3.5 KB
import fs from "node:fs";
import path from "node:path";

import { generateVueModule, writeGeneratedFiles } from "../packages/ai/codegen/src/index.mjs";
import {
	mcpDesignContextToFigmaFrame,
	mcpDesignContextToPageSchema,
	toJson,
} from "../packages/ai/figma-ingest/src/index.mjs";
import { generateIncrementalPatchBundle, writeIncrementalPatchBundle } from "../packages/ai/patcher/src/index.mjs";

const workspaceRoot = path.resolve(import.meta.dirname, "..");
const figmaUrl =
	"https://www.figma.com/design/taBZMgXMmkHYUEDwb4ROMX/202604-%E8%B0%83%E6%8B%A8%E7%AE%A1%E7%90%86?node-id=1-1592&m=dev";

const designContext = `
<div data-node-id="1:1592">
  <div data-node-id="1:1593"><p>调拨确认单</p></div>
  <div className="bg-[#f4f6fc] border-b border-solid"><p>序号</p></div>
  <div className="bg-[#f4f6fc] border-b border-solid"><p>商品名称</p></div>
  <div className="bg-[#f4f6fc] border-b border-solid"><p>规格</p></div>
  <div className="bg-[#f4f6fc] border-b border-solid"><p>调拨数量</p></div>
  <div className="bg-[#f4f6fc] border-b border-solid"><p>调出仓</p></div>
  <div className="bg-[#f4f6fc] border-b border-solid"><p>调入仓</p></div>
  <div><p>1</p></div>
  <div><p>商品名称商品名称 商品名称</p></div>
  <div><p>A/B</p></div>
  <div><p>20</p></div>
  <div><p>杭州仓</p></div>
  <div><p>上海仓</p></div>
</div>
`.trim();

const options = {
	figmaUrl,
	frameId: "allocation-confirmation",
	title: "调拨确认单",
	primaryBlockName: "调拨明细表",
	route: {
		path: "/allocation/confirmation",
		name: "AllocationConfirmation",
		menuTitle: "调拨确认单",
		requiresAuth: true,
	},
	notes: "Generated from the real Figma MCP node 1:1592 for the allocation confirmation pilot.",
};

const figmaFrame = mcpDesignContextToFigmaFrame(designContext, options);
figmaFrame.$schema = "../figma-frame.schema.json";

const pageSchema = mcpDesignContextToPageSchema(designContext, options);
const codegenProjectContext = JSON.parse(
	fs.readFileSync(path.join(workspaceRoot, "contracts/ai/examples/vui-admin.project-context.json"), "utf8"),
);
const patchProjectContext = JSON.parse(
	fs.readFileSync(path.join(workspaceRoot, "contracts/ai/examples/example.project-context.json"), "utf8"),
);
const generatedFiles = generateVueModule(pageSchema, codegenProjectContext);
const patchBundle = generateIncrementalPatchBundle(pageSchema, patchProjectContext);

const framePath = path.join(workspaceRoot, "contracts/ai/examples/allocation-confirmation.figma-frame.json");
const pageSchemaPath = path.join(
	workspaceRoot,
	"contracts/ai/examples/allocation-confirmation.from-figma.page-schema.json",
);
const patchPlanPath = path.join(
	workspaceRoot,
	"contracts/ai/examples/allocation-confirmation.example.incremental-patch-plan.json",
);
const outputRoot = path.join(
	workspaceRoot,
	"packages/ai/codegen/examples/generated-from-figma-allocation-confirmation",
);
const patchOutputRoot = path.join(
	workspaceRoot,
	"packages/ai/patcher/examples/generated-from-figma-allocation-confirmation-example",
);

fs.writeFileSync(framePath, toJson(figmaFrame), "utf8");
fs.writeFileSync(pageSchemaPath, toJson(pageSchema), "utf8");
fs.writeFileSync(patchPlanPath, toJson(patchBundle.plan), "utf8");
writeGeneratedFiles(outputRoot, generatedFiles);
writeIncrementalPatchBundle(patchOutputRoot, patchBundle);

console.log(
	JSON.stringify(
		{
			framePath,
			pageSchemaPath,
			patchPlanPath,
			generatedFiles: generatedFiles.map((file) => file.path),
			patchFiles: patchBundle.files.map((file) => file.path),
		},
		null,
		2,
	),
);