plugins {
    id 'base'
}

// Fake NPM task that would normally execute npm with its provided arguments
class NpmTask extends DefaultTask {

    final ListProperty<String> args = project.objects.listProperty(String)

    @TaskAction
    void run() {
        project.file("$project.buildDir/bundle.js").withOutputStream { stream ->
            project.file("scripts").listFiles().sort().each {
                stream.write(it.bytes)
            }
        }
    }
}

// tag::bundle-task[]
task bundle(type: NpmTask) {
    args = ['run', 'bundle']

    outputs.cacheIf { true }

    inputs.dir(file("scripts"))
        .withPropertyName("scripts")
        .withPathSensitivity(PathSensitivity.RELATIVE)

    inputs.files("package.json", "package-lock.json")
        .withPropertyName("configFiles")
        .withPathSensitivity(PathSensitivity.RELATIVE)

    outputs.file("$buildDir/bundle.js")
        .withPropertyName("bundle")
}
// end::bundle-task[]

task printBundle {
    dependsOn bundle
    doLast {
        println file("$buildDir/bundle.js").text
    }
}
