mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
81 lines
2.8 KiB
TypeScript
81 lines
2.8 KiB
TypeScript
export * as ConfigVariable from "./variable.js"
|
|
|
|
import os from "os"
|
|
import path from "path"
|
|
import { Effect } from "effect"
|
|
import { type ParseError, parse } from "jsonc-parser"
|
|
import { FSUtil } from "@opencode/util/fs-util"
|
|
|
|
type ParseSource =
|
|
| {
|
|
type: "path"
|
|
path: string
|
|
}
|
|
| {
|
|
type: "virtual"
|
|
source: string
|
|
dir: string
|
|
}
|
|
|
|
type SubstituteInput = ParseSource & {
|
|
text: string
|
|
env?: Record<string, string>
|
|
}
|
|
|
|
/** Apply {env:VAR} and {file:path} substitutions to config text. */
|
|
export const substitute = Effect.fn("ConfigVariable.substitute")(function* (input: SubstituteInput) {
|
|
const env = (escape: boolean) =>
|
|
input.text.replace(/\{env:([^}]+)\}/g, (_, name: string) => {
|
|
const value = (input.env?.[name] ?? process.env[name]) || ""
|
|
return escape ? JSON.stringify(value).slice(1, -1) : value
|
|
})
|
|
// Escape only when raw values break the JSON (e.g. Windows paths), so configs that parse today keep their meaning.
|
|
const raw = yield* substituteFiles(input, env(false))
|
|
const errors: ParseError[] = []
|
|
if (input.text.includes("{env:")) parse(raw, errors, { allowTrailingComma: true })
|
|
if (!errors.length) return raw
|
|
return yield* substituteFiles(input, env(true))
|
|
})
|
|
|
|
const substituteFiles = Effect.fnUntraced(function* (input: SubstituteInput, text: string) {
|
|
const fs = yield* FSUtil.Service
|
|
const configDir = input.type === "path" ? path.dirname(input.path) : input.dir
|
|
const configSource = input.type === "path" ? input.path : input.source
|
|
let out = ""
|
|
let cursor = 0
|
|
|
|
for (const match of text.matchAll(/\{file:[^}]+\}/g)) {
|
|
const token = match[0]
|
|
const index = match.index
|
|
out += text.slice(cursor, index)
|
|
|
|
const lineStart = text.lastIndexOf("\n", index - 1) + 1
|
|
const prefix = text.slice(lineStart, index).trimStart()
|
|
if (prefix.startsWith("//")) {
|
|
out += token
|
|
cursor = index + token.length
|
|
continue
|
|
}
|
|
|
|
const filePath = token.slice("{file:".length, -1)
|
|
const expandedPath = filePath.startsWith("~/") ? path.join(os.homedir(), filePath.slice(2)) : filePath
|
|
const resolvedPath = path.isAbsolute(expandedPath) ? expandedPath : path.resolve(configDir, expandedPath)
|
|
// Like a missing {env:VAR}, a missing or unreadable file substitutes empty so one stale secret
|
|
// reference cannot take down every Location that loads this config.
|
|
const fileContent = yield* fs.readFileString(resolvedPath).pipe(
|
|
Effect.catch((cause) =>
|
|
Effect.logWarning("config file reference is missing or unreadable", {
|
|
source: configSource,
|
|
reference: token,
|
|
path: resolvedPath,
|
|
cause,
|
|
}).pipe(Effect.as("")),
|
|
),
|
|
)
|
|
|
|
out += JSON.stringify(fileContent.trim()).slice(1, -1)
|
|
cursor = index + token.length
|
|
}
|
|
|
|
return out + text.slice(cursor)
|
|
})
|