fix(shared): memoize detectPluginConfigFile per process

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
YeonGyu-Kim
2026-04-18 14:12:55 +09:00
parent 948343ab66
commit 6dc2234d89
+28 -10
View File
@@ -9,6 +9,14 @@ export interface JsoncParseResult<T> {
errors: Array<{ message: string; offset: number; length: number }>
}
type DetectPluginConfigResult = {
format: "json" | "jsonc" | "none"
path: string
legacyPath?: string
}
const pluginConfigFileDetectionCache = new Map<string, DetectPluginConfigResult>()
function stripBom(content: string): string {
return content.charCodeAt(0) === 0xfeff ? content.slice(1) : content
}
@@ -75,24 +83,34 @@ export function detectConfigFile(basePath: string): {
return { format: "none", path: jsonPath }
}
export function detectPluginConfigFile(dir: string): {
format: "json" | "jsonc" | "none"
path: string
legacyPath?: string
} {
export function clearPluginConfigFileDetectionCache(): void {
pluginConfigFileDetectionCache.clear()
}
export function detectPluginConfigFile(dir: string): DetectPluginConfigResult {
const cachedResult = pluginConfigFileDetectionCache.get(dir)
if (cachedResult !== undefined) {
return cachedResult
}
const canonicalResult = detectConfigFile(join(dir, CONFIG_BASENAME))
const legacyResult = detectConfigFile(join(dir, LEGACY_CONFIG_BASENAME))
let detectionResult: DetectPluginConfigResult
if (canonicalResult.format !== "none") {
return {
detectionResult = {
...canonicalResult,
legacyPath: legacyResult.format !== "none" ? legacyResult.path : undefined,
}
} else if (legacyResult.format !== "none") {
detectionResult = legacyResult
} else {
detectionResult = { format: "none", path: join(dir, `${CONFIG_BASENAME}.json`) }
}
if (legacyResult.format !== "none") {
return legacyResult
}
pluginConfigFileDetectionCache.set(dir, detectionResult)
return { format: "none", path: join(dir, `${CONFIG_BASENAME}.json`) }
return detectionResult
}