mirror of
https://github.com/anomalyco/opencode.git
synced 2026-02-01 22:48:16 +00:00
Apply PR #11402: refactor: unify path resolve
This commit is contained in:
@@ -1164,10 +1164,7 @@ export namespace Config {
|
||||
if (lineIndex !== -1 && lines[lineIndex].trim().startsWith("//")) {
|
||||
continue // Skip if line is commented
|
||||
}
|
||||
let filePath = match.replace(/^\{file:/, "").replace(/\}$/, "")
|
||||
if (filePath.startsWith("~/")) {
|
||||
filePath = path.join(os.homedir(), filePath.slice(2))
|
||||
}
|
||||
const filePath = Filesystem.resolveTilde(match.replace(/^\{file:/, "").replace(/\}$/, ""))
|
||||
const resolvedPath = path.isAbsolute(filePath) ? filePath : path.resolve(configDir, filePath)
|
||||
const fileContent = (
|
||||
await Bun.file(resolvedPath)
|
||||
@@ -1224,14 +1221,26 @@ export namespace Config {
|
||||
await Bun.write(configFilepath, updated).catch(() => {})
|
||||
}
|
||||
const data = parsed.data
|
||||
if (data.plugin) {
|
||||
for (let i = 0; i < data.plugin.length; i++) {
|
||||
const plugin = data.plugin[i]
|
||||
try {
|
||||
data.plugin[i] = import.meta.resolve!(plugin, configFilepath)
|
||||
} catch (err) {}
|
||||
const expand = (arr?: string[]) => arr?.forEach((v, i) => (arr[i] = Filesystem.resolveTilde(v)))
|
||||
|
||||
expand(data.instructions)
|
||||
expand(data.plugin)
|
||||
if (data.skills) expand(data.skills.paths)
|
||||
|
||||
for (const mcp of Object.values(data.mcp ?? {}))
|
||||
if (typeof mcp === "object" && "type" in mcp && mcp.type === "local") expand(mcp.command)
|
||||
for (const lsp of Object.values(data.lsp ?? {}))
|
||||
if (typeof lsp === "object" && "command" in lsp) expand(lsp.command)
|
||||
for (const fmt of Object.values(data.formatter ?? {}))
|
||||
if (typeof fmt === "object" && "command" in fmt) expand(fmt.command)
|
||||
|
||||
data.plugin = data.plugin?.map((p: string) => {
|
||||
try {
|
||||
return import.meta.resolve!(p, configFilepath)
|
||||
} catch {
|
||||
return p
|
||||
}
|
||||
}
|
||||
})
|
||||
return data
|
||||
}
|
||||
|
||||
|
||||
@@ -93,9 +93,6 @@ export namespace InstructionPrompt {
|
||||
if (config.instructions) {
|
||||
for (let instruction of config.instructions) {
|
||||
if (instruction.startsWith("https://") || instruction.startsWith("http://")) continue
|
||||
if (instruction.startsWith("~/")) {
|
||||
instruction = path.join(os.homedir(), instruction.slice(2))
|
||||
}
|
||||
const matches = path.isAbsolute(instruction)
|
||||
? await Array.fromAsync(
|
||||
new Bun.Glob(path.basename(instruction)).scan({
|
||||
|
||||
@@ -129,8 +129,7 @@ export namespace Skill {
|
||||
// Scan additional skill paths from config
|
||||
const config = await Config.get()
|
||||
for (const skillPath of config.skills?.paths ?? []) {
|
||||
const expanded = skillPath.startsWith("~/") ? path.join(os.homedir(), skillPath.slice(2)) : skillPath
|
||||
const resolved = path.isAbsolute(expanded) ? expanded : path.join(Instance.directory, expanded)
|
||||
const resolved = path.isAbsolute(skillPath) ? skillPath : path.join(Instance.directory, skillPath)
|
||||
if (!(await Filesystem.isDir(resolved))) {
|
||||
log.warn("skill path not found", { path: resolved })
|
||||
continue
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
import { realpathSync } from "fs"
|
||||
import { dirname, join, relative } from "path"
|
||||
import os from "os"
|
||||
|
||||
export namespace Filesystem {
|
||||
export function resolveTilde(p: string): string {
|
||||
return p.startsWith("~/") ? join(os.homedir(), p.slice(2)) : p
|
||||
}
|
||||
|
||||
export const exists = (p: string) =>
|
||||
Bun.file(p)
|
||||
.stat()
|
||||
|
||||
Reference in New Issue
Block a user