(
options: CheckForUpdatesOptions = {}
)
| 216 | } |
| 217 | |
| 218 | export async function checkForUpdates( |
| 219 | options: CheckForUpdatesOptions = {} |
| 220 | ): Promise<UpdateInfo | null> { |
| 221 | const now = options.now ?? Date.now(); |
| 222 | const cacheTtlMs = options.cacheTtlMs ?? DEFAULT_CACHE_TTL_MS; |
| 223 | const stateFile = options.stateFile; |
| 224 | const state = await readUpdateState(stateFile); |
| 225 | const isStale = |
| 226 | options.force || |
| 227 | !state.lastCheckedAt || |
| 228 | now - state.lastCheckedAt >= cacheTtlMs || |
| 229 | !state.latestVersion; |
| 230 | |
| 231 | let latestVersion = state.latestVersion ?? null; |
| 232 | |
| 233 | if (isStale) { |
| 234 | const fetchedVersion = await fetchLatestVersion(); |
| 235 | if (fetchedVersion) { |
| 236 | latestVersion = fetchedVersion; |
| 237 | await writeUpdateState( |
| 238 | { |
| 239 | ...state, |
| 240 | latestVersion: fetchedVersion, |
| 241 | lastCheckedAt: now, |
| 242 | }, |
| 243 | stateFile |
| 244 | ); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | if (!latestVersion) return null; |
| 249 | |
| 250 | const installMethod = detectInstallMethod(); |
| 251 | |
| 252 | return { |
| 253 | currentVersion: VERSION, |
| 254 | latestVersion, |
| 255 | updateAvailable: compareVersions(latestVersion, VERSION) > 0, |
| 256 | installMethod, |
| 257 | upgradePlan: getUpgradePlan(installMethod), |
| 258 | }; |
| 259 | } |
| 260 | |
| 261 | export async function shouldShowUpdateNotification( |
| 262 | info: UpdateInfo, |
no test coverage detected