| 124 | } |
| 125 | |
| 126 | function installPackage(name) { |
| 127 | const version = packageJson.optionalDependencies?.[name] |
| 128 | if (!version) return |
| 129 | |
| 130 | const temp = fs.mkdtempSync(path.join(os.tmpdir(), "opencode-install-")) |
| 131 | try { |
| 132 | const result = childProcess.spawnSync( |
| 133 | "npm", |
| 134 | ["install", "--ignore-scripts", "--no-save", "--loglevel=error", "--prefix", temp, `${name}@${version}`], |
| 135 | { stdio: "inherit", windowsHide: true }, |
| 136 | ) |
| 137 | if (result.status !== 0) return |
| 138 | const packageDir = path.join(temp, "node_modules", name) |
| 139 | copyBinary(path.join(packageDir, "bin", sourceBinary), targetBinary) |
| 140 | return true |
| 141 | } finally { |
| 142 | fs.rmSync(temp, { recursive: true, force: true }) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | function copyBinary(source, target) { |
| 147 | if (!fs.existsSync(source)) throw new Error(`Binary not found at ${source}`) |