forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate-notifier.ts
More file actions
74 lines (63 loc) · 2.07 KB
/
Copy pathupdate-notifier.ts
File metadata and controls
74 lines (63 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { compareVersions, fetchLatestVersion } from './commands/update/action.js';
import { PACKAGE_VERSION } from './constants.js';
import { mkdir, readFile, writeFile } from 'fs/promises';
import { homedir } from 'os';
import { join } from 'path';
const CACHE_DIR = join(homedir(), '.agentcore');
const CACHE_FILE = join(CACHE_DIR, 'update-check.json');
const CHECK_INTERVAL_MS = 24 * 60 * 60 * 1000; // every 24 hours
interface CacheData {
lastCheck: number;
latestVersion: string;
}
export interface UpdateCheckResult {
updateAvailable: boolean;
latestVersion: string;
}
async function readCache(): Promise<CacheData | null> {
try {
const data = await readFile(CACHE_FILE, 'utf-8');
return JSON.parse(data) as CacheData;
} catch {
return null;
}
}
async function writeCache(data: CacheData): Promise<void> {
try {
await mkdir(CACHE_DIR, { recursive: true });
await writeFile(CACHE_FILE, JSON.stringify(data), 'utf-8');
} catch {
// Silently ignore cache write failures
}
}
export async function checkForUpdate(): Promise<UpdateCheckResult | null> {
try {
const cache = await readCache();
const now = Date.now();
if (cache && now - cache.lastCheck < CHECK_INTERVAL_MS) {
const comparison = compareVersions(PACKAGE_VERSION, cache.latestVersion);
return {
updateAvailable: comparison > 0,
latestVersion: cache.latestVersion,
};
}
const latestVersion = await fetchLatestVersion();
await writeCache({ lastCheck: now, latestVersion });
const comparison = compareVersions(PACKAGE_VERSION, latestVersion);
return {
updateAvailable: comparison > 0,
latestVersion,
};
} catch {
return null;
}
}
export function printUpdateNotification(result: UpdateCheckResult): void {
const yellow = '\x1b[33m';
const cyan = '\x1b[36m';
const reset = '\x1b[0m';
process.stderr.write(
`\n${yellow}Update available:${reset} ${PACKAGE_VERSION} → ${cyan}${result.latestVersion}${reset}\n` +
`Run ${cyan}\`npm install -g @aws/agentcore@latest\`${reset} to update.\n`
);
}