node-pty-rebuild.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { spawn } from "node:child_process";
  2. import { access } from "node:fs/promises";
  3. import { dirname, join, normalize, posix as posixPath, sep } from "node:path";
  4. import process from "node:process";
  5. import { fileURLToPath } from "node:url";
  6. import { createRequire } from "node:module";
  7. import { ensureElectronRuntime } from "./ensure-electron-runtime.mjs";
  8. const require = createRequire(import.meta.url);
  9. function normalizePathForComparison(filePath) {
  10. return normalize(filePath).split(sep).join("/");
  11. }
  12. export function getNodePtyPrebuildPath({ platform, arch, nodePtyRoot }) {
  13. const normalizedRoot = normalizePathForComparison(nodePtyRoot);
  14. return posixPath.join(normalizedRoot, "prebuilds", `${platform}-${arch}`, "pty.node");
  15. }
  16. export function decideNodePtyRebuild({ platform, arch, nodePtyRoot, existingPaths }) {
  17. const prebuildPath = getNodePtyPrebuildPath({ platform, arch, nodePtyRoot });
  18. if (platform !== "win32") {
  19. return {
  20. shouldRebuild: true,
  21. prebuildPath,
  22. reason: "non-windows-platform",
  23. };
  24. }
  25. if (existingPaths.has(prebuildPath)) {
  26. return {
  27. shouldRebuild: false,
  28. prebuildPath,
  29. reason: "windows-prebuild-available",
  30. };
  31. }
  32. return {
  33. shouldRebuild: true,
  34. prebuildPath,
  35. reason: "windows-prebuild-missing",
  36. };
  37. }
  38. export function resolveNodePtyRoot(cwd = process.cwd()) {
  39. const nodePtyPackageJsonPath = require.resolve("node-pty/package.json", { paths: [cwd] });
  40. return dirname(nodePtyPackageJsonPath);
  41. }
  42. async function pathExists(path) {
  43. try {
  44. await access(path);
  45. return true;
  46. } catch {
  47. return false;
  48. }
  49. }
  50. export async function resolveNodePtyRebuildDecision({
  51. platform = process.platform,
  52. arch = process.arch,
  53. cwd = process.cwd(),
  54. } = {}) {
  55. const nodePtyRoot = resolveNodePtyRoot(cwd);
  56. const prebuildPath = getNodePtyPrebuildPath({ platform, arch, nodePtyRoot });
  57. const existingPaths = new Set((await pathExists(prebuildPath)) ? [prebuildPath] : []);
  58. return decideNodePtyRebuild({
  59. platform,
  60. arch,
  61. nodePtyRoot,
  62. existingPaths,
  63. });
  64. }
  65. function runElectronRebuild() {
  66. return new Promise((resolve, reject) => {
  67. const command = process.platform === "win32" ? "electron-rebuild.cmd" : "electron-rebuild";
  68. const child = spawn(command, ["-o", "node-pty"], {
  69. cwd: process.cwd(),
  70. stdio: "inherit",
  71. });
  72. child.on("error", reject);
  73. child.on("exit", (code, signal) => {
  74. if (code === 0) {
  75. resolve();
  76. return;
  77. }
  78. reject(
  79. new Error(
  80. `electron-rebuild failed with code ${code ?? "null"} and signal ${signal ?? "null"}`,
  81. ),
  82. );
  83. });
  84. });
  85. }
  86. export async function main() {
  87. await ensureElectronRuntime();
  88. const decision = await resolveNodePtyRebuildDecision();
  89. if (!decision.shouldRebuild) {
  90. // electron-rebuild 会在 Windows 强制 node-gyp build-from-source。
  91. // node-pty 1.1 已自带 Electron 可复用的 N-API 预编译产物,继续本地编译只会把 CI 绑到 VS 的 Spectre 库上,
  92. // 导致安装阶段直接失败,所以这里命中预编译时跳过 rebuild。
  93. console.log(`[node-pty] skip electron-rebuild (${decision.reason}): ${decision.prebuildPath}`);
  94. return;
  95. }
  96. console.log(`[node-pty] run electron-rebuild (${decision.reason}): ${decision.prebuildPath}`);
  97. await runElectronRebuild();
  98. }
  99. const currentFilePath = fileURLToPath(import.meta.url);
  100. if (process.argv[1] && currentFilePath === process.argv[1]) {
  101. await main();
  102. }