fix: guard against missing brace in JSONC provider replacement
This commit is contained in:
parent
06d265c1de
commit
a1d7f9e822
@ -55,24 +55,28 @@ export function addProviderConfig(config: InstallConfig): ConfigMergeResult {
|
|||||||
const providerIdx = content.indexOf('"provider"')
|
const providerIdx = content.indexOf('"provider"')
|
||||||
if (providerIdx !== -1) {
|
if (providerIdx !== -1) {
|
||||||
const colonIdx = content.indexOf(":", providerIdx + '"provider"'.length)
|
const colonIdx = content.indexOf(":", providerIdx + '"provider"'.length)
|
||||||
const braceStart = content.indexOf("{", colonIdx)
|
const braceStart = colonIdx !== -1 ? content.indexOf("{", colonIdx) : -1
|
||||||
let depth = 0
|
if (braceStart === -1) {
|
||||||
let braceEnd = braceStart
|
writeFileSync(path, JSON.stringify(newConfig, null, 2) + "\n")
|
||||||
for (let i = braceStart; i < content.length; i++) {
|
} else {
|
||||||
if (content[i] === "{") depth++
|
let depth = 0
|
||||||
else if (content[i] === "}") {
|
let braceEnd = braceStart
|
||||||
depth--
|
for (let i = braceStart; i < content.length; i++) {
|
||||||
if (depth === 0) {
|
if (content[i] === "{") depth++
|
||||||
braceEnd = i
|
else if (content[i] === "}") {
|
||||||
break
|
depth--
|
||||||
|
if (depth === 0) {
|
||||||
|
braceEnd = i
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const newContent =
|
||||||
|
content.slice(0, providerIdx) +
|
||||||
|
`"provider": ${providerJson}` +
|
||||||
|
content.slice(braceEnd + 1)
|
||||||
|
writeFileSync(path, newContent)
|
||||||
}
|
}
|
||||||
const newContent =
|
|
||||||
content.slice(0, providerIdx) +
|
|
||||||
`"provider": ${providerJson}` +
|
|
||||||
content.slice(braceEnd + 1)
|
|
||||||
writeFileSync(path, newContent)
|
|
||||||
} else {
|
} else {
|
||||||
const newContent = content.replace(/(\{)/, `$1\n "provider": ${providerJson},`)
|
const newContent = content.replace(/(\{)/, `$1\n "provider": ${providerJson},`)
|
||||||
writeFileSync(path, newContent)
|
writeFileSync(path, newContent)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user