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);