## Background Addressing feedback from https://github.com/openai/codex/pull/333#discussion_r2050893224, this PR adds support for Bun alongside npm, pnpm while keeping the code simple. ## Summary The update‑check flow is refactored to use a direct registry lookup (`fast-npm-meta` + `semver`) instead of shelling out to `npm outdated`, and adds a lightweight installer‑detection mechanism that: 1. Checks if the invoked script lives under a known global‑bin directory (npm, pnpm, or bun) 2. If not, falls back to local detection via `getUserAgent()` (the `package‑manager‑detector` library) ## What’s Changed - **Registry‑based version check** - Replace `execFile("npm", ["outdated"])` with `getLatestVersion()` and `semver.gt()` - **Multi‑manager support** - New `renderUpdateCommand` handles update commands for `npm`, `pnpm`, and `bun`. - Detect global installer first via `detectInstallerByPath()` - Fallback to local detection via `getUserAgent()` - **Module cleanup** - Extract `detectInstallerByPath` into `utils/package-manager-detector.ts` - Remove legacy `checkOutdated`, `getNPMCommandPath`, and child‑process JSON parsing - **Flow improvements in `checkForUpdates`** 1. Short‑circuit by `UPDATE_CHECK_FREQUENCY` 3. Fetch & compare versions 4. Persist new timestamp immediately 5. Render & display styled box only when an update exists - **Maintain simplicity** - All multi‑manager logic lives in one small helper and a concise lookup rather than a complex adapter hierarchy - Core `checkForUpdates` remains a single, easy‑to‑follow async function - **Dependencies added** - `fast-npm-meta`, `semver`, `package-manager-detector`, `@types/semver` ## Considerations If we decide to drop the interactive update‑message (`npm install -g @openai/codex`) rendering altogether, we could remove most of the installer‑detection code and dependencies, which would simplify the codebase further but result in a less friendly UX. ## Preview * npm  * bun  ## Simple Flow Chart ```mermaid flowchart TD A(Start) --> B[Read state] B --> C{Recent check?} C -- Yes --> Z[End] C -- No --> D[Fetch latest version] D --> E[Save check time] E --> F{Version data OK?} F -- No --> Z F -- Yes --> G{Update available?} G -- No --> Z G -- Yes --> H{Global install?} H -- Yes --> I[Select global manager] H -- No --> K{Local install?} K -- No --> Z K -- Yes --> L[Select local manager] I & L --> M[Render update message] M --> N[Format with boxen] N --> O[Print update] O --> Z ```
146 lines
3.5 KiB
TypeScript
146 lines
3.5 KiB
TypeScript
import type { AgentName } from "package-manager-detector";
|
|
|
|
import { detectInstallerByPath } from "./package-manager-detector";
|
|
import { CLI_VERSION } from "./session";
|
|
import boxen from "boxen";
|
|
import chalk from "chalk";
|
|
import { getLatestVersion } from "fast-npm-meta";
|
|
import { readFile, writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { getUserAgent } from "package-manager-detector";
|
|
import semver from "semver";
|
|
|
|
interface UpdateCheckState {
|
|
lastUpdateCheck?: string;
|
|
}
|
|
|
|
interface UpdateCheckInfo {
|
|
currentVersion: string;
|
|
latestVersion: string;
|
|
}
|
|
|
|
export interface UpdateOptions {
|
|
manager: AgentName;
|
|
packageName: string;
|
|
}
|
|
|
|
const UPDATE_CHECK_FREQUENCY = 1000 * 60 * 60 * 24; // 1 day
|
|
|
|
export function renderUpdateCommand({
|
|
manager,
|
|
packageName,
|
|
}: UpdateOptions): string {
|
|
const updateCommands: Record<AgentName, string> = {
|
|
npm: `npm install -g ${packageName}`,
|
|
pnpm: `pnpm add -g ${packageName}`,
|
|
bun: `bun add -g ${packageName}`,
|
|
/** Only works in yarn@v1 */
|
|
yarn: `yarn global add ${packageName}`,
|
|
deno: `deno install -g npm:${packageName}`,
|
|
};
|
|
|
|
return updateCommands[manager];
|
|
}
|
|
|
|
function renderUpdateMessage(options: UpdateOptions) {
|
|
const updateCommand = renderUpdateCommand(options);
|
|
return `To update, run ${chalk.magenta(updateCommand)} to update.`;
|
|
}
|
|
|
|
async function writeState(stateFilePath: string, state: UpdateCheckState) {
|
|
await writeFile(stateFilePath, JSON.stringify(state, null, 2), {
|
|
encoding: "utf8",
|
|
});
|
|
}
|
|
|
|
async function getUpdateCheckInfo(
|
|
packageName: string,
|
|
): Promise<UpdateCheckInfo | undefined> {
|
|
const metadata = await getLatestVersion(packageName, {
|
|
force: true,
|
|
throw: false,
|
|
});
|
|
|
|
if ("error" in metadata || !metadata?.version) {
|
|
return;
|
|
}
|
|
|
|
return {
|
|
currentVersion: CLI_VERSION,
|
|
latestVersion: metadata.version,
|
|
};
|
|
}
|
|
|
|
export async function checkForUpdates(): Promise<void> {
|
|
const { CONFIG_DIR } = await import("./config");
|
|
const stateFile = join(CONFIG_DIR, "update-check.json");
|
|
|
|
// Load previous check timestamp
|
|
let state: UpdateCheckState | undefined;
|
|
try {
|
|
state = JSON.parse(await readFile(stateFile, "utf8"));
|
|
} catch {
|
|
// ignore
|
|
}
|
|
|
|
// Bail out if we checked less than the configured frequency ago
|
|
if (
|
|
state?.lastUpdateCheck &&
|
|
Date.now() - new Date(state.lastUpdateCheck).valueOf() <
|
|
UPDATE_CHECK_FREQUENCY
|
|
) {
|
|
return;
|
|
}
|
|
|
|
// Fetch current vs latest from the registry
|
|
const { name: packageName } = await import("../../package.json");
|
|
const packageInfo = await getUpdateCheckInfo(packageName);
|
|
|
|
await writeState(stateFile, {
|
|
...state,
|
|
lastUpdateCheck: new Date().toUTCString(),
|
|
});
|
|
|
|
if (
|
|
!packageInfo ||
|
|
!semver.gt(packageInfo.latestVersion, packageInfo.currentVersion)
|
|
) {
|
|
return;
|
|
}
|
|
|
|
// Detect global installer
|
|
let managerName = await detectInstallerByPath();
|
|
|
|
// Fallback to the local package manager
|
|
if (!managerName) {
|
|
const local = getUserAgent();
|
|
if (!local) {
|
|
// No package managers found, skip it.
|
|
return;
|
|
}
|
|
managerName = local;
|
|
}
|
|
|
|
const updateMessage = renderUpdateMessage({
|
|
manager: managerName,
|
|
packageName,
|
|
});
|
|
|
|
const box = boxen(
|
|
`\
|
|
Update available! ${chalk.red(packageInfo.currentVersion)} → ${chalk.green(
|
|
packageInfo.latestVersion,
|
|
)}.
|
|
${updateMessage}`,
|
|
{
|
|
padding: 1,
|
|
margin: 1,
|
|
align: "center",
|
|
borderColor: "yellow",
|
|
borderStyle: "round",
|
|
},
|
|
);
|
|
|
|
// eslint-disable-next-line no-console
|
|
console.log(box);
|
|
}
|