|
| 1 | +// electron-builder afterPack hook. |
| 2 | +// |
| 3 | +// node-pty ships a `spawn-helper` binary that posix_spawn invokes to set up |
| 4 | +// the pty. electron-builder's asar-unpack copy can strip the execute bit, |
| 5 | +// which makes posix_spawnp fail at runtime with the opaque |
| 6 | +// "posix_spawnp failed." error. Restore +x on every prebuild we ship. |
| 7 | + |
| 8 | +const { existsSync, statSync, chmodSync, readdirSync } = require("node:fs"); |
| 9 | +const { join } = require("node:path"); |
| 10 | + |
| 11 | +function ensureExecutable(path) { |
| 12 | + if (!existsSync(path)) return false; |
| 13 | + const stat = statSync(path); |
| 14 | + if (!stat.isFile()) return false; |
| 15 | + if ((stat.mode & 0o111) === 0o111) return false; |
| 16 | + chmodSync(path, stat.mode | 0o111); |
| 17 | + return true; |
| 18 | +} |
| 19 | + |
| 20 | +function findResourcesDir(appOutDir, electronPlatformName) { |
| 21 | + if (electronPlatformName === "darwin" || electronPlatformName === "mas") { |
| 22 | + const entries = readdirSync(appOutDir).filter((name) => name.endsWith(".app")); |
| 23 | + if (entries.length === 0) return null; |
| 24 | + return join(appOutDir, entries[0], "Contents", "Resources"); |
| 25 | + } |
| 26 | + if (electronPlatformName === "linux") { |
| 27 | + return join(appOutDir, "resources"); |
| 28 | + } |
| 29 | + if (electronPlatformName === "win32") { |
| 30 | + return join(appOutDir, "resources"); |
| 31 | + } |
| 32 | + return null; |
| 33 | +} |
| 34 | + |
| 35 | +function chmodNodePtyHelpers(resourcesDir) { |
| 36 | + const prebuildsRoot = join( |
| 37 | + resourcesDir, |
| 38 | + "app.asar.unpacked", |
| 39 | + "node_modules", |
| 40 | + "node-pty", |
| 41 | + "prebuilds", |
| 42 | + ); |
| 43 | + if (!existsSync(prebuildsRoot)) return []; |
| 44 | + const fixed = []; |
| 45 | + for (const platformDir of readdirSync(prebuildsRoot)) { |
| 46 | + const helper = join(prebuildsRoot, platformDir, "spawn-helper"); |
| 47 | + if (ensureExecutable(helper)) fixed.push(helper); |
| 48 | + } |
| 49 | + return fixed; |
| 50 | +} |
| 51 | + |
| 52 | +module.exports = async function afterPack(context) { |
| 53 | + const resourcesDir = findResourcesDir(context.appOutDir, context.electronPlatformName); |
| 54 | + if (!resourcesDir) return; |
| 55 | + const fixed = chmodNodePtyHelpers(resourcesDir); |
| 56 | + for (const path of fixed) { |
| 57 | + console.log(`[afterPack] chmod +x ${path}`); |
| 58 | + } |
| 59 | +}; |
0 commit comments