|
| 1 | +import { homedir } from "node:os"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { ensureDir, pathExists, readJSON, writeJSON } from "fs-extra/esm"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Platform identifier for version information |
| 7 | + */ |
| 8 | +export type Platform = "mac" | "win-x64" | "win-arm64" | "win32" | "linux-x86_64" | "linux-i686" | "linux-arm64"; |
| 9 | + |
| 10 | +/** |
| 11 | + * Version info structure |
| 12 | + */ |
| 13 | +export interface ZoteroVersionInfo { |
| 14 | + lastUpdated: number; |
| 15 | + release: Record<Platform, string>; |
| 16 | + beta: Record<Platform, string>; |
| 17 | +} |
| 18 | + |
| 19 | +const CACHE_DIR = join(homedir(), ".scaffold", "cache"); |
| 20 | +const CACHE_FILE = join(CACHE_DIR, "zotero-version.json"); |
| 21 | +const CACHE_TTL = 3 * 24 * 60 * 60 * 1000; // 3 days in milliseconds |
| 22 | +const API_BETA = "https://www.zotero.org/download/client/version?channel=beta"; |
| 23 | +const API_RELEASE = "https://www.zotero.org/download/client/version?channel=release"; |
| 24 | + |
| 25 | +/** |
| 26 | + * Fetch Zotero version information from API |
| 27 | + */ |
| 28 | +async function fetchVersionsFromAPI(): Promise<Omit<ZoteroVersionInfo, "lastUpdated">> { |
| 29 | + const [betaRes, releaseRes] = await Promise.all([ |
| 30 | + fetch(API_BETA), |
| 31 | + fetch(API_RELEASE), |
| 32 | + ]); |
| 33 | + |
| 34 | + if (!betaRes.ok || !releaseRes.ok) { |
| 35 | + throw new Error("Failed to fetch Zotero version information from API"); |
| 36 | + } |
| 37 | + |
| 38 | + const [beta, release] = await Promise.all([ |
| 39 | + betaRes.json(), |
| 40 | + releaseRes.json(), |
| 41 | + ]); |
| 42 | + |
| 43 | + return { |
| 44 | + beta: beta as Record<Platform, string>, |
| 45 | + release: release as Record<Platform, string>, |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Load version cache from disk |
| 51 | + */ |
| 52 | +async function loadCache(): Promise<ZoteroVersionInfo | null> { |
| 53 | + try { |
| 54 | + if (!await pathExists(CACHE_FILE)) { |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + const cache = await readJSON(CACHE_FILE) as ZoteroVersionInfo; |
| 59 | + const age = Date.now() - cache.lastUpdated; |
| 60 | + |
| 61 | + // Check if cache is still valid |
| 62 | + if (age > CACHE_TTL) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + return cache; |
| 67 | + } |
| 68 | + catch { |
| 69 | + return null; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Save version cache to disk |
| 75 | + */ |
| 76 | +async function saveCache(data: Omit<ZoteroVersionInfo, "lastUpdated">): Promise<void> { |
| 77 | + await ensureDir(CACHE_DIR); |
| 78 | + const cache: ZoteroVersionInfo = { |
| 79 | + lastUpdated: Date.now(), |
| 80 | + ...data, |
| 81 | + }; |
| 82 | + await writeJSON(CACHE_FILE, cache, { spaces: 2 }); |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Get Zotero version information with caching |
| 87 | + */ |
| 88 | +export async function getZoteroVersionInfo(): Promise<ZoteroVersionInfo> { |
| 89 | + // Try to load from cache first |
| 90 | + const cachedData = await loadCache(); |
| 91 | + if (cachedData) { |
| 92 | + return cachedData; |
| 93 | + } |
| 94 | + |
| 95 | + // Fetch from API if cache is not available |
| 96 | + const freshData = await fetchVersionsFromAPI(); |
| 97 | + await saveCache(freshData); |
| 98 | + |
| 99 | + return { |
| 100 | + lastUpdated: Date.now(), |
| 101 | + ...freshData, |
| 102 | + }; |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Parse version string to extract major version |
| 107 | + * E.g., "9.0-beta.21+1a89239a1" -> 9, "9.0" -> 9 |
| 108 | + */ |
| 109 | +export function parseMajorVersion(version: string): number { |
| 110 | + const match = version.match(/^(\d+)/); |
| 111 | + if (!match) { |
| 112 | + throw new Error(`Invalid version format: ${version}`); |
| 113 | + } |
| 114 | + return Number.parseInt(match[1], 10); |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Compare two version strings |
| 119 | + * Returns: -1 if v1 < v2, 0 if v1 === v2, 1 if v1 > v2 |
| 120 | + */ |
| 121 | +export function compareVersions(v1: string, v2: string): number { |
| 122 | + const v1Parts = v1.split(/[.-]/).map((p) => { |
| 123 | + const num = Number.parseInt(p, 10); |
| 124 | + return Number.isNaN(num) ? 0 : num; |
| 125 | + }); |
| 126 | + const v2Parts = v2.split(/[.-]/).map((p) => { |
| 127 | + const num = Number.parseInt(p, 10); |
| 128 | + return Number.isNaN(num) ? 0 : num; |
| 129 | + }); |
| 130 | + |
| 131 | + for (let i = 0; i < Math.max(v1Parts.length, v2Parts.length); i++) { |
| 132 | + const p1 = v1Parts[i] ?? 0; |
| 133 | + const p2 = v2Parts[i] ?? 0; |
| 134 | + |
| 135 | + if (p1 < p2) |
| 136 | + return -1; |
| 137 | + if (p1 > p2) |
| 138 | + return 1; |
| 139 | + } |
| 140 | + |
| 141 | + return 0; |
| 142 | +} |
0 commit comments