()
| 69 | } |
| 70 | |
| 71 | export function checkForUpdateNotice(): string | null { |
| 72 | const cache = State.readUpdateCheck(); |
| 73 | |
| 74 | const shouldFetch = |
| 75 | !cache || Date.now() - new Date(cache.lastChecked).getTime() > CHECK_INTERVAL_MS; |
| 76 | |
| 77 | if (shouldFetch) { |
| 78 | // Fire-and-forget background refresh — never awaited |
| 79 | fetchLatestVersion({ unref: true }) |
| 80 | .then(latestVersion => { |
| 81 | State.writeUpdateCheck({ |
| 82 | ...cache, |
| 83 | lastChecked: new Date().toISOString(), |
| 84 | latestVersion, |
| 85 | }); |
| 86 | }) |
| 87 | .catch(() => { |
| 88 | // non-critical |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | if (!cache || !isNewerVersion(cache.latestVersion, getCurrentVersion())) { |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | // Rate-limit: show the notice at most once per NOTIFY_INTERVAL_MS, regardless |
| 97 | // of how often the user invokes the CLI. |
| 98 | const lastNotifiedMs = cache.lastNotified ? new Date(cache.lastNotified).getTime() : 0; |
| 99 | if (Date.now() - lastNotifiedMs < NOTIFY_INTERVAL_MS) { |
| 100 | return null; |
| 101 | } |
| 102 | |
| 103 | State.writeUpdateCheck({ |
| 104 | ...cache, |
| 105 | lastNotified: new Date().toISOString(), |
| 106 | }); |
| 107 | |
| 108 | return formatUpdateNotice(cache.latestVersion); |
| 109 | } |
no test coverage detected