diff --git a/infrastructure/zk/src/init.ts b/infrastructure/zk/src/init.ts index b1761d1bd61..8a0b2ea1a8f 100644 --- a/infrastructure/zk/src/init.ts +++ b/infrastructure/zk/src/init.ts @@ -140,31 +140,55 @@ export async function submoduleUpdate() { await utils.exec('git submodule update'); } -function updateConfigFile(path: string, modeConstantValues: Record) { +interface ConfigLine { + key: string; + value: string | number | null; + section: string | null; + lineIndex?: number | null; +} + +function updateConfigFile(path: string, modeConstantValues: ConfigLine[]) { let content = fs.readFileSync(path, 'utf-8'); let lines = content.split('\n'); let addedContent: string | undefined; const lineIndices: Record = {}; + // Iterate through each line in the file for (let i = 0; i < lines.length; i++) { const line = lines[i]; + + // Check if the line does not start with '#' (comment) if (!line.startsWith('#')) { + // Using regex to match key-value pairs in the line const match = line.match(/([^=]+)=(.*)/); + if (match) { + // Extract the key and trim any leading/trailing whitespaces const key = match[1].trim(); + + // Store the index of the line where the key is found lineIndices[key] = i; } } } - for (const [key, value] of Object.entries(modeConstantValues)) { - const lineIndex = lineIndices[key]; + + // Iterate through each key-value pair in modeConstantValues + modeConstantValues.forEach((configLine) =>{ + // Get the line index for the current key from the record + const lineIndex = lineIndices[configLine.key]; + + // Check if [key, value] to append/remove is found in the file if (lineIndex !== undefined) { - if (value !== null) { - lines.splice(lineIndex, 1, `${key}=${value}`); + // If the value to insert is not null, update the line with the new value + if (configLine.value !== null) { + lines.splice(lineIndex, 1, `${configLine.key}=${configLine.value}`); } else { + // If the value is null, remove the line and update line indices lines.splice(lineIndex, 1); + + // Update line indices for keys that appear after the removed line for (const [k, index] of Object.entries(lineIndices)) { if (index > lineIndex) { lineIndices[k] = index - 1; @@ -172,54 +196,98 @@ function updateConfigFile(path: string, modeConstantValues: Record