From a9205346d4896cb3497b7a25f6c504676fbe2345 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Fri, 27 Feb 2026 16:45:24 +0000 Subject: [PATCH] feat(inquirerer-utils): add suppressUpdateCheck to write cache entry after update After a CLI update command installs a new version, the currently running binary still reports the old version. If we merely clear the cache, the next command fetches the latest from npm and compares against the stale pkgVersion, producing a false-positive 'Update available' message. suppressUpdateCheck writes the current binary's version as latestVersion so the next checkForUpdates call sees no update needed. The cache expires after 24h, at which point a fresh check runs against the new binary. --- packages/inquirerer-utils/src/index.ts | 2 +- packages/inquirerer-utils/src/update-check.ts | 37 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/inquirerer-utils/src/index.ts b/packages/inquirerer-utils/src/index.ts index 7c8b72a..87ff025 100644 --- a/packages/inquirerer-utils/src/index.ts +++ b/packages/inquirerer-utils/src/index.ts @@ -10,5 +10,5 @@ export { export type { ParsedArgs, ParseArgvOptions, CliExitOptions, PackageJson } from 'inquirerer'; // Update checking (requires appstash, not available in inquirerer) -export { checkForUpdates, clearUpdateCache, shouldSkipUpdateCheck } from './update-check'; +export { checkForUpdates, clearUpdateCache, shouldSkipUpdateCheck, suppressUpdateCheck } from './update-check'; export type { UpdateCheckOptions, UpdateCheckResult } from './update-check'; diff --git a/packages/inquirerer-utils/src/update-check.ts b/packages/inquirerer-utils/src/update-check.ts index 8fc0b04..6ed6c95 100644 --- a/packages/inquirerer-utils/src/update-check.ts +++ b/packages/inquirerer-utils/src/update-check.ts @@ -83,6 +83,43 @@ export function clearUpdateCache(toolName: string): boolean { return false; } +/** + * Write a cache entry that suppresses the update notification. + * + * After a CLI `update` command installs a new version, the currently running + * binary still reports the OLD version via getPackageJson(__dirname). If we + * merely clear the cache, the next command will fetch the latest from npm and + * compare it against the stale pkgVersion, producing a false-positive + * "Update available" message. + * + * By writing the current binary's version as `latestVersion`, the next + * checkForUpdates call sees latestVersion === pkgVersion and returns + * hasUpdate: false. Once the cache expires (24 h by default), a fresh check + * runs against the (by then correct) new binary version. + * + * @param toolName - Tool name used for appstash directory (e.g., 'pgpm') + * @param currentVersion - The version of the currently running binary + * @returns true if the cache was written successfully + */ +export function suppressUpdateCheck(toolName: string, currentVersion: string): boolean { + try { + const dirs = appstash(toolName); + const cacheFile = path.join(dirs.cache, CACHE_FILENAME); + if (!fs.existsSync(dirs.cache)) { + fs.mkdirSync(dirs.cache, { recursive: true }); + } + fs.writeFileSync(cacheFile, JSON.stringify({ + latestVersion: currentVersion, + timestamp: Date.now() + })); + return true; + } catch { + // If writing fails, fall back to clearing the old cache + clearUpdateCache(toolName); + return false; + } +} + function isNewerVersion(latest: string, current: string): boolean { if (semver.valid(latest) && semver.valid(current)) { return semver.gt(latest, current);