generated from nhcarrigan/template
feat: add CLI update check with npm registry indicator
On app start, check the npm registry for the latest @anthropic-ai/claude-code version and compare against the installed version. If behind, the CLI badge turns amber with a pulsing up-arrow and a tooltip advising how to update. Also bumps SUPPORTED_CLI_VERSION to 2.1.72.
This commit is contained in:
@@ -2,9 +2,10 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import { onMount } from "svelte";
|
||||
|
||||
const SUPPORTED_CLI_VERSION = "2.1.53";
|
||||
const SUPPORTED_CLI_VERSION = "2.1.72";
|
||||
|
||||
let installedVersion = $state("Loading...");
|
||||
let latestNpmVersion = $state<string | null>(null);
|
||||
|
||||
function compareVersions(a: string, b: string): number {
|
||||
const aParts = a.split(".").map(Number);
|
||||
@@ -32,6 +33,15 @@
|
||||
return "current";
|
||||
});
|
||||
|
||||
let updateAvailable = $derived.by(() => {
|
||||
if (!latestNpmVersion || installedVersion === "Loading..." || installedVersion === "Unknown") {
|
||||
return false;
|
||||
}
|
||||
const semverMatch = /(\d+\.\d+\.\d+)/.exec(installedVersion);
|
||||
if (!semverMatch) return false;
|
||||
return compareVersions(semverMatch[1], latestNpmVersion) < 0;
|
||||
});
|
||||
|
||||
async function fetchVersion() {
|
||||
try {
|
||||
const result = await invoke<string>("get_claude_version");
|
||||
@@ -42,13 +52,28 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchLatestNpmVersion() {
|
||||
try {
|
||||
const result = await invoke<string>("check_cli_latest_version");
|
||||
latestNpmVersion = result;
|
||||
} catch (error) {
|
||||
console.error("Failed to check latest CLI version:", error);
|
||||
}
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
fetchVersion();
|
||||
fetchLatestNpmVersion();
|
||||
});
|
||||
</script>
|
||||
|
||||
<div class="cli-versions">
|
||||
<div class="cli-version">
|
||||
<div
|
||||
class="cli-version {updateAvailable ? 'update-available' : ''}"
|
||||
title={updateAvailable
|
||||
? `Update available: ${latestNpmVersion} — run: npm install -g @anthropic-ai/claude-code`
|
||||
: "Installed CLI version"}
|
||||
>
|
||||
<svg
|
||||
class="terminal-icon"
|
||||
width="14"
|
||||
@@ -64,6 +89,22 @@
|
||||
<line x1="12" y1="19" x2="20" y2="19" />
|
||||
</svg>
|
||||
<span class="version-text">CLI {displayVersion}</span>
|
||||
{#if updateAvailable}
|
||||
<svg
|
||||
class="update-icon"
|
||||
width="12"
|
||||
height="12"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2.5"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<polyline points="17 11 12 6 7 11" />
|
||||
<line x1="12" y1="6" x2="12" y2="18" />
|
||||
</svg>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div class="cli-version supported {supportedBadgeState}" title="Highest audited CLI version">
|
||||
@@ -135,6 +176,27 @@
|
||||
color: var(--error-color, #f44336);
|
||||
}
|
||||
|
||||
.cli-version.update-available {
|
||||
border-color: var(--warning-color, #ff9800);
|
||||
color: var(--warning-color, #ff9800);
|
||||
cursor: help;
|
||||
}
|
||||
|
||||
.update-icon {
|
||||
flex-shrink: 0;
|
||||
animation: pulse 2s ease-in-out infinite;
|
||||
}
|
||||
|
||||
@keyframes pulse {
|
||||
0%,
|
||||
100% {
|
||||
opacity: 1;
|
||||
}
|
||||
50% {
|
||||
opacity: 0.5;
|
||||
}
|
||||
}
|
||||
|
||||
.terminal-icon {
|
||||
flex-shrink: 0;
|
||||
opacity: 0.7;
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
|
||||
import { describe, it, expect } from "vitest";
|
||||
|
||||
const SUPPORTED_CLI_VERSION = "2.1.53";
|
||||
const SUPPORTED_CLI_VERSION = "2.1.72";
|
||||
|
||||
function compareVersions(a: string, b: string): number {
|
||||
const aParts = a.split(".").map(Number);
|
||||
@@ -41,7 +41,7 @@ describe("SUPPORTED_CLI_VERSION", () => {
|
||||
});
|
||||
|
||||
it("matches the expected audited version", () => {
|
||||
expect(SUPPORTED_CLI_VERSION).toBe("2.1.53");
|
||||
expect(SUPPORTED_CLI_VERSION).toBe("2.1.72");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -128,7 +128,55 @@ describe("compareVersions", () => {
|
||||
});
|
||||
|
||||
it("returns 0 for exactly the supported version", () => {
|
||||
expect(compareVersions("2.1.53", SUPPORTED_CLI_VERSION)).toBe(0);
|
||||
expect(compareVersions("2.1.72", SUPPORTED_CLI_VERSION)).toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// Mirrors the updateAvailable derived logic in CliVersion.svelte
|
||||
function isUpdateAvailable(installedVersion: string, latestNpmVersion: string | null): boolean {
|
||||
if (!latestNpmVersion || installedVersion === "Loading..." || installedVersion === "Unknown") {
|
||||
return false;
|
||||
}
|
||||
const semverMatch = /(\d+\.\d+\.\d+)/.exec(installedVersion);
|
||||
if (!semverMatch) return false;
|
||||
return compareVersions(semverMatch[1], latestNpmVersion) < 0;
|
||||
}
|
||||
|
||||
describe("updateAvailable", () => {
|
||||
it("returns false when latestNpmVersion is null", () => {
|
||||
expect(isUpdateAvailable("2.1.70", null)).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when installed is Loading...", () => {
|
||||
expect(isUpdateAvailable("Loading...", "2.1.72")).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when installed is Unknown", () => {
|
||||
expect(isUpdateAvailable("Unknown", "2.1.72")).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when installed equals latest", () => {
|
||||
expect(isUpdateAvailable("2.1.72", "2.1.72")).toBe(false);
|
||||
});
|
||||
|
||||
it("returns false when installed is ahead of latest", () => {
|
||||
expect(isUpdateAvailable("2.1.73", "2.1.72")).toBe(false);
|
||||
});
|
||||
|
||||
it("returns true when installed is behind latest", () => {
|
||||
expect(isUpdateAvailable("2.1.70", "2.1.72")).toBe(true);
|
||||
});
|
||||
|
||||
it("returns true when installed has a lower minor version", () => {
|
||||
expect(isUpdateAvailable("2.0.99", "2.1.72")).toBe(true);
|
||||
});
|
||||
|
||||
it("handles version strings with extra info like '2.1.70 (build 123)'", () => {
|
||||
expect(isUpdateAvailable("2.1.70 (build 123)", "2.1.72")).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false for unparseable installed version", () => {
|
||||
expect(isUpdateAvailable("not-a-version", "2.1.72")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user