bootstrap.mjs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 runBootstrapE2eReportBuild() {
  116. const e2eReportDir = resolve(rootDir, "packages/e2e-report");
  117. // Bugfix: bootstrap:with-remote 的最终构建不能再嵌套 pnpm build:node。
  118. // 低内存环境中 pnpm shim + tsc 会被 SIGKILL;这里只在 bootstrap runner 中直接执行等价入口。
  119. runCommand(
  120. process.execPath,
  121. [resolve(rootDir, "node_modules/typescript/bin/tsc"), "-p", "tsconfig.json"],
  122. {
  123. cwd: e2eReportDir,
  124. env: {
  125. ...process.env,
  126. ...bootstrapWithRemoteEnv,
  127. },
  128. },
  129. );
  130. runCommand(process.execPath, [resolve(rootDir, "node_modules/vite/bin/vite.js"), "build"], {
  131. cwd: e2eReportDir,
  132. env: {
  133. ...process.env,
  134. ...bootstrapWithRemoteEnv,
  135. },
  136. });
  137. }
  138. function runBootstrapStreamAnimateBuild() {
  139. const streamAnimateDir = resolve(rootDir, "packages/stream-animate");
  140. // Bugfix: bootstrap:with-remote 串行最终构建时,继续通过 pnpm 执行 stream-animate 的 tsc
  141. // 仍可能命中 shim 被 SIGKILL;这里仅在 bootstrap runner 中直接执行等价入口。
  142. runCommand(
  143. process.execPath,
  144. [resolve(rootDir, "node_modules/typescript/bin/tsc"), "-p", "tsconfig.server.json"],
  145. {
  146. cwd: streamAnimateDir,
  147. env: {
  148. ...process.env,
  149. ...bootstrapWithRemoteEnv,
  150. },
  151. },
  152. );
  153. runCommand(process.execPath, [resolve(rootDir, "node_modules/vite/bin/vite.js"), "build"], {
  154. cwd: streamAnimateDir,
  155. env: {
  156. ...process.env,
  157. ...bootstrapWithRemoteEnv,
  158. },
  159. });
  160. }
  161. function runBootstrapDesktopBuild() {
  162. const desktopDir = resolve(rootDir, "packages/desktop");
  163. // bootstrap:with-remote 的目标是完成远程资源和本地 runtime 初始化。
  164. // 继续触发 desktop app bundle 会进入生产构建脚本里的 tsup/vite 路径,在本地低内存环境中被 SIGKILL。
  165. // 这里仅在 bootstrap runner 中保留 build meta,生产/CI 的 build:no-runtime-assets 仍保持原语义。
  166. runCommand(process.execPath, ["scripts/build-metadata.mjs"], {
  167. cwd: desktopDir,
  168. env: {
  169. ...process.env,
  170. ...bootstrapWithRemoteEnv,
  171. },
  172. });
  173. console.log("[bootstrap:with-remote] skip desktop app bundle build; runtime assets are prepared");
  174. }
  175. function runBootstrapWithRemoteBuild() {
  176. for (const filter of ["@zcode/rpc", "@zcode/web", "@zcode/formal-proof"]) {
  177. // pnpm -r 会在 bootstrap:with-remote 的最终构建阶段并发启动多个 Vite/esbuild/tsup。
  178. // remote assets 已经占过一轮内存峰值,这里显式串行包构建,且不改变 build:bootstrap/CI 命令。
  179. runPnpm(["--filter", filter, "build"]);
  180. }
  181. runBootstrapE2eReportBuild();
  182. runBootstrapStreamAnimateBuild();
  183. runBootstrapServerBuild();
  184. runBootstrapDesktopBuild();
  185. }
  186. runGit(["submodule", "update", "--init", "--recursive", "apps/zcode-cli"]);
  187. runPnpm(withRemoteAssets ? ["install", "--config.confirmModulesPurge=false"] : ["install"]);
  188. runPnpm(["prepare:desktop-runtime"], {
  189. env: withRemoteAssets
  190. ? {}
  191. : {
  192. // 本地 bootstrap 过去默认准备 remote mock-cdn,
  193. // 每次都会重新打包跨平台组件,导致普通初始化很慢。
  194. // 默认只准备桌面端本地 runtime;需要远程资源时使用 bootstrap:with-remote。
  195. ZCODE_SKIP_REMOTE_ASSETS: "1",
  196. },
  197. });
  198. if (withRemoteAssets) {
  199. runBootstrapWithRemoteBuild();
  200. } else {
  201. runPnpm(["run", "build:bootstrap"]);
  202. }