|
| 1 | +import * as fs from "node:fs" |
| 2 | +import * as path from "node:path" |
| 3 | +import type { NpmDistTags, OpencodeConfig, PackageJson, UpdateCheckResult } from "./types" |
| 4 | +import { |
| 5 | + PACKAGE_NAME, |
| 6 | + NPM_REGISTRY_URL, |
| 7 | + NPM_FETCH_TIMEOUT, |
| 8 | + INSTALLED_PACKAGE_JSON, |
| 9 | + USER_OPENCODE_CONFIG, |
| 10 | +} from "./constants" |
| 11 | +import { log } from "../../shared/logger" |
| 12 | + |
| 13 | +export function isLocalDevMode(directory: string): boolean { |
| 14 | + const projectConfig = path.join(directory, ".opencode", "opencode.json") |
| 15 | + |
| 16 | + for (const configPath of [projectConfig, USER_OPENCODE_CONFIG]) { |
| 17 | + try { |
| 18 | + if (!fs.existsSync(configPath)) continue |
| 19 | + const content = fs.readFileSync(configPath, "utf-8") |
| 20 | + const config = JSON.parse(content) as OpencodeConfig |
| 21 | + const plugins = config.plugin ?? [] |
| 22 | + |
| 23 | + for (const entry of plugins) { |
| 24 | + if (entry.startsWith("file://") && entry.includes(PACKAGE_NAME)) { |
| 25 | + return true |
| 26 | + } |
| 27 | + } |
| 28 | + } catch { |
| 29 | + continue |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return false |
| 34 | +} |
| 35 | + |
| 36 | +export function findPluginEntry(directory: string): string | null { |
| 37 | + const projectConfig = path.join(directory, ".opencode", "opencode.json") |
| 38 | + |
| 39 | + for (const configPath of [projectConfig, USER_OPENCODE_CONFIG]) { |
| 40 | + try { |
| 41 | + if (!fs.existsSync(configPath)) continue |
| 42 | + const content = fs.readFileSync(configPath, "utf-8") |
| 43 | + const config = JSON.parse(content) as OpencodeConfig |
| 44 | + const plugins = config.plugin ?? [] |
| 45 | + |
| 46 | + for (const entry of plugins) { |
| 47 | + if (entry === PACKAGE_NAME || entry.startsWith(`${PACKAGE_NAME}@`)) { |
| 48 | + return entry |
| 49 | + } |
| 50 | + } |
| 51 | + } catch { |
| 52 | + continue |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + return null |
| 57 | +} |
| 58 | + |
| 59 | +export function getCachedVersion(): string | null { |
| 60 | + try { |
| 61 | + if (!fs.existsSync(INSTALLED_PACKAGE_JSON)) return null |
| 62 | + const content = fs.readFileSync(INSTALLED_PACKAGE_JSON, "utf-8") |
| 63 | + const pkg = JSON.parse(content) as PackageJson |
| 64 | + return pkg.version ?? null |
| 65 | + } catch { |
| 66 | + return null |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +export async function getLatestVersion(): Promise<string | null> { |
| 71 | + const controller = new AbortController() |
| 72 | + const timeoutId = setTimeout(() => controller.abort(), NPM_FETCH_TIMEOUT) |
| 73 | + |
| 74 | + try { |
| 75 | + const response = await fetch(NPM_REGISTRY_URL, { |
| 76 | + signal: controller.signal, |
| 77 | + headers: { Accept: "application/json" }, |
| 78 | + }) |
| 79 | + |
| 80 | + if (!response.ok) return null |
| 81 | + |
| 82 | + const data = (await response.json()) as NpmDistTags |
| 83 | + return data.latest ?? null |
| 84 | + } catch { |
| 85 | + return null |
| 86 | + } finally { |
| 87 | + clearTimeout(timeoutId) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export async function checkForUpdate(directory: string): Promise<UpdateCheckResult> { |
| 92 | + if (isLocalDevMode(directory)) { |
| 93 | + log("[auto-update-checker] Local dev mode detected, skipping update check") |
| 94 | + return { needsUpdate: false, currentVersion: null, latestVersion: null, isLocalDev: true } |
| 95 | + } |
| 96 | + |
| 97 | + const pluginEntry = findPluginEntry(directory) |
| 98 | + if (!pluginEntry) { |
| 99 | + log("[auto-update-checker] Plugin not found in config") |
| 100 | + return { needsUpdate: false, currentVersion: null, latestVersion: null, isLocalDev: false } |
| 101 | + } |
| 102 | + |
| 103 | + const currentVersion = getCachedVersion() |
| 104 | + if (!currentVersion) { |
| 105 | + log("[auto-update-checker] No cached version found") |
| 106 | + return { needsUpdate: false, currentVersion: null, latestVersion: null, isLocalDev: false } |
| 107 | + } |
| 108 | + |
| 109 | + const latestVersion = await getLatestVersion() |
| 110 | + if (!latestVersion) { |
| 111 | + log("[auto-update-checker] Failed to fetch latest version") |
| 112 | + return { needsUpdate: false, currentVersion, latestVersion: null, isLocalDev: false } |
| 113 | + } |
| 114 | + |
| 115 | + const needsUpdate = currentVersion !== latestVersion |
| 116 | + log(`[auto-update-checker] Current: ${currentVersion}, Latest: ${latestVersion}, NeedsUpdate: ${needsUpdate}`) |
| 117 | + |
| 118 | + return { needsUpdate, currentVersion, latestVersion, isLocalDev: false } |
| 119 | +} |
0 commit comments