2026-02-08 15:01:42 +09:00
|
|
|
import * as fs from "node:fs"
|
|
|
|
|
import type { OpencodeConfig } from "../types"
|
|
|
|
|
import { PACKAGE_NAME } from "../constants"
|
|
|
|
|
import { getConfigPaths } from "./config-paths"
|
|
|
|
|
import { stripJsonComments } from "./jsonc-strip"
|
|
|
|
|
|
|
|
|
|
export interface PluginEntryInfo {
|
|
|
|
|
entry: string
|
|
|
|
|
isPinned: boolean
|
|
|
|
|
pinnedVersion: string | null
|
|
|
|
|
configPath: string
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 07:42:08 -06:00
|
|
|
const EXACT_SEMVER_REGEX = /^\d+\.\d+\.\d+(-[\w.]+)?$/
|
2026-02-21 02:24:43 +09:00
|
|
|
|
2026-02-08 15:01:42 +09:00
|
|
|
export function findPluginEntry(directory: string): PluginEntryInfo | null {
|
|
|
|
|
for (const configPath of getConfigPaths(directory)) {
|
|
|
|
|
try {
|
|
|
|
|
if (!fs.existsSync(configPath)) continue
|
|
|
|
|
const content = fs.readFileSync(configPath, "utf-8")
|
|
|
|
|
const config = JSON.parse(stripJsonComments(content)) as OpencodeConfig
|
|
|
|
|
const plugins = config.plugin ?? []
|
|
|
|
|
|
|
|
|
|
for (const entry of plugins) {
|
|
|
|
|
if (entry === PACKAGE_NAME) {
|
|
|
|
|
return { entry, isPinned: false, pinnedVersion: null, configPath }
|
|
|
|
|
}
|
|
|
|
|
if (entry.startsWith(`${PACKAGE_NAME}@`)) {
|
|
|
|
|
const pinnedVersion = entry.slice(PACKAGE_NAME.length + 1)
|
2026-03-11 07:42:08 -06:00
|
|
|
const isPinned = EXACT_SEMVER_REGEX.test(pinnedVersion.trim())
|
2026-02-21 02:24:43 +09:00
|
|
|
return { entry, isPinned, pinnedVersion, configPath }
|
2026-02-08 15:01:42 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null
|
|
|
|
|
}
|