import { build } from "esbuild"; import { mkdtemp, mkdir, readFile, writeFile, readdir, copyFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { dirname, join, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { createRequire } from "node:module"; import { spawn, execFile } from "node:child_process"; import { promisify } from "node:util"; import { copyGenUiRuntimeAssets } from "./gen-ui-runtime-assets.mjs"; const require = createRequire(import.meta.url); const desktop = resolve(dirname(fileURLToPath(import.meta.url)), ".."); const root = resolve(desktop, "../.."); const fixture = join(desktop, "scripts/gen-ui-fixture"); const output = await mkdtemp(join(tmpdir(), "zcode-gen-ui-e2e-")); await copyGenUiRuntimeAssets(output); const shared = JSON.parse(await readFile(join(root, "packages/shared/package.json"), "utf8")); const alias = { "@": join(root, "packages/ui/src"), ...Object.fromEntries( Object.entries(shared.exports).map(([key, path]) => [ `@zcode/shared${key === "." ? "" : key.slice(1)}`, resolve(root, "packages/shared", path), ]), ), }; const bundle = (entry, name, platform = "browser") => build({ entryPoints: [entry], outfile: join(output, name), bundle: true, platform, jsx: "automatic", format: platform === "node" ? "cjs" : "iife", alias, external: ["electron"], loader: { ".png": "dataurl", ".svg": "dataurl" }, plugins: [ { name: "fixture-url-assets", setup(builder) { builder.onResolve({ filter: /\?raw$/ }, (args) => ({ path: resolve(args.resolveDir, args.path.slice(0, -4)), namespace: "fixture-raw", })); builder.onLoad({ filter: /.*/, namespace: "fixture-raw" }, async (args) => ({ contents: await readFile(args.path, "utf8"), loader: "text", })); builder.onResolve({ filter: /\?url$/ }, (args) => ({ path: require.resolve(args.path.slice(0, -4), { paths: [args.resolveDir] }), namespace: "fixture-url", })); builder.onLoad({ filter: /.*/, namespace: "fixture-url" }, async (args) => ({ contents: await readFile(args.path), loader: "file", })); }, }, ], define: { "process.env.NODE_ENV": '"production"', "import.meta.env": '{"DEV":false,"PROD":true,"BASE_URL":"./"}', }, logLevel: "warning", }); await mkdir(join(output, "assets")); await Promise.all([ bundle(join(fixture, "main.ts"), "main.cjs", "node"), bundle(join(fixture, "preload.ts"), "preload.cjs", "node"), bundle(join(fixture, "renderer.ts"), "host.js"), bundle(join(fixture, "react.tsx"), "react.js"), bundle(join(desktop, "src/preload/pluginSandbox/index.ts"), "guest.cjs", "node"), bundle(join(desktop, "src/renderer/src/plugin-sandbox/main.ts"), "assets/shell.js"), bundle(join(desktop, "src/renderer/src/plugin-sandbox/genUi.ts"), "gen-ui.js"), ]); await writeFile( join(output, "host.html"), 'ZCode Gen UI test
', ); await writeFile( join(output, "plugin-sandbox.html"), '
', ); await writeFile( join(output, "demo.html"), `

Original heading

Interactive Gen UI

First panel
Second panel
Adjustable card
Object binding
`, ); const cssFiles = (await readdir(join(desktop, "out/renderer/assets"))).filter((name) => name.endsWith(".css"), ); if (!cssFiles.length) throw new Error( "Build the desktop renderer before this E2E: pnpm --dir packages/desktop exec vite build", ); await Promise.all( cssFiles.map((name) => copyFile(join(desktop, "out/renderer/assets", name), join(output, name))), ); // 正式正文会加载含 Unicode 正则的 Markdown runtime;fixture 也必须声明 UTF-8,避免本地文件按旧编码解析脚本。 await writeFile( join(output, "react.html"), `${cssFiles.map((name) => ``).join("")}
`, ); process.stdout.write(`Gen UI E2E artifacts: ${output}\n`); await writeFile( join(output, "standalone-fragment.html"), '

Standalone state

', ); await promisify(execFile)("python3", [ join(root, "apps/zcode-cli/packages/visualize-plugin/skills/visualize/scripts/render.py"), join(output, "standalone-fragment.html"), join(output, "standalone.html"), ]); const child = spawn(require("electron"), [join(output, "main.cjs"), output], { stdio: "inherit", env: { ...process.env, ELECTRON_RUN_AS_NODE: "" }, }); const exitCode = await new Promise((resolveExit, reject) => { child.on("error", reject); child.on("exit", (code) => resolveExit(code ?? 1)); }); process.exitCode = exitCode;