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:
@@ -662,6 +662,37 @@ pub async fn fetch_changelog() -> Result<Vec<ChangelogEntry>, String> {
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn parse_npm_cli_version(json: &str) -> Result<String, String> {
|
||||
let data: serde_json::Value =
|
||||
serde_json::from_str(json).map_err(|e| format!("Failed to parse response: {}", e))?;
|
||||
data.get("version")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|s| s.to_string())
|
||||
.ok_or_else(|| "No version field in response".to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn check_cli_latest_version() -> Result<String, String> {
|
||||
let client = reqwest::Client::new();
|
||||
let response = client
|
||||
.get("https://registry.npmjs.org/@anthropic-ai/claude-code/latest")
|
||||
.header("Accept", "application/json")
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| format!("Failed to fetch CLI version: {}", e))?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(format!("Registry returned status: {}", response.status()));
|
||||
}
|
||||
|
||||
let body = response
|
||||
.text()
|
||||
.await
|
||||
.map_err(|e| format!("Failed to read response: {}", e))?;
|
||||
|
||||
parse_npm_cli_version(&body)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize)]
|
||||
pub struct SavedFileInfo {
|
||||
pub path: String,
|
||||
@@ -2838,6 +2869,35 @@ mod tests {
|
||||
assert!(json.contains("null") || json.contains("release_notes"));
|
||||
}
|
||||
|
||||
// ==================== parse_npm_cli_version tests ====================
|
||||
|
||||
#[test]
|
||||
fn test_parse_npm_cli_version_valid() {
|
||||
let json = r#"{"name":"@anthropic-ai/claude-code","version":"2.1.72","description":"Claude Code"}"#;
|
||||
let result = parse_npm_cli_version(json).unwrap();
|
||||
assert_eq!(result, "2.1.72");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_npm_cli_version_missing_field() {
|
||||
let json = r#"{"name":"@anthropic-ai/claude-code","description":"no version here"}"#;
|
||||
let result = parse_npm_cli_version(json);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_npm_cli_version_invalid_json() {
|
||||
let result = parse_npm_cli_version("not json at all");
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_npm_cli_version_non_string_version() {
|
||||
let json = r#"{"version":123}"#;
|
||||
let result = parse_npm_cli_version(json);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
// ==================== SavedFileInfo struct tests ====================
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -134,6 +134,7 @@ pub fn run() {
|
||||
list_skills,
|
||||
check_for_updates,
|
||||
fetch_changelog,
|
||||
check_cli_latest_version,
|
||||
save_temp_file,
|
||||
register_temp_file,
|
||||
get_temp_files,
|
||||
|
||||
Reference in New Issue
Block a user