Note the high-level motivation behind this change is to avoid the need
to make temporary changes in the source tree in order to cut a release
build since that runs the risk of leaving things in an inconsistent
state in the event of a failure. The existing code:
```
import pkg from "../../package.json" assert { type: "json" };
```
did not work as intended because, as written, ESBuild would bake the
contents of the local `package.json` into the release build at build
time whereas we want it to read the contents at runtime so we can use
the `package.json` in the tree to build the code and later inject a
modified version into the release package with a timestamped build
version.
Changes:
* move `CLI_VERSION` out of `src/utils/session.ts` and into
`src/version.ts` so `../package.json` is a correct relative path both
from `src/version.ts` in the source tree and also in the final
`dist/cli.js` build output
* change `assert` to `with` in `import pkg` as apparently `with` became
standard in Node 22
* mark `"../package.json"` as external in `build.mjs` so the version is
not baked into the `.js` at build time
After using `pnpm stage-release` to build a release version, if I use
Node 22.0 to run Codex, I see the following printed to stderr at
startup:
```
(node:71308) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
```
Note it is a warning and does not prevent Codex from running.
In Node 22.12, the warning goes away, but the warning still appears in
Node 22.11. For Node 22, 22.15.0 is the current LTS version, so LTS
users will not see this.
Also, something about moving the definition of `CLI_VERSION` caused a
problem with the mocks in `check-updates.test.ts`. I asked Codex to fix
it, and it came up with the change to the test configs. I don't know
enough about vitest to understand what it did, but the tests seem
healthy again, so I'm going with it.
147 lines
3.5 KiB
TypeScript
147 lines
3.5 KiB
TypeScript
import type { AgentName } from "package-manager-detector";
|
|
|
|
import { detectInstallerByPath } from "./package-manager-detector";
|
|
import { CLI_VERSION } from "../version";
|
|
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);
|
|
}
|