node-pty-rebuild.mjs 3.3 KB

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