()
| 365 | } |
| 366 | |
| 367 | async function checkForUpdate(): Promise<void> { |
| 368 | try { |
| 369 | const cacheFile = path.join(dataDir(), '.update-check'); |
| 370 | // Re-fetch from npm if cache is stale (>24hr) |
| 371 | let needsFetch = true; |
| 372 | try { |
| 373 | const stat = fs.statSync(cacheFile); |
| 374 | if (Date.now() - stat.mtimeMs < UPDATE_CHECK_INTERVAL_MS) needsFetch = false; |
| 375 | } catch { /* file doesn't exist, fetch */ } |
| 376 | |
| 377 | if (needsFetch) { |
| 378 | const controller = new AbortController(); |
| 379 | const timeout = setTimeout(() => controller.abort(), 5000); |
| 380 | const res = await fetch('https://registry.npmjs.org/fieldtheory/latest', { |
| 381 | signal: controller.signal, |
| 382 | headers: { accept: 'application/json' }, |
| 383 | }); |
| 384 | clearTimeout(timeout); |
| 385 | |
| 386 | if (res.ok) { |
| 387 | const data = await res.json() as any; |
| 388 | if (data?.version) fs.writeFileSync(cacheFile, data.version); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // Always show notice from cache |
| 393 | showCachedUpdateNotice(); |
| 394 | } catch { /* network error, offline, etc — silently skip */ } |
| 395 | } |
| 396 | |
| 397 | /** Sync version — reads cached check result. Used after help output where we can't await. */ |
| 398 | function showCachedUpdateNotice(): void { |
no test coverage detected