/**
 * This example shows how to use the gradle-node-plugin to install node and npm in separate directories
 * and use these binaries with the prettier formatter.
 */
plugins {
    id 'com.diffplug.spotless'
    id 'com.github.node-gradle.node' version '3.5.1'
}
repositories { mavenCentral() }
node {
    download = true
    version = '18.16.1'
    npmVersion = '9.5.1'
	// when setting both these directories, npm and node will be in separate directories
    workDir = file("${buildDir}/nodejs")
    npmWorkDir = file("${buildDir}/npm")
}
def prettierConfig = [:]
prettierConfig['printWidth'] = 20
prettierConfig['parser'] = 'typescript'

// the executable names
def npmExec = System.getProperty('os.name').toLowerCase().contains('windows') ? '/npm.cmd' : '/bin/npm'
def nodeExec = System.getProperty('os.name').toLowerCase().contains('windows') ? '/node.exe' : '/bin/node'

spotless {
    format 'mytypescript', {
        target 'test.ts'
        prettier()
            .npmExecutable("${tasks.named('npmSetup').get().npmDir.get()}${npmExec}") // get the npm executable path from gradle-node-plugin
            .nodeExecutable("${tasks.named('nodeSetup').get().nodeDir.get()}${nodeExec}") // get the node executable path from gradle-node-plugin
            .config(prettierConfig)
    }
}

tasks.named('spotlessMytypescript').configure {
    it.dependsOn('nodeSetup', 'npmSetup')
}
