| #!/usr/bin/env node |
| |
|
|
| import { spawn } from "node:child_process"; |
| import { existsSync } from "fs"; |
| import { createRequire } from "node:module"; |
| import path from "path"; |
| import { fileURLToPath } from "url"; |
|
|
| |
| const __filename = fileURLToPath(import.meta.url); |
| const __dirname = path.dirname(__filename); |
| const require = createRequire(import.meta.url); |
|
|
| const PLATFORM_PACKAGE_BY_TARGET = { |
| "x86_64-unknown-linux-musl": "@openai/codex-linux-x64", |
| "aarch64-unknown-linux-musl": "@openai/codex-linux-arm64", |
| "x86_64-apple-darwin": "@openai/codex-darwin-x64", |
| "aarch64-apple-darwin": "@openai/codex-darwin-arm64", |
| "x86_64-pc-windows-msvc": "@openai/codex-win32-x64", |
| "aarch64-pc-windows-msvc": "@openai/codex-win32-arm64", |
| }; |
|
|
| const { platform, arch } = process; |
|
|
| let targetTriple = null; |
| switch (platform) { |
| case "linux": |
| case "android": |
| switch (arch) { |
| case "x64": |
| targetTriple = "x86_64-unknown-linux-musl"; |
| break; |
| case "arm64": |
| targetTriple = "aarch64-unknown-linux-musl"; |
| break; |
| default: |
| break; |
| } |
| break; |
| case "darwin": |
| switch (arch) { |
| case "x64": |
| targetTriple = "x86_64-apple-darwin"; |
| break; |
| case "arm64": |
| targetTriple = "aarch64-apple-darwin"; |
| break; |
| default: |
| break; |
| } |
| break; |
| case "win32": |
| switch (arch) { |
| case "x64": |
| targetTriple = "x86_64-pc-windows-msvc"; |
| break; |
| case "arm64": |
| targetTriple = "aarch64-pc-windows-msvc"; |
| break; |
| default: |
| break; |
| } |
| break; |
| default: |
| break; |
| } |
|
|
| if (!targetTriple) { |
| throw new Error(`Unsupported platform: ${platform} (${arch})`); |
| } |
|
|
| const platformPackage = PLATFORM_PACKAGE_BY_TARGET[targetTriple]; |
| if (!platformPackage) { |
| throw new Error(`Unsupported target triple: ${targetTriple}`); |
| } |
|
|
| const codexBinaryName = process.platform === "win32" ? "codex.exe" : "codex"; |
| const localVendorRoot = path.join(__dirname, "..", "vendor"); |
| const localBinaryPath = path.join( |
| localVendorRoot, |
| targetTriple, |
| "codex", |
| codexBinaryName, |
| ); |
|
|
| let vendorRoot; |
| try { |
| const packageJsonPath = require.resolve(`${platformPackage}/package.json`); |
| vendorRoot = path.join(path.dirname(packageJsonPath), "vendor"); |
| } catch { |
| if (existsSync(localBinaryPath)) { |
| vendorRoot = localVendorRoot; |
| } else { |
| const packageManager = detectPackageManager(); |
| const updateCommand = |
| packageManager === "bun" |
| ? "bun install -g @openai/codex@latest" |
| : "npm install -g @openai/codex@latest"; |
| throw new Error( |
| `Missing optional dependency ${platformPackage}. Reinstall Codex: ${updateCommand}`, |
| ); |
| } |
| } |
|
|
| if (!vendorRoot) { |
| const packageManager = detectPackageManager(); |
| const updateCommand = |
| packageManager === "bun" |
| ? "bun install -g @openai/codex@latest" |
| : "npm install -g @openai/codex@latest"; |
| throw new Error( |
| `Missing optional dependency ${platformPackage}. Reinstall Codex: ${updateCommand}`, |
| ); |
| } |
|
|
| const archRoot = path.join(vendorRoot, targetTriple); |
| const binaryPath = path.join(archRoot, "codex", codexBinaryName); |
|
|
| |
| |
| |
| |
| |
|
|
| function getUpdatedPath(newDirs) { |
| const pathSep = process.platform === "win32" ? ";" : ":"; |
| const existingPath = process.env.PATH || ""; |
| const updatedPath = [ |
| ...newDirs, |
| ...existingPath.split(pathSep).filter(Boolean), |
| ].join(pathSep); |
| return updatedPath; |
| } |
|
|
| |
| |
| |
| |
| function detectPackageManager() { |
| const userAgent = process.env.npm_config_user_agent || ""; |
| if (/\bbun\//.test(userAgent)) { |
| return "bun"; |
| } |
|
|
| const execPath = process.env.npm_execpath || ""; |
| if (execPath.includes("bun")) { |
| return "bun"; |
| } |
|
|
| if ( |
| __dirname.includes(".bun/install/global") || |
| __dirname.includes(".bun\\install\\global") |
| ) { |
| return "bun"; |
| } |
|
|
| return userAgent ? "npm" : null; |
| } |
|
|
| const additionalDirs = []; |
| const pathDir = path.join(archRoot, "path"); |
| if (existsSync(pathDir)) { |
| additionalDirs.push(pathDir); |
| } |
| const updatedPath = getUpdatedPath(additionalDirs); |
|
|
| const env = { ...process.env, PATH: updatedPath }; |
| const packageManagerEnvVar = |
| detectPackageManager() === "bun" |
| ? "CODEX_MANAGED_BY_BUN" |
| : "CODEX_MANAGED_BY_NPM"; |
| env[packageManagerEnvVar] = "1"; |
|
|
| const child = spawn(binaryPath, process.argv.slice(2), { |
| stdio: "inherit", |
| env, |
| }); |
|
|
| child.on("error", (err) => { |
| |
| |
| |
| |
| console.error(err); |
| process.exit(1); |
| }); |
|
|
| |
| |
| |
| |
| const forwardSignal = (signal) => { |
| if (child.killed) { |
| return; |
| } |
| try { |
| child.kill(signal); |
| } catch { |
| |
| } |
| }; |
|
|
| ["SIGINT", "SIGTERM", "SIGHUP"].forEach((sig) => { |
| process.on(sig, () => forwardSignal(sig)); |
| }); |
|
|
| |
| |
| |
| |
| |
| const childResult = await new Promise((resolve) => { |
| child.on("exit", (code, signal) => { |
| if (signal) { |
| resolve({ type: "signal", signal }); |
| } else { |
| resolve({ type: "code", exitCode: code ?? 1 }); |
| } |
| }); |
| }); |
|
|
| if (childResult.type === "signal") { |
| |
| |
| process.kill(process.pid, childResult.signal); |
| } else { |
| process.exit(childResult.exitCode); |
| } |
|
|