-
Notifications
You must be signed in to change notification settings - Fork 593
Expand file tree
/
Copy pathupdater.ts
More file actions
59 lines (50 loc) · 1.64 KB
/
updater.ts
File metadata and controls
59 lines (50 loc) · 1.64 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
/**
* Auto-update checker for ClawRouter.
* Checks npm registry on startup and notifies user if update available.
*/
import { VERSION } from "./version.js";
const NPM_REGISTRY = "https://registry.npmjs.org/@blockrun/clawrouter/latest";
const CHECK_TIMEOUT_MS = 5_000; // Don't block startup for more than 5s
/**
* Compare semver versions. Returns:
* 1 if a > b
* 0 if a === b
* -1 if a < b
*/
function compareSemver(a: string, b: string): number {
const pa = a.split(".").map(Number);
const pb = b.split(".").map(Number);
for (let i = 0; i < 3; i++) {
if ((pa[i] || 0) > (pb[i] || 0)) return 1;
if ((pa[i] || 0) < (pb[i] || 0)) return -1;
}
return 0;
}
/**
* Check npm registry for latest version.
* Non-blocking, silent on errors.
*/
export async function checkForUpdates(): Promise<void> {
try {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), CHECK_TIMEOUT_MS);
const res = await fetch(NPM_REGISTRY, {
signal: controller.signal,
headers: { Accept: "application/json" },
});
clearTimeout(timeout);
if (!res.ok) return;
const data = (await res.json()) as { version?: string };
const latest = data.version;
if (!latest) return;
if (compareSemver(latest, VERSION) > 0) {
console.log("");
console.log(`\x1b[33m⬆️ ClawRouter ${latest} available (you have ${VERSION})\x1b[0m`);
console.log(` Run: \x1b[36mnpx @blockrun/clawrouter@latest\x1b[0m`);
console.log(` Docs: \x1b[36mhttps://blockrun.ai/clawrouter.md\x1b[0m`);
console.log("");
}
} catch {
// Silent fail - don't disrupt startup
}
}