Detect Codex installs managed by Vite+

This commit is contained in:
Charlie Marsh
2026-07-01 20:02:38 -04:00
parent 042e61726d
commit dc174cdc4e
7 changed files with 102 additions and 9 deletions

View File

@@ -4,6 +4,7 @@
import { spawn } from "node:child_process";
import { existsSync, realpathSync } from "fs";
import { createRequire } from "node:module";
import os from "os";
import path from "path";
import { fileURLToPath } from "url";
@@ -11,6 +12,7 @@ import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const require = createRequire(import.meta.url);
const codexPackageRoot = realpathSync(path.join(__dirname, ".."));
const PLATFORM_PACKAGE_BY_TARGET = {
"x86_64-unknown-linux-musl": "@openai/codex-linux-x64",
@@ -98,7 +100,9 @@ function findCodexExecutable() {
const updateCommand =
packageManager === "bun"
? "bun install -g @openai/codex@latest"
: "npm install -g @openai/codex@latest";
: packageManager === "vite-plus"
? "vp install -g @openai/codex@latest"
: "npm install -g @openai/codex@latest";
throw new Error(
`Missing optional dependency ${platformPackage}. Reinstall Codex: ${updateCommand}`,
);
@@ -117,6 +121,15 @@ const binaryPath = findCodexExecutable();
* in order to give the user a hint about how to update it.
*/
function detectPackageManager() {
const vpHome = process.env.VP_HOME || path.join(os.homedir(), ".vite-plus");
const vpPackagesRoot = path.join(vpHome, "packages");
const resolvedVpPackagesRoot = existsSync(vpPackagesRoot)
? realpathSync(vpPackagesRoot)
: path.resolve(vpPackagesRoot);
if (isPathInside(resolvedVpPackagesRoot, codexPackageRoot)) {
return "vite-plus";
}
const userAgent = process.env.npm_config_user_agent || "";
if (/\bbun\//.test(userAgent)) {
return "bun";
@@ -137,15 +150,29 @@ function detectPackageManager() {
return userAgent ? "npm" : null;
}
function isPathInside(parent, child) {
const relative = path.relative(parent, child);
return (
relative === "" ||
(relative !== ".." &&
!relative.startsWith(`..${path.sep}`) &&
!path.isAbsolute(relative))
);
}
const packageManagerEnvVar =
detectPackageManager() === "bun"
? "CODEX_MANAGED_BY_BUN"
: "CODEX_MANAGED_BY_NPM";
{
"bun": "CODEX_MANAGED_BY_BUN",
"vite-plus": "CODEX_MANAGED_BY_VITE_PLUS",
}[detectPackageManager()] ?? "CODEX_MANAGED_BY_NPM";
const env = {
...process.env,
[packageManagerEnvVar]: "1",
CODEX_MANAGED_PACKAGE_ROOT: realpathSync(path.join(__dirname, "..")),
CODEX_MANAGED_PACKAGE_ROOT: codexPackageRoot,
};
delete env.CODEX_MANAGED_BY_NPM;
delete env.CODEX_MANAGED_BY_BUN;
delete env.CODEX_MANAGED_BY_VITE_PLUS;
env[packageManagerEnvVar] = "1";
const child = spawn(binaryPath, process.argv.slice(2), {
stdio: "inherit",