// 快速启动桌面 dev —— 与 dev-desktop-env.mjs 唯一的区别: // 当 agent bundle 比全部 CLI 源码都新时,跳过 build-desktop-agent-cli 全量重建。 // // 背景:dev:desktop 每次启动都串行重建 10 个 workspace 包 + 打包 15.9MB zcode.cjs, // 纯 UI/桌面侧改动(skins、packages/ui、packages/desktop)完全不影响 agent bundle, // 却每次都要等几分钟。本脚本用「源码最新 mtime vs bundle 最新 mtime」判断是否需要重建。 // // 安全性(防「改动没生效」静默陷阱,见 build-desktop-agent-cli.mjs:83-93 的教训): // - 比较范围覆盖 apps/zcode-cli/packages/*/src 与 */scripts、根 packages/shared/src、 // packages/rpc/src(agent bundle 依赖 @zcode/shared 与 @zcode/rpc); // - 只要任何一个源文件比 staged bundle 新 → 立即回退全量重建; // - 跳过与重建都会打印明显日志;`--force` 强制重建。 // // 用法:node scripts/dev-desktop-fast.mjs [--force] import { spawn } from "node:child_process"; import { existsSync } from "node:fs"; import { readdir, stat } from "node:fs/promises"; import { fileURLToPath } from "node:url"; import { dirname, join, resolve } from "node:path"; import { withPinnedNodePath } from "./mise-toolchain-env.mjs"; import { quoteArgsForWindowsShell } from "./spawn-command.mjs"; const requestedEnv = process.argv[2]?.trim().toLowerCase() ?? "production"; const forceRebuild = process.argv.slice(3).includes("--force"); if (requestedEnv !== "test" && requestedEnv !== "production") { console.error("Usage: node scripts/dev-desktop-fast.mjs [--force]"); process.exit(1); } const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), ".."); const pnpmCommand = process.platform === "win32" ? "pnpm.cmd" : "pnpm"; /** CLI 源码监听范围:任何文件比 bundle 新就触发重建(宁可误建,不可静默陈旧)。 */ const cliSourceWatchDirs = [ "apps/zcode-cli/packages/shared-types/src", "apps/zcode-cli/packages/contracts/src", "apps/zcode-cli/packages/dynamic-workflow/src", "apps/zcode-cli/packages/dynamic-workflow/scripts", "apps/zcode-cli/packages/dynamic-workflow-runtime/src", "apps/zcode-cli/packages/core/src", "apps/zcode-cli/packages/adapters/src", "apps/zcode-cli/packages/i18n/src", "apps/zcode-cli/packages/telemetry/src", "apps/zcode-cli/packages/bootstrap/src", "apps/zcode-cli/packages/cli/src", "apps/zcode-cli/packages/node-repl-host/src", "apps/zcode-cli/packages/browser-use-plugin/scripts", "packages/shared/src", "packages/rpc/src", ]; /** 已暂存的 agent bundle 位置(stage-agent-bundle.mjs 的产出 + cli dist 兜底)。 */ const bundleArtifactDirs = ["packages/desktop/bundled-agents"]; const bundleArtifactFiles = ["apps/zcode-cli/packages/cli/dist/zcode.cjs"]; async function newestMtime(paths, label) { let newest = 0; for (const relativePath of paths) { const absolutePath = join(repoRoot, relativePath); if (!existsSync(absolutePath)) continue; const entries = await readdir(absolutePath, { recursive: true, withFileTypes: true }); for (const entry of entries) { if (!entry.isFile()) continue; const stats = await stat(join(entry.parentPath ?? absolutePath, entry.name)); if (stats.mtimeMs > newest) newest = stats.mtimeMs; } } if (newest === 0) { console.log(`[dev-desktop-fast] ${label}: 无文件(视为需要构建)`); } return newest; } async function needsAgentRebuild() { if (forceRebuild) { console.log("[dev-desktop-fast] --force:强制重建 agent bundle"); return true; } const sourceTime = await newestMtime(cliSourceWatchDirs, "CLI 源码"); let bundleTime = await newestMtime(bundleArtifactDirs, "staged bundle"); for (const file of bundleArtifactFiles) { const absolutePath = join(repoRoot, file); if (existsSync(absolutePath)) { const stats = await stat(absolutePath); if (stats.mtimeMs > bundleTime) bundleTime = stats.mtimeMs; } } if (bundleTime === 0) return true; const needs = sourceTime > bundleTime; console.log( needs ? "[dev-desktop-fast] CLI 源码比 bundle 新 → 重建 agent bundle" : "[dev-desktop-fast] CLI 源码无变化 → 跳过 agent bundle 重建(--force 可强制)", ); return needs; } function run(command, args) { return new Promise((resolveRun, rejectRun) => { // Windows 下 shell:true 按空格拼接;仓库路径含空格时需先补引号(同 dev-desktop-env.mjs:20-22)。 const spawnArgs = process.platform === "win32" ? quoteArgsForWindowsShell(args) : args; const child = spawn(command, spawnArgs, { cwd: repoRoot, env: withPinnedNodePath( { ...process.env, ZCODE_ENV: requestedEnv, }, process.execPath, ), stdio: "inherit", shell: process.platform === "win32", }); child.on("error", rejectRun); child.on("exit", (code, signal) => { if (code === 0) { resolveRun(); return; } rejectRun( new Error( signal ? `${command} exited with signal ${signal}` : `${command} exited with code ${code ?? "unknown"}`, ), ); }); }); } // quoteArgsForWindowsShell 已改为顶部静态 import,与 dev-desktop-env.mjs 同构。 try { if (await needsAgentRebuild()) { await run(process.platform === "win32" ? "node" : process.execPath, [ resolve(repoRoot, "scripts/build-desktop-agent-cli.mjs"), ]); } // pre-dev(runtime assets + 清 out)保留:与 dev-desktop-env.mjs:60 同语义。 await run(pnpmCommand, ["--filter", "@zcode/desktop", "pre-dev"]); await run(pnpmCommand, ["--filter", "@zcode/desktop", "dev:runtime"]); } catch (error) { console.error(error instanceof Error ? error.message : error); process.exit(1); }