import groovy.json.JsonSlurper
/*
 * This file was generated by the Gradle 'init' task.
 *
 * This is a general purpose Gradle build.
 * To learn more about Gradle by exploring our Samples at https://docs.gradle.org/8.7/samples
 */

def getLatestNodeVersionForMajor(int majorVersion) {
    def nodeDistUrl = 'https://nodejs.org/dist/index.json'
    def jsonSlurper = new JsonSlurper()
    def nodeVersions = jsonSlurper.parseText(nodeDistUrl.toURL().text)

    nodeVersions.find { it.version.startsWith("v${majorVersion}.") }.version.substring(1)
}

plugins {
    id "com.dorongold.task-tree" version "3.0.0"
    id "com.github.node-gradle.node" version "7.0.2"
}

node {
    download = true
    version = getLatestNodeVersionForMajor(20)
    npmInstallCommand = 'clean-install'
}

npmInstall {
    inputs.files file('package.json'), file('package-lock.json')
    outputs.dir file('node_modules')
}

task createOutDir {
    doLast {
        file("out").mkdirs()
    }
}

task compileAdblockList(type: NpmTask) {
    dependsOn npmInstall, createOutDir
    args = ['run', 'compile']
}