fun addTempNpmFixes()

in kotlin-tanstack-history/karakum/build.gradle.kts [21:56]


fun addTempNpmFixes() {
    val file = file("build/js/node_modules/@tanstack/history/dist/esm/index.d.ts")
    var content = file.readText()

    if (": Int" in content)
        return

    content = content
        .replace("Promise<ShouldAllowNavigation> | ShouldAllowNavigation", "PromiseResult<ShouldAllowNavigation>")
        .replace(": number", ": Int")
        .replace("=> number", "=> Int")

    val oldRouterHistory = content
        .substringAfter("export interface RouterHistory {\n")
        .substringBefore("\n}\n")

    val newRouterHistory = oldRouterHistory
        .splitToSequence("\n")
        .joinToString("\n") {
            if (": (" in it) {
                val result = it.replaceFirst(": (", "(")
                if (") => () => void;" in result) {
                    result.replace(") => () => void;", "): () => void;")
                } else {
                    result.replace(") => ", "): ")
                }
            } else {
                "readonly $it"
            }
        }

    content = content
        .replace(oldRouterHistory, newRouterHistory)

    file.writeText(content)
}