dev-desktop-env.mjs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { spawn } from "node:child_process";
  2. import { fileURLToPath } from "node:url";
  3. import { dirname, resolve } from "node:path";
  4. import { withPinnedNodePath } from "./mise-toolchain-env.mjs";
  5. import { quoteArgsForWindowsShell } from "./spawn-command.mjs";
  6. const requestedEnv = process.argv[2]?.trim().toLowerCase();
  7. const agentBytecode = process.argv.slice(3).includes("--agent-bytecode");
  8. if (requestedEnv !== "test" && requestedEnv !== "production") {
  9. console.error("Usage: node scripts/dev-desktop-env.mjs <test|production> [--agent-bytecode]");
  10. process.exit(1);
  11. }
  12. const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
  13. const pnpmCommand = process.platform === "win32" ? "pnpm.cmd" : "pnpm";
  14. function run(command, args) {
  15. return new Promise((resolveRun, rejectRun) => {
  16. // Windows 下 shell:true 只按空格拼接参数;仓库路径含空格(如 E:\Z Code\...)时
  17. // node <script> 的脚本路径会被 cmd 截断成 E:\Z 并报 Cannot find module,因此先补引号。
  18. const spawnArgs = process.platform === "win32" ? quoteArgsForWindowsShell(args) : args;
  19. const child = spawn(command, spawnArgs, {
  20. cwd: repoRoot,
  21. env: withPinnedNodePath(
  22. {
  23. ...process.env,
  24. ZCODE_ENV: requestedEnv,
  25. ZCODE_DESKTOP_AGENT_BYTECODE: agentBytecode ? "1" : "0",
  26. },
  27. process.execPath,
  28. ),
  29. stdio: "inherit",
  30. // Windows .cmd/.bat executables (pnpm.cmd, npm.cmd, etc.) require shell: true
  31. shell: process.platform === "win32",
  32. });
  33. child.on("error", rejectRun);
  34. child.on("exit", (code, signal) => {
  35. if (code === 0) {
  36. resolveRun();
  37. return;
  38. }
  39. rejectRun(
  40. new Error(
  41. signal
  42. ? `${command} exited with signal ${signal}`
  43. : `${command} exited with code ${code ?? "unknown"}`,
  44. ),
  45. );
  46. });
  47. });
  48. }
  49. try {
  50. // The public dev scripts delegate here instead of invoking the package's
  51. // `dev` lifecycle directly, so pnpm will not run `pre-dev` automatically.
  52. // Preserve its runtime-asset preparation and stale `out` cleanup explicitly
  53. // before rebuilding bundles or starting Electron.
  54. await run(pnpmCommand, ["--filter", "@zcode/desktop", "pre-dev"]);
  55. // On Windows, use "node" (resolved via PATHEXT) to avoid "C:\Program Files\..." space issues
  56. await run(process.platform === "win32" ? "node" : process.execPath, [
  57. resolve(repoRoot, "scripts/build-desktop-agent-cli.mjs"),
  58. ]);
  59. if (agentBytecode) {
  60. await run(process.platform === "win32" ? "node" : process.execPath, [
  61. resolve(repoRoot, "scripts/build-desktop-agent-bytecode.mjs"),
  62. ]);
  63. }
  64. await run(pnpmCommand, ["--filter", "@zcode/desktop", "dev:runtime"]);
  65. } catch (error) {
  66. console.error(error instanceof Error ? error.message : error);
  67. process.exit(1);
  68. }