Skip to content

Commit b012c9b

Browse files
committed
fix(@inquirerer/utils): use semver for version comparison and add clearUpdateCache()
1 parent 1d2795d commit b012c9b

4 files changed

Lines changed: 2661 additions & 5220 deletions

File tree

packages/inquirerer-utils/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
},
3030
"dependencies": {
3131
"appstash": "workspace:*",
32-
"inquirerer": "workspace:*"
32+
"inquirerer": "workspace:*",
33+
"semver": "^7.7.4"
3334
},
3435
"devDependencies": {
3536
"@types/minimist": "^1.2.5",
37+
"@types/semver": "^7.7.1",
3638
"makage": "0.1.10"
3739
},
3840
"keywords": []

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, shouldSkipUpdateCheck } from './update-check';
13+
export { checkForUpdates, clearUpdateCache, shouldSkipUpdateCheck } from './update-check';
1414
export type { UpdateCheckOptions, UpdateCheckResult } from './update-check';

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { appstash } from 'appstash';
22
import * as fs from 'fs';
33
import * as path from 'path';
4+
import semver from 'semver';
45

56
export interface UpdateCheckOptions {
67
pkgName: string;
@@ -66,9 +67,29 @@ export function shouldSkipUpdateCheck(
6667
return { skip: false };
6768
}
6869

70+
const CACHE_FILENAME = 'update-check.json';
6971
const DEFAULT_TTL = 24 * 60 * 60 * 1000; // 24 hours
7072
const DEFAULT_REGISTRY = 'https://registry.npmjs.org';
7173

74+
export function clearUpdateCache(toolName: string): boolean {
75+
try {
76+
const dirs = appstash(toolName);
77+
const cacheFile = path.join(dirs.cache, CACHE_FILENAME);
78+
if (fs.existsSync(cacheFile)) {
79+
fs.unlinkSync(cacheFile);
80+
return true;
81+
}
82+
} catch {}
83+
return false;
84+
}
85+
86+
function isNewerVersion(latest: string, current: string): boolean {
87+
if (semver.valid(latest) && semver.valid(current)) {
88+
return semver.gt(latest, current);
89+
}
90+
return latest !== current && latest > current;
91+
}
92+
7293
/**
7394
* Checks for updates to a package and caches the result.
7495
* Uses appstash for configuration storage.
@@ -108,18 +129,18 @@ export const checkForUpdates = async (
108129
}
109130

110131
const dirs = appstash(toolName);
111-
const cacheFile = path.join(dirs.cache, 'update-check.json');
132+
const cacheFile = path.join(dirs.cache, CACHE_FILENAME);
112133

113134
// Check cache first
114135
try {
115136
if (fs.existsSync(cacheFile)) {
116137
const cached = JSON.parse(fs.readFileSync(cacheFile, 'utf-8'));
117138
if (Date.now() - cached.timestamp < ttl) {
118139
return {
119-
hasUpdate: cached.latestVersion !== pkgVersion && cached.latestVersion > pkgVersion,
140+
hasUpdate: isNewerVersion(cached.latestVersion, pkgVersion),
120141
currentVersion: pkgVersion,
121142
latestVersion: cached.latestVersion,
122-
message: cached.latestVersion > pkgVersion
143+
message: isNewerVersion(cached.latestVersion, pkgVersion)
123144
? `Update available: ${pkgVersion} -> ${cached.latestVersion}`
124145
: null
125146
};
@@ -158,10 +179,10 @@ export const checkForUpdates = async (
158179
}
159180

160181
return {
161-
hasUpdate: latestVersion !== pkgVersion && latestVersion > pkgVersion,
182+
hasUpdate: isNewerVersion(latestVersion, pkgVersion),
162183
currentVersion: pkgVersion,
163184
latestVersion,
164-
message: latestVersion > pkgVersion
185+
message: isNewerVersion(latestVersion, pkgVersion)
165186
? `Update available: ${pkgVersion} -> ${latestVersion}`
166187
: null
167188
};

0 commit comments

Comments
 (0)