gen-ui-runtime-assets.mjs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { copyFile, mkdir, readFile } from "node:fs/promises";
  2. import { createHash } from "node:crypto";
  3. import { join } from "node:path";
  4. const source = new URL(
  5. "../../../apps/zcode-cli/packages/visualize-plugin/skills/visualize/assets/",
  6. import.meta.url,
  7. );
  8. /** Inline 和导出共用实际资源文件,避免维护另一份有细节差异的运行时。 */
  9. export async function copyGenUiRuntimeAssets(destination) {
  10. const manifest = JSON.parse(await readFile(new URL("vendor/manifest.json", source), "utf8"));
  11. const vendorFiles = new Map();
  12. for (const resource of manifest.resources) {
  13. vendorFiles.set(resource.file, resource.sha256);
  14. vendorFiles.set(resource.licenseFile, resource.licenseSha256);
  15. }
  16. // 离线运行依赖完整的固定快照;构建时发现缺失或漂移,不能把失败留到用户首次打开。
  17. await Promise.all(
  18. [...vendorFiles].map(async ([file, expectedHash]) => {
  19. if (!/^[a-zA-Z0-9.-]+$/.test(file)) throw new Error(`Invalid Gen UI vendor file: ${file}`);
  20. const bytes = await readFile(new URL(`vendor/${file}`, source));
  21. if (createHash("sha256").update(bytes).digest("hex") !== expectedHash)
  22. throw new Error(`Gen UI vendor hash mismatch: ${file}`);
  23. }),
  24. );
  25. await mkdir(destination, { recursive: true });
  26. await mkdir(join(destination, "vendor"), { recursive: true });
  27. await Promise.all(
  28. [
  29. "visualize.css",
  30. "visualize.html",
  31. "calendar.js",
  32. "vendor/manifest.json",
  33. ...[...vendorFiles.keys()].map((file) => `vendor/${file}`),
  34. ].map((file) => copyFile(new URL(file, source), join(destination, file))),
  35. );
  36. }