function updateChecksumsKtFile()

in common/update-cli.js [119:153]


function updateChecksumsKtFile(checksums, latestVersion) {
    const filepath = path.resolve(__dirname, `../${checksumsKtPath}`);
    if (fs.existsSync(filepath)) {
        console.log(`Checksums.kt file exists at ${filepath}`);
    } else {
        throw new Error(`Checksums.kt file not found at ${filepath}`);
    }

    let checksumsKtContent = fs.readFileSync(filepath, 'utf-8');
    console.log('Current Checksum File Content:', checksumsKtContent);

    const checksumsMapStart = checksumsKtContent.indexOf('val CHECKSUMS = mapOf(');
    const checksumsMapEnd = checksumsKtContent.lastIndexOf(')') + 1;

    const beforeChecksumsMap = checksumsKtContent.substring(0, checksumsMapStart);
    const afterChecksumsMap = checksumsKtContent.substring(checksumsMapEnd);

    const newChecksums = `    "${latestVersion}" to mapOf(\n` +
      checksums.map(
        ({platform, arch, checksum}) => `        "${platform}_${arch}" to "${checksum}"`
      ).join(',\n') +
      '\n    )';

    let updatedChecksumsMap = checksumsKtContent.substring(checksumsMapStart, checksumsMapEnd);

    // Insert the new checksums before the final closing parenthesis
    updatedChecksumsMap = updatedChecksumsMap.replace(/\n\)$/, `,\n${newChecksums}\n)`);

    // Combine all parts
    const updatedContent = beforeChecksumsMap + updatedChecksumsMap + afterChecksumsMap;

    // Write the updated content back to the file
    fs.writeFileSync(filepath, updatedContent);
    console.log('Checksums.kt file updated');
}