|
| 1 | +const fs = require("node:fs"); |
| 2 | +const path = require("node:path"); |
| 3 | + |
| 4 | +const EXECUTABLE_MASK = 0o111; |
| 5 | +const NODE_PTY_HELPER_PATH_PATCHES = [ |
| 6 | + { |
| 7 | + from: "helperPath = helperPath.replace('app.asar', 'app.asar.unpacked');", |
| 8 | + to: "helperPath = helperPath.replace(/app\\.asar(?!\\.unpacked)/, 'app.asar.unpacked');", |
| 9 | + }, |
| 10 | + { |
| 11 | + from: "helperPath = helperPath.replace('node_modules.asar', 'node_modules.asar.unpacked');", |
| 12 | + to: "helperPath = helperPath.replace(/node_modules\\.asar(?!\\.unpacked)/, 'node_modules.asar.unpacked');", |
| 13 | + }, |
| 14 | +]; |
| 15 | + |
| 16 | +function pathExists(targetPath) { |
| 17 | + try { |
| 18 | + fs.accessSync(targetPath); |
| 19 | + return true; |
| 20 | + } catch { |
| 21 | + return false; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +function listDirectories(rootPath) { |
| 26 | + if (!pathExists(rootPath)) return []; |
| 27 | + return fs.readdirSync(rootPath, { withFileTypes: true }) |
| 28 | + .filter((entry) => entry.isDirectory()) |
| 29 | + .map((entry) => path.join(rootPath, entry.name)); |
| 30 | +} |
| 31 | + |
| 32 | +function ensureExecutable(filePath) { |
| 33 | + const stat = fs.statSync(filePath); |
| 34 | + if (!stat.isFile()) return false; |
| 35 | + const currentMode = stat.mode & 0o777; |
| 36 | + if ((currentMode & EXECUTABLE_MASK) === EXECUTABLE_MASK) { |
| 37 | + return false; |
| 38 | + } |
| 39 | + fs.chmodSync(filePath, currentMode | EXECUTABLE_MASK); |
| 40 | + return true; |
| 41 | +} |
| 42 | + |
| 43 | +function normalizeFileSet(filePaths, label) { |
| 44 | + const normalized = []; |
| 45 | + for (const filePath of filePaths) { |
| 46 | + if (!pathExists(filePath)) continue; |
| 47 | + if (ensureExecutable(filePath)) normalized.push(filePath); |
| 48 | + } |
| 49 | + return normalized.map((filePath) => ({ filePath, label })); |
| 50 | +} |
| 51 | + |
| 52 | +function collectDesktopRuntimeExecutableCandidates(rootPath) { |
| 53 | + const candidates = []; |
| 54 | + |
| 55 | + for (const prebuildDir of listDirectories(path.join(rootPath, "node_modules", "node-pty", "prebuilds"))) { |
| 56 | + candidates.push({ |
| 57 | + filePath: path.join(prebuildDir, "spawn-helper"), |
| 58 | + label: "node-pty spawn helper", |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + for (const packageDir of listDirectories(path.join(rootPath, "node_modules", "@openai"))) { |
| 63 | + if (!path.basename(packageDir).startsWith("codex-darwin-")) continue; |
| 64 | + for (const vendorDir of listDirectories(path.join(packageDir, "vendor"))) { |
| 65 | + candidates.push({ |
| 66 | + filePath: path.join(vendorDir, "codex", "codex"), |
| 67 | + label: "Codex CLI binary", |
| 68 | + }); |
| 69 | + candidates.push({ |
| 70 | + filePath: path.join(vendorDir, "path", "rg"), |
| 71 | + label: "Codex ripgrep helper", |
| 72 | + }); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + for (const vendorDir of listDirectories(path.join(rootPath, "node_modules", "@anthropic-ai", "claude-agent-sdk", "vendor", "ripgrep"))) { |
| 77 | + candidates.push({ |
| 78 | + filePath: path.join(vendorDir, "rg"), |
| 79 | + label: "Claude ripgrep helper", |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + return candidates; |
| 84 | +} |
| 85 | + |
| 86 | +function normalizeDesktopRuntimeBinaries(rootPath) { |
| 87 | + const normalized = []; |
| 88 | + for (const candidate of collectDesktopRuntimeExecutableCandidates(rootPath)) { |
| 89 | + if (!pathExists(candidate.filePath)) continue; |
| 90 | + if (ensureExecutable(candidate.filePath)) { |
| 91 | + normalized.push(candidate); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + const helperPathFiles = [ |
| 96 | + path.join(rootPath, "node_modules", "node-pty", "lib", "unixTerminal.js"), |
| 97 | + path.join(rootPath, "node_modules", "node-pty", "src", "unixTerminal.ts"), |
| 98 | + ]; |
| 99 | + |
| 100 | + for (const filePath of helperPathFiles) { |
| 101 | + if (!pathExists(filePath)) continue; |
| 102 | + const original = fs.readFileSync(filePath, "utf8"); |
| 103 | + let updated = original; |
| 104 | + for (const patch of NODE_PTY_HELPER_PATH_PATCHES) { |
| 105 | + updated = updated.replace(patch.from, patch.to); |
| 106 | + } |
| 107 | + if (updated === original) continue; |
| 108 | + fs.writeFileSync(filePath, updated, "utf8"); |
| 109 | + normalized.push({ |
| 110 | + filePath, |
| 111 | + label: "node-pty helper path patch", |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + return normalized; |
| 116 | +} |
| 117 | + |
| 118 | +function resolvePackagedRuntimeRoot(appBundlePath) { |
| 119 | + return path.join(appBundlePath, "Contents", "Resources", "app.asar.unpacked"); |
| 120 | +} |
| 121 | + |
| 122 | +module.exports = { |
| 123 | + normalizeDesktopRuntimeBinaries, |
| 124 | + resolvePackagedRuntimeRoot, |
| 125 | +}; |
0 commit comments