|
1 | 1 | import { appstash } from 'appstash'; |
2 | 2 | import * as fs from 'fs'; |
3 | 3 | import * as path from 'path'; |
| 4 | +import semver from 'semver'; |
4 | 5 |
|
5 | 6 | export interface UpdateCheckOptions { |
6 | 7 | pkgName: string; |
@@ -66,9 +67,29 @@ export function shouldSkipUpdateCheck( |
66 | 67 | return { skip: false }; |
67 | 68 | } |
68 | 69 |
|
| 70 | +const CACHE_FILENAME = 'update-check.json'; |
69 | 71 | const DEFAULT_TTL = 24 * 60 * 60 * 1000; // 24 hours |
70 | 72 | const DEFAULT_REGISTRY = 'https://registry.npmjs.org'; |
71 | 73 |
|
| 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 | + |
72 | 93 | /** |
73 | 94 | * Checks for updates to a package and caches the result. |
74 | 95 | * Uses appstash for configuration storage. |
@@ -108,18 +129,18 @@ export const checkForUpdates = async ( |
108 | 129 | } |
109 | 130 |
|
110 | 131 | const dirs = appstash(toolName); |
111 | | - const cacheFile = path.join(dirs.cache, 'update-check.json'); |
| 132 | + const cacheFile = path.join(dirs.cache, CACHE_FILENAME); |
112 | 133 |
|
113 | 134 | // Check cache first |
114 | 135 | try { |
115 | 136 | if (fs.existsSync(cacheFile)) { |
116 | 137 | const cached = JSON.parse(fs.readFileSync(cacheFile, 'utf-8')); |
117 | 138 | if (Date.now() - cached.timestamp < ttl) { |
118 | 139 | return { |
119 | | - hasUpdate: cached.latestVersion !== pkgVersion && cached.latestVersion > pkgVersion, |
| 140 | + hasUpdate: isNewerVersion(cached.latestVersion, pkgVersion), |
120 | 141 | currentVersion: pkgVersion, |
121 | 142 | latestVersion: cached.latestVersion, |
122 | | - message: cached.latestVersion > pkgVersion |
| 143 | + message: isNewerVersion(cached.latestVersion, pkgVersion) |
123 | 144 | ? `Update available: ${pkgVersion} -> ${cached.latestVersion}` |
124 | 145 | : null |
125 | 146 | }; |
@@ -158,10 +179,10 @@ export const checkForUpdates = async ( |
158 | 179 | } |
159 | 180 |
|
160 | 181 | return { |
161 | | - hasUpdate: latestVersion !== pkgVersion && latestVersion > pkgVersion, |
| 182 | + hasUpdate: isNewerVersion(latestVersion, pkgVersion), |
162 | 183 | currentVersion: pkgVersion, |
163 | 184 | latestVersion, |
164 | | - message: latestVersion > pkgVersion |
| 185 | + message: isNewerVersion(latestVersion, pkgVersion) |
165 | 186 | ? `Update available: ${pkgVersion} -> ${latestVersion}` |
166 | 187 | : null |
167 | 188 | }; |
|
0 commit comments