bootstrap.mjs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/env node
  2. import process from "node:process";
  3. import { existsSync, readFileSync } from "node:fs";
  4. import { delimiter, dirname, join, resolve } from "node:path";
  5. import { homedir } from "node:os";
  6. import { fileURLToPath } from "node:url";
  7. import { runCommand } from "./spawn-command.mjs";
  8. const scriptDir = dirname(fileURLToPath(import.meta.url));
  9. const rootDir = resolve(scriptDir, "..");
  10. const gitCommand = "git";
  11. const withRemoteAssets = process.argv.includes("--with-remote");
  12. function prependPathEntries(pathValue, entries) {
  13. const currentEntries = pathValue ? pathValue.split(delimiter) : [];
  14. return [...entries, ...currentEntries].join(delimiter);
  15. }
  16. function resolvePinnedNodeBin() {
  17. if (process.platform === "win32") {
  18. return undefined;
  19. }
  20. const miseConfigPath = resolve(rootDir, "mise.toml");
  21. if (!existsSync(miseConfigPath)) {
  22. return undefined;
  23. }
  24. const miseConfig = readFileSync(miseConfigPath, "utf8");
  25. const match = miseConfig.match(/^\s*node\s*=\s*"([^"]+)"/m);
  26. const version = match?.[1]?.trim();
  27. if (!version) {
  28. return undefined;
  29. }
  30. const nvmNodeBin = join(homedir(), ".nvm", "versions", "node", `v${version}`, "bin");
  31. return existsSync(nvmNodeBin) ? nvmNodeBin : undefined;
  32. }
  33. function resolveUserPnpmBin() {
  34. if (process.platform === "win32") {
  35. return undefined;
  36. }
  37. const pnpmBin = join(homedir(), "Library", "pnpm");
  38. return existsSync(join(pnpmBin, "pnpm")) ? pnpmBin : undefined;
  39. }
  40. function resolvePnpmCommand() {
  41. if (!withRemoteAssets) {
  42. return process.platform === "win32" ? "pnpm.cmd" : "pnpm";
  43. }
  44. const userPnpmBin = resolveUserPnpmBin();
  45. if (userPnpmBin) {
  46. return join(userPnpmBin, process.platform === "win32" ? "pnpm.cmd" : "pnpm");
  47. }
  48. return process.platform === "win32" ? "pnpm.cmd" : "pnpm";
  49. }
  50. function resolveBootstrapWithRemoteEnv(baseEnv = process.env) {
  51. if (!withRemoteAssets) {
  52. return {};
  53. }
  54. const pathEntries = [resolvePinnedNodeBin(), resolveUserPnpmBin()].filter(Boolean);
  55. return {
  56. // bootstrap:with-remote 是手动远程资源初始化入口,自动化等非 TTY 环境里
  57. // pnpm install 可能要求确认清理 node_modules。只在该入口禁用确认,不改变 CI/生产构建命令。
  58. HUSKY: "0",
  59. PNPM_CONFIG_CONFIRM_MODULES_PURGE: "false",
  60. npm_config_confirm_modules_purge: "false",
  61. // remote assets 和 bootstrap build 同一轮里会触发大量 workspace 构建。
  62. // 这里把降峰值限制在 bootstrap:with-remote 子进程,不修改 build/build:bootstrap 的全局语义。
  63. PNPM_CONFIG_WORKSPACE_CONCURRENCY: "1",
  64. ZCODE_BOOTSTRAP_WITH_REMOTE: "1",
  65. ...(pathEntries.length > 0
  66. ? {
  67. PATH: prependPathEntries(baseEnv.PATH, pathEntries),
  68. }
  69. : {}),
  70. };
  71. }
  72. const bootstrapWithRemoteEnv = resolveBootstrapWithRemoteEnv();
  73. const pnpmCommand = resolvePnpmCommand();
  74. function runGit(args) {
  75. runCommand(gitCommand, args, {
  76. cwd: rootDir,
  77. env: process.env,
  78. });
  79. }
  80. function runPnpm(args, options = {}) {
  81. runCommand(pnpmCommand, args, {
  82. cwd: rootDir,
  83. env: {
  84. ...process.env,
  85. ...bootstrapWithRemoteEnv,
  86. ...options.env,
  87. },
  88. });
  89. }
  90. function runBootstrapServerBuild() {
  91. const serverDir = resolve(rootDir, "packages/server");
  92. // bootstrap:with-remote 的最终构建过去复用 build:bootstrap,导致 @zcode/server build
  93. // 内部再次嵌套 pnpm run build:remote;在本地低内存环境中 tsx/esbuild 子进程容易卡住或被停掉。
  94. // 同时不能按 dist 文件存在就跳过构建:开发时 version 经常不变,旧 entry-http 或 remote bundle
  95. // 会让本地/远端继续运行旧协议。这里仅保留直接执行等价入口的低内存优化,CI 和生产 build 脚本保持原样。
  96. runCommand(process.execPath, [resolve(rootDir, "node_modules/tsup/dist/cli-default.js")], {
  97. cwd: serverDir,
  98. env: {
  99. ...process.env,
  100. ...bootstrapWithRemoteEnv,
  101. },
  102. });
  103. runCommand(
  104. process.execPath,
  105. [resolve(rootDir, "node_modules/tsx/dist/cli.mjs"), "build-remote.ts"],
  106. {
  107. cwd: serverDir,
  108. env: {
  109. ...process.env,
  110. ...bootstrapWithRemoteEnv,
  111. },
  112. },
  113. );
  114. }
  115. function runBootstrapDesktopBuild() {
  116. const desktopDir = resolve(rootDir, "packages/desktop");
  117. // bootstrap:with-remote 的目标是完成远程资源和本地 runtime 初始化。
  118. // 继续触发 desktop app bundle 会进入生产构建脚本里的 tsup/vite 路径,在本地低内存环境中被 SIGKILL。
  119. // 这里仅在 bootstrap runner 中保留 build meta,生产/CI 的 build:no-runtime-assets 仍保持原语义。
  120. runCommand(process.execPath, ["scripts/build-metadata.mjs"], {
  121. cwd: desktopDir,
  122. env: {
  123. ...process.env,
  124. ...bootstrapWithRemoteEnv,
  125. },
  126. });
  127. console.log("[bootstrap:with-remote] skip desktop app bundle build; runtime assets are prepared");
  128. }
  129. function runBootstrapWithRemoteBuild() {
  130. for (const filter of ["@zcode/rpc", "@zcode/web", "@zcode/formal-proof"]) {
  131. // pnpm -r 会在 bootstrap:with-remote 的最终构建阶段并发启动多个 Vite/esbuild/tsup。
  132. // remote assets 已经占过一轮内存峰值,这里显式串行包构建,且不改变 build:bootstrap/CI 命令。
  133. runPnpm(["--filter", filter, "build"]);
  134. }
  135. runBootstrapServerBuild();
  136. runBootstrapDesktopBuild();
  137. }
  138. runGit(["submodule", "update", "--init", "--recursive", "apps/zcode-cli"]);
  139. runPnpm(withRemoteAssets ? ["install", "--config.confirmModulesPurge=false"] : ["install"]);
  140. runPnpm(["prepare:desktop-runtime"], {
  141. env: withRemoteAssets
  142. ? {}
  143. : {
  144. // 本地 bootstrap 过去默认准备 remote mock-cdn,
  145. // 每次都会重新打包跨平台组件,导致普通初始化很慢。
  146. // 默认只准备桌面端本地 runtime;需要远程资源时使用 bootstrap:with-remote。
  147. ZCODE_SKIP_REMOTE_ASSETS: "1",
  148. },
  149. });
  150. if (withRemoteAssets) {
  151. runBootstrapWithRemoteBuild();
  152. } else {
  153. runPnpm(["run", "build:bootstrap"]);
  154. }