Skip to content

Commit 8aa6b50

Browse files
authored
Merge pull request #65 from constructive-io/devin/1772210653-add-suppress-update-check
feat(inquirerer-utils): add suppressUpdateCheck to write cache entry after update
2 parents fd9721c + a920534 commit 8aa6b50

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)