Update Node.js to v20.19.0 and npm to v10.2.3; refactor npmBuild task to use plugin-provided npmCommand and add nodeVersionCheck task for environment validation.

This commit is contained in:
akastijn 2025-07-30 00:08:00 +02:00
parent 291c9df5c6
commit 871615702b

View File

@ -8,8 +8,9 @@ plugins {
node {
download.set(true)
version.set("22.14.0")
npmVersion.set("10.9.2")
// Update to the version that's compatible with your environment requirements
version.set("20.19.0")
npmVersion.set("10.2.3") // A compatible npm version for Node.js 20.19.0
workDir.set(file("${project.projectDir}/node"))
npmWorkDir.set(file("${project.projectDir}/node"))
}
@ -21,38 +22,37 @@ tasks.register<Delete>("cleanDist") {
}
// Create a task that will run npm build
tasks.register("npmBuild") {
tasks.register<com.github.gradle.node.npm.task.NpmTask>("npmBuild") {
description = "Run 'npm run build'"
group = "build"
doLast {
// Use nodeCommand directly from the plugin
project.exec {
workingDir(project.projectDir)
// Use node's npm to ensure it works on all environments
val nodeDir = "${project.projectDir}/node"
// Determine which build script to run based on the OS
val isWindows = System.getProperty("os.name").lowercase().contains("windows")
if (isWindows) {
val npmCmd = file(nodeDir).listFiles()?.find { it.name.startsWith("npm") && it.isDirectory }?.let {
"${it.absolutePath}/npm.cmd"
} ?: "$nodeDir/node_modules/npm/bin/npm.cmd"
commandLine(npmCmd, "run", "build:dev")
} else {
val npmExecutable = file(nodeDir).listFiles()?.find { it.name.startsWith("npm") && it.isDirectory }?.let {
"${it.absolutePath}/bin/npm"
} ?: "$nodeDir/node_modules/npm/bin/npm"
commandLine(npmExecutable, "run", "build:beta")
}
}
}
npmCommand.set(listOf("run", if (isWindows) "build:dev" else "build:beta"))
dependsOn("npmInstall")
}
// Add a new task to check Node.js and npm versions
tasks.register<com.github.gradle.node.task.NodeTask>("nodeVersionCheck") {
description = "Check Node.js and npm versions"
script.set(file("${projectDir}/node-version-check.js"))
doFirst {
// Create a temporary script to check versions
file("${projectDir}/node-version-check.js").writeText("""
console.log('Node.js version:', process.version);
console.log('npm version:', require('npm/package.json').version);
console.log('Build command that would be used:', process.platform === 'win32' ? 'build:dev' : 'build:beta');
""".trimIndent())
}
doLast {
// Clean up the temporary script
delete("${projectDir}/node-version-check.js")
}
}
tasks.named("assemble") {
dependsOn("npmBuild")
}