import { cpSync, existsSync, mkdirSync } from "node:fs"; import { createRequire } from "node:module"; import { dirname, resolve } from "node:path"; const require = createRequire(import.meta.url); export function restoreTargetNodePtyPrebuild({ desktopPackageRoot, targetPlatform }) { if (targetPlatform.os !== "linux") { console.log(`[beforePack] node-pty prebuild restore skipped for ${targetPlatform.key}`); return; } const platformKey = targetPlatform.key; const sourcePackageName = `@lydell/node-pty-${platformKey}`; let sourceBinaryPath; try { sourceBinaryPath = resolveSourceNodePtyPrebuildPath({ sourcePackageName, platformKey }); } catch (error) { const message = error instanceof Error ? error.message : String(error); throw new Error(`缺少 ${sourcePackageName},无法为 ${platformKey} 打包 node-pty: ${message}`); } const nodePtyPackageRoot = dirname( require.resolve("node-pty/package.json", { paths: [desktopPackageRoot] }), ); const targetPrebuildDir = resolve(nodePtyPackageRoot, "prebuilds", platformKey); const targetBinaryPath = resolve(targetPrebuildDir, "pty.node"); // Linux 包中 node-pty 本体只会查自己的 prebuilds/linux-*/pty.node, // 但 Linux 预编译文件实际来自 @lydell/node-pty-linux-* 平台包;若排除该平台包, // 而 node-pty 自身目录没有 linux prebuild,最终安装包里会缺 pty.node,终端启动失败。 // 这里在 beforePack 阶段恢复依赖资产,让后续 asarUnpack 按标准链路处理 native addon。 mkdirSync(targetPrebuildDir, { recursive: true }); cpSync(sourceBinaryPath, targetBinaryPath); if (!existsSync(targetBinaryPath)) throw new Error(`node-pty 预编译产物恢复失败: ${targetBinaryPath}`); console.log(`[beforePack] node-pty prebuild restored: ${targetBinaryPath}`); } export function resolveSourceNodePtyPrebuildPath({ sourcePackageName, platformKey }) { const sourcePackageEntry = require.resolve(sourcePackageName); let currentDir = dirname(sourcePackageEntry); while (currentDir !== dirname(currentDir)) { const candidatePath = resolve(currentDir, "prebuilds", platformKey, "pty.node"); if (existsSync(candidatePath)) return candidatePath; currentDir = dirname(currentDir); } // @lydell/node-pty-linux-* 通过 package exports 只暴露 lib/index.js, // 不能再解析 package.json。这里从公开入口向上寻找 prebuilds,兼容 exports 限制。 throw new Error( `缺少 node-pty 预编译产物: ${sourcePackageName}/prebuilds/${platformKey}/pty.node`, ); } export function resolvePackagedNodePtyPrebuildPath({ resourcesDir, platformKey }) { return resolve( resourcesDir, "app.asar.unpacked", "node_modules", "node-pty", "prebuilds", platformKey, "pty.node", ); }