fix: use brace-depth matching for JSONC provider replacement instead of fragile regex
This commit is contained in:
@@ -47,13 +47,31 @@ export function addProviderConfig(config: InstallConfig): ConfigMergeResult {
|
|||||||
|
|
||||||
if (format === "jsonc") {
|
if (format === "jsonc") {
|
||||||
const content = readFileSync(path, "utf-8")
|
const content = readFileSync(path, "utf-8")
|
||||||
const providerRegex = /"provider"\s*:\s*\{[\s\S]*?\n \}/
|
|
||||||
const providerJson = JSON.stringify(newConfig.provider, null, 2)
|
const providerJson = JSON.stringify(newConfig.provider, null, 2)
|
||||||
.split("\n")
|
.split("\n")
|
||||||
.map((line, i) => (i === 0 ? line : ` ${line}`))
|
.map((line, i) => (i === 0 ? line : ` ${line}`))
|
||||||
.join("\n")
|
.join("\n")
|
||||||
if (providerRegex.test(content)) {
|
// Match "provider" key with any indentation and nested brace depth
|
||||||
const newContent = content.replace(providerRegex, `"provider": ${providerJson}`)
|
const providerIdx = content.indexOf('"provider"')
|
||||||
|
if (providerIdx !== -1) {
|
||||||
|
const colonIdx = content.indexOf(":", providerIdx + '"provider"'.length)
|
||||||
|
const braceStart = content.indexOf("{", colonIdx)
|
||||||
|
let depth = 0
|
||||||
|
let braceEnd = braceStart
|
||||||
|
for (let i = braceStart; i < content.length; i++) {
|
||||||
|
if (content[i] === "{") depth++
|
||||||
|
else if (content[i] === "}") {
|
||||||
|
depth--
|
||||||
|
if (depth === 0) {
|
||||||
|
braceEnd = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const newContent =
|
||||||
|
content.slice(0, providerIdx) +
|
||||||
|
`"provider": ${providerJson}` +
|
||||||
|
content.slice(braceEnd + 1)
|
||||||
writeFileSync(path, newContent)
|
writeFileSync(path, newContent)
|
||||||
} else {
|
} else {
|
||||||
const newContent = content.replace(/(\{)/, `$1\n "provider": ${providerJson},`)
|
const newContent = content.replace(/(\{)/, `$1\n "provider": ${providerJson},`)
|
||||||
|
|||||||
Reference in New Issue
Block a user