Skip to content

Commit a920534

Browse files
committed
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.
1 parent fd9721c commit a920534

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

packages/inquirerer-utils/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ export {
1010
export type { ParsedArgs, ParseArgvOptions, CliExitOptions, PackageJson } from 'inquirerer';
1111

1212
// Update checking (requires appstash, not available in inquirerer)
13-
export { checkForUpdates, clearUpdateCache, shouldSkipUpdateCheck } from './update-check';
13+
export { checkForUpdates, clearUpdateCache, shouldSkipUpdateCheck, suppressUpdateCheck } from './update-check';
1414
export type { UpdateCheckOptions, UpdateCheckResult } from './update-check';

packages/inquirerer-utils/src/update-check.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,43 @@ export function clearUpdateCache(toolName: string): boolean {
8383
return false;
8484
}
8585

86+
/**
87+
* Write a cache entry that suppresses the update notification.
88+
*
89+
* After a CLI `update` command installs a new version, the currently running
90+
* binary still reports the OLD version via getPackageJson(__dirname). If we
91+
* merely clear the cache, the next command will fetch the latest from npm and
92+
* compare it against the stale pkgVersion, producing a false-positive
93+
* "Update available" message.
94+
*
95+
* By writing the current binary's version as `latestVersion`, the next
96+
* checkForUpdates call sees latestVersion === pkgVersion and returns
97+
* hasUpdate: false. Once the cache expires (24 h by default), a fresh check
98+
* runs against the (by then correct) new binary version.
99+
*
100+
* @param toolName - Tool name used for appstash directory (e.g., 'pgpm')
101+
* @param currentVersion - The version of the currently running binary
102+
* @returns true if the cache was written successfully
103+
*/
104+
export function suppressUpdateCheck(toolName: string, currentVersion: string): boolean {
105+
try {
106+
const dirs = appstash(toolName);
107+
const cacheFile = path.join(dirs.cache, CACHE_FILENAME);
108+
if (!fs.existsSync(dirs.cache)) {
109+
fs.mkdirSync(dirs.cache, { recursive: true });
110+
}
111+
fs.writeFileSync(cacheFile, JSON.stringify({
112+
latestVersion: currentVersion,
113+
timestamp: Date.now()
114+
}));
115+
return true;
116+
} catch {
117+
// If writing fails, fall back to clearing the old cache
118+
clearUpdateCache(toolName);
119+
return false;
120+
}
121+
}
122+
86123
function isNewerVersion(latest: string, current: string): boolean {
87124
if (semver.valid(latest) && semver.valid(current)) {
88125
return semver.gt(latest, current);

0 commit comments

Comments
 (0)