1- import { existsSync , readFileSync } from "node:fs"
2- import { homedir } from "node:os"
3- import { join } from "node:path"
41import type { CheckResult , CheckDefinition , VersionCheckInfo } from "../types"
5- import { CHECK_IDS , CHECK_NAMES , PACKAGE_NAME } from "../constants"
6- import { parseJsonc } from "../../../shared"
7-
8- const OPENCODE_CONFIG_DIR = join ( homedir ( ) , ".config" , "opencode" )
9- const OPENCODE_PACKAGE_JSON = join ( OPENCODE_CONFIG_DIR , "package.json" )
10- const OPENCODE_JSON = join ( OPENCODE_CONFIG_DIR , "opencode.json" )
11- const OPENCODE_JSONC = join ( OPENCODE_CONFIG_DIR , "opencode.jsonc" )
12-
13- async function fetchLatestVersion ( ) : Promise < string | null > {
14- try {
15- const res = await fetch ( `https://registry.npmjs.org/${ PACKAGE_NAME } /latest` , {
16- signal : AbortSignal . timeout ( 5000 ) ,
17- } )
18- if ( ! res . ok ) return null
19- const data = ( await res . json ( ) ) as { version : string }
20- return data . version
21- } catch {
22- return null
23- }
24- }
25-
26- function getCurrentVersion ( ) : {
27- version : string | null
28- isLocalDev : boolean
29- isPinned : boolean
30- pinnedVersion : string | null
31- } {
32- const configPath = existsSync ( OPENCODE_JSONC ) ? OPENCODE_JSONC : OPENCODE_JSON
33-
34- if ( ! existsSync ( configPath ) ) {
35- return { version : null , isLocalDev : false , isPinned : false , pinnedVersion : null }
36- }
37-
38- try {
39- const content = readFileSync ( configPath , "utf-8" )
40- const config = parseJsonc < { plugin ?: string [ ] } > ( content )
41- const plugins = config . plugin ?? [ ]
42-
43- for ( const plugin of plugins ) {
44- if ( plugin . startsWith ( "file:" ) && plugin . includes ( PACKAGE_NAME ) ) {
45- return { version : "local-dev" , isLocalDev : true , isPinned : false , pinnedVersion : null }
46- }
47- if ( plugin . startsWith ( `${ PACKAGE_NAME } @` ) ) {
48- const pinnedVersion = plugin . split ( "@" ) [ 1 ]
49- return { version : pinnedVersion , isLocalDev : false , isPinned : true , pinnedVersion }
50- }
51- if ( plugin === PACKAGE_NAME ) {
52- if ( existsSync ( OPENCODE_PACKAGE_JSON ) ) {
53- try {
54- const pkgContent = readFileSync ( OPENCODE_PACKAGE_JSON , "utf-8" )
55- const pkg = JSON . parse ( pkgContent ) as { dependencies ?: Record < string , string > }
56- const depVersion = pkg . dependencies ?. [ PACKAGE_NAME ]
57- if ( depVersion ) {
58- const cleanVersion = depVersion . replace ( / ^ [ \^ ~ ] / , "" )
59- return { version : cleanVersion , isLocalDev : false , isPinned : false , pinnedVersion : null }
60- }
61- } catch {
62- // intentionally empty - parse errors ignored
63- }
64- }
65- return { version : null , isLocalDev : false , isPinned : false , pinnedVersion : null }
66- }
67- }
68-
69- return { version : null , isLocalDev : false , isPinned : false , pinnedVersion : null }
70- } catch {
71- return { version : null , isLocalDev : false , isPinned : false , pinnedVersion : null }
72- }
73- }
2+ import { CHECK_IDS , CHECK_NAMES } from "../constants"
3+ import {
4+ getCachedVersion ,
5+ getLatestVersion ,
6+ isLocalDevMode ,
7+ findPluginEntry ,
8+ } from "../../../hooks/auto-update-checker/checker"
749
7510function compareVersions ( current : string , latest : string ) : boolean {
7611 const parseVersion = ( v : string ) : number [ ] => {
@@ -91,22 +26,43 @@ function compareVersions(current: string, latest: string): boolean {
9126}
9227
9328export async function getVersionInfo ( ) : Promise < VersionCheckInfo > {
94- const current = getCurrentVersion ( )
95- const latestVersion = await fetchLatestVersion ( )
29+ const cwd = process . cwd ( )
30+
31+ if ( isLocalDevMode ( cwd ) ) {
32+ return {
33+ currentVersion : "local-dev" ,
34+ latestVersion : null ,
35+ isUpToDate : true ,
36+ isLocalDev : true ,
37+ isPinned : false ,
38+ }
39+ }
40+
41+ const pluginInfo = findPluginEntry ( cwd )
42+ if ( pluginInfo ?. isPinned ) {
43+ return {
44+ currentVersion : pluginInfo . pinnedVersion ,
45+ latestVersion : null ,
46+ isUpToDate : true ,
47+ isLocalDev : false ,
48+ isPinned : true ,
49+ }
50+ }
51+
52+ const currentVersion = getCachedVersion ( )
53+ const latestVersion = await getLatestVersion ( )
9654
9755 const isUpToDate =
98- current . isLocalDev ||
99- current . isPinned ||
100- ! current . version ||
56+ ! currentVersion ||
10157 ! latestVersion ||
102- compareVersions ( current . version , latestVersion )
58+ compareVersions ( currentVersion , latestVersion )
10359
10460 return {
105- currentVersion : current . version ,
61+ currentVersion,
10662 latestVersion,
10763 isUpToDate,
108- isLocalDev : current . isLocalDev ,
109- isPinned : current . isPinned ,
64+ isLocalDev : false ,
65+ isPinned : false ,
11066 }
11167}
11268
0 commit comments