Skip to content

Commit 5dd4d97

Browse files
authored
fix(auto-update-checker): resolve unknown version display and improve JSONC parsing (#54)
1 parent b1abb79 commit 5dd4d97

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/hooks/auto-update-checker/checker.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
NPM_FETCH_TIMEOUT,
99
INSTALLED_PACKAGE_JSON,
1010
USER_OPENCODE_CONFIG,
11+
USER_OPENCODE_CONFIG_JSONC,
1112
} from "./constants"
1213
import { log } from "../../shared/logger"
1314

@@ -16,13 +17,22 @@ export function isLocalDevMode(directory: string): boolean {
1617
}
1718

1819
function stripJsonComments(json: string): string {
19-
return json.replace(/^\s*\/\/.*$/gm, "").replace(/,(\s*[}\]])/g, "$1")
20+
return json
21+
.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (m, g) => (g ? "" : m))
22+
.replace(/,(\s*[}\]])/g, "$1")
2023
}
2124

22-
export function getLocalDevPath(directory: string): string | null {
23-
const projectConfig = path.join(directory, ".opencode", "opencode.json")
25+
function getConfigPaths(directory: string): string[] {
26+
return [
27+
path.join(directory, ".opencode", "opencode.json"),
28+
path.join(directory, ".opencode", "opencode.jsonc"),
29+
USER_OPENCODE_CONFIG,
30+
USER_OPENCODE_CONFIG_JSONC,
31+
]
32+
}
2433

25-
for (const configPath of [projectConfig, USER_OPENCODE_CONFIG]) {
34+
export function getLocalDevPath(directory: string): string | null {
35+
for (const configPath of getConfigPaths(directory)) {
2636
try {
2737
if (!fs.existsSync(configPath)) continue
2838
const content = fs.readFileSync(configPath, "utf-8")
@@ -31,7 +41,11 @@ export function getLocalDevPath(directory: string): string | null {
3141

3242
for (const entry of plugins) {
3343
if (entry.startsWith("file://") && entry.includes(PACKAGE_NAME)) {
34-
return entry.replace("file://", "")
44+
try {
45+
return fileURLToPath(entry)
46+
} catch {
47+
return entry.replace("file://", "")
48+
}
3549
}
3650
}
3751
} catch {
@@ -86,9 +100,7 @@ export interface PluginEntryInfo {
86100
}
87101

88102
export function findPluginEntry(directory: string): PluginEntryInfo | null {
89-
const projectConfig = path.join(directory, ".opencode", "opencode.json")
90-
91-
for (const configPath of [projectConfig, USER_OPENCODE_CONFIG]) {
103+
for (const configPath of getConfigPaths(directory)) {
92104
try {
93105
if (!fs.existsSync(configPath)) continue
94106
const content = fs.readFileSync(configPath, "utf-8")
@@ -170,7 +182,6 @@ export async function checkForUpdate(directory: string): Promise<UpdateCheckResu
170182
return { needsUpdate: false, currentVersion: null, latestVersion: null, isLocalDev: false, isPinned: false }
171183
}
172184

173-
// Respect version pinning
174185
if (pluginInfo.isPinned) {
175186
log(`[auto-update-checker] Version pinned to ${pluginInfo.pinnedVersion}, skipping update check`)
176187
return { needsUpdate: false, currentVersion: pluginInfo.pinnedVersion, latestVersion: null, isLocalDev: false, isPinned: true }
@@ -190,6 +201,5 @@ export async function checkForUpdate(directory: string): Promise<UpdateCheckResu
190201

191202
const needsUpdate = currentVersion !== latestVersion
192203
log(`[auto-update-checker] Current: ${currentVersion}, Latest: ${latestVersion}, NeedsUpdate: ${needsUpdate}`)
193-
194204
return { needsUpdate, currentVersion, latestVersion, isLocalDev: false, isPinned: false }
195205
}

src/hooks/auto-update-checker/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ function getUserConfigDir(): string {
3838

3939
export const USER_CONFIG_DIR = getUserConfigDir()
4040
export const USER_OPENCODE_CONFIG = path.join(USER_CONFIG_DIR, "opencode", "opencode.json")
41+
export const USER_OPENCODE_CONFIG_JSONC = path.join(USER_CONFIG_DIR, "opencode", "opencode.jsonc")

0 commit comments

Comments
 (0)