fix: guard against missing brace in JSONC provider replacement
This commit is contained in:
parent
06d265c1de
commit
a1d7f9e822
@ -55,7 +55,10 @@ 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
|
||||||
|
if (braceStart === -1) {
|
||||||
|
writeFileSync(path, JSON.stringify(newConfig, null, 2) + "\n")
|
||||||
|
} else {
|
||||||
let depth = 0
|
let depth = 0
|
||||||
let braceEnd = braceStart
|
let braceEnd = braceStart
|
||||||
for (let i = braceStart; i < content.length; i++) {
|
for (let i = braceStart; i < content.length; i++) {
|
||||||
@ -73,6 +76,7 @@ export function addProviderConfig(config: InstallConfig): ConfigMergeResult {
|
|||||||
`"provider": ${providerJson}` +
|
`"provider": ${providerJson}` +
|
||||||
content.slice(braceEnd + 1)
|
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},`)
|
||||||
writeFileSync(path, newContent)
|
writeFileSync(path, newContent)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user