dev-desktop-fast.mjs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // 快速启动桌面 dev —— 与 dev-desktop-env.mjs 唯一的区别:
  2. // 当 agent bundle 比全部 CLI 源码都新时,跳过 build-desktop-agent-cli 全量重建。
  3. //
  4. // 背景:dev:desktop 每次启动都串行重建 10 个 workspace 包 + 打包 15.9MB zcode.cjs,
  5. // 纯 UI/桌面侧改动(skins、packages/ui、packages/desktop)完全不影响 agent bundle,
  6. // 却每次都要等几分钟。本脚本用「源码最新 mtime vs bundle 最新 mtime」判断是否需要重建。
  7. //
  8. // 安全性(防「改动没生效」静默陷阱,见 build-desktop-agent-cli.mjs:83-93 的教训):
  9. // - 比较范围覆盖 apps/zcode-cli/packages/*/src 与 */scripts、根 packages/shared/src、
  10. // packages/rpc/src(agent bundle 依赖 @zcode/shared 与 @zcode/rpc);
  11. // - 只要任何一个源文件比 staged bundle 新 → 立即回退全量重建;
  12. // - 跳过与重建都会打印明显日志;`--force` 强制重建。
  13. //
  14. // 用法:node scripts/dev-desktop-fast.mjs <test|production> [--force]
  15. import { spawn } from "node:child_process";
  16. import { existsSync } from "node:fs";
  17. import { readdir, stat } from "node:fs/promises";
  18. import { fileURLToPath } from "node:url";
  19. import { dirname, join, resolve } from "node:path";
  20. import { withPinnedNodePath } from "./mise-toolchain-env.mjs";
  21. import { quoteArgsForWindowsShell } from "./spawn-command.mjs";
  22. const requestedEnv = process.argv[2]?.trim().toLowerCase() ?? "production";
  23. const forceRebuild = process.argv.slice(3).includes("--force");
  24. if (requestedEnv !== "test" && requestedEnv !== "production") {
  25. console.error("Usage: node scripts/dev-desktop-fast.mjs <test|production> [--force]");
  26. process.exit(1);
  27. }
  28. const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
  29. const pnpmCommand = process.platform === "win32" ? "pnpm.cmd" : "pnpm";
  30. /** CLI 源码监听范围:任何文件比 bundle 新就触发重建(宁可误建,不可静默陈旧)。 */
  31. const cliSourceWatchDirs = [
  32. "apps/zcode-cli/packages/shared-types/src",
  33. "apps/zcode-cli/packages/contracts/src",
  34. "apps/zcode-cli/packages/dynamic-workflow/src",
  35. "apps/zcode-cli/packages/dynamic-workflow/scripts",
  36. "apps/zcode-cli/packages/dynamic-workflow-runtime/src",
  37. "apps/zcode-cli/packages/core/src",
  38. "apps/zcode-cli/packages/adapters/src",
  39. "apps/zcode-cli/packages/i18n/src",
  40. "apps/zcode-cli/packages/telemetry/src",
  41. "apps/zcode-cli/packages/bootstrap/src",
  42. "apps/zcode-cli/packages/cli/src",
  43. "apps/zcode-cli/packages/node-repl-host/src",
  44. "apps/zcode-cli/packages/browser-use-plugin/scripts",
  45. "packages/shared/src",
  46. "packages/rpc/src",
  47. ];
  48. /** 已暂存的 agent bundle 位置(stage-agent-bundle.mjs 的产出 + cli dist 兜底)。 */
  49. const bundleArtifactDirs = ["packages/desktop/bundled-agents"];
  50. const bundleArtifactFiles = ["apps/zcode-cli/packages/cli/dist/zcode.cjs"];
  51. async function newestMtime(paths, label) {
  52. let newest = 0;
  53. for (const relativePath of paths) {
  54. const absolutePath = join(repoRoot, relativePath);
  55. if (!existsSync(absolutePath)) continue;
  56. const entries = await readdir(absolutePath, { recursive: true, withFileTypes: true });
  57. for (const entry of entries) {
  58. if (!entry.isFile()) continue;
  59. const stats = await stat(join(entry.parentPath ?? absolutePath, entry.name));
  60. if (stats.mtimeMs > newest) newest = stats.mtimeMs;
  61. }
  62. }
  63. if (newest === 0) {
  64. console.log(`[dev-desktop-fast] ${label}: 无文件(视为需要构建)`);
  65. }
  66. return newest;
  67. }
  68. async function needsAgentRebuild() {
  69. if (forceRebuild) {
  70. console.log("[dev-desktop-fast] --force:强制重建 agent bundle");
  71. return true;
  72. }
  73. const sourceTime = await newestMtime(cliSourceWatchDirs, "CLI 源码");
  74. let bundleTime = await newestMtime(bundleArtifactDirs, "staged bundle");
  75. for (const file of bundleArtifactFiles) {
  76. const absolutePath = join(repoRoot, file);
  77. if (existsSync(absolutePath)) {
  78. const stats = await stat(absolutePath);
  79. if (stats.mtimeMs > bundleTime) bundleTime = stats.mtimeMs;
  80. }
  81. }
  82. if (bundleTime === 0) return true;
  83. const needs = sourceTime > bundleTime;
  84. console.log(
  85. needs
  86. ? "[dev-desktop-fast] CLI 源码比 bundle 新 → 重建 agent bundle"
  87. : "[dev-desktop-fast] CLI 源码无变化 → 跳过 agent bundle 重建(--force 可强制)",
  88. );
  89. return needs;
  90. }
  91. function run(command, args) {
  92. return new Promise((resolveRun, rejectRun) => {
  93. // Windows 下 shell:true 按空格拼接;仓库路径含空格时需先补引号(同 dev-desktop-env.mjs:20-22)。
  94. const spawnArgs = process.platform === "win32" ? quoteArgsForWindowsShell(args) : args;
  95. const child = spawn(command, spawnArgs, {
  96. cwd: repoRoot,
  97. env: withPinnedNodePath(
  98. {
  99. ...process.env,
  100. ZCODE_ENV: requestedEnv,
  101. },
  102. process.execPath,
  103. ),
  104. stdio: "inherit",
  105. shell: process.platform === "win32",
  106. });
  107. child.on("error", rejectRun);
  108. child.on("exit", (code, signal) => {
  109. if (code === 0) {
  110. resolveRun();
  111. return;
  112. }
  113. rejectRun(
  114. new Error(
  115. signal
  116. ? `${command} exited with signal ${signal}`
  117. : `${command} exited with code ${code ?? "unknown"}`,
  118. ),
  119. );
  120. });
  121. });
  122. }
  123. // quoteArgsForWindowsShell 已改为顶部静态 import,与 dev-desktop-env.mjs 同构。
  124. try {
  125. if (await needsAgentRebuild()) {
  126. await run(process.platform === "win32" ? "node" : process.execPath, [
  127. resolve(repoRoot, "scripts/build-desktop-agent-cli.mjs"),
  128. ]);
  129. }
  130. // pre-dev(runtime assets + 清 out)保留:与 dev-desktop-env.mjs:60 同语义。
  131. await run(pnpmCommand, ["--filter", "@zcode/desktop", "pre-dev"]);
  132. await run(pnpmCommand, ["--filter", "@zcode/desktop", "dev:runtime"]);
  133. } catch (error) {
  134. console.error(error instanceof Error ? error.message : error);
  135. process.exit(1);
  136. }