Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/inquirerer-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
37 changes: 37 additions & 0 deletions packages/inquirerer-utils/src/update-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down