feat(update): add automatic update check on startup

- check cached update result synchronously on every command
- show warnBox notification if update available
- skip check for version/help/update commands
- refresh cache in background if stale (>24h)
- use proper UI components (warnBox) for notification
This commit is contained in:
kaitranntt
2025-12-18 06:12:40 -05:00
parent a0751ed604
commit 8a0ad53082
2 changed files with 119 additions and 21 deletions
+49 -8
View File
@@ -6,7 +6,6 @@ import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import * as https from 'https';
import { warn, color } from './ui';
const CACHE_DIR = path.join(os.homedir(), '.ccs', 'cache');
const UPDATE_CHECK_FILE = path.join(CACHE_DIR, 'update-check.json');
@@ -301,15 +300,24 @@ export async function checkForUpdates(
}
/**
* Show update notification
* Show update notification (async - initializes UI first)
*/
export function showUpdateNotification(updateInfo: { current: string; latest: string }): void {
export async function showUpdateNotification(updateInfo: {
current: string;
latest: string;
}): Promise<void> {
// Lazy import UI to ensure modules are loaded
const { initUI, warnBox, color } = await import('./ui');
await initUI();
const content = [
`Update available: ${updateInfo.current} -> ${updateInfo.latest}`,
'',
`Run ${color('ccs update', 'command')} to update`,
].join('\n');
console.log('');
console.log(color('═══════════════════════════════════════════════════════', 'info'));
console.log(warn(`Update available: ${updateInfo.current}${updateInfo.latest}`));
console.log(color('═══════════════════════════════════════════════════════', 'info'));
console.log('');
console.log(` Run ${color('ccs update', 'command')} to update`);
console.log(warnBox(content, 'UPDATE AVAILABLE'));
console.log('');
}
@@ -321,3 +329,36 @@ export function dismissUpdate(version: string): void {
cache.dismissed_version = version;
writeCache(cache);
}
/**
* Check cached update result synchronously (no network call)
* Returns update info if cached result indicates update available, null otherwise
*/
export function checkCachedUpdate(
currentVersion: string
): { current: string; latest: string } | null {
const cache = readCache();
// Check if cache has a newer version
if (
cache.latest_version &&
compareVersionsWithPrerelease(cache.latest_version, currentVersion) > 0
) {
// Don't show if user dismissed this version
if (cache.dismissed_version === cache.latest_version) {
return null;
}
return { current: currentVersion, latest: cache.latest_version };
}
return null;
}
/**
* Check if cache is stale (older than CHECK_INTERVAL)
*/
export function isCacheStale(): boolean {
const cache = readCache();
const now = Date.now();
return now - cache.last_check >= CHECK_INTERVAL;
}