Kafkatorio/docs/notes/question-how-to-run-npx-gradle-task-with-kotlin-js.md
2021-12-28 17:05:52 +01:00

1.6 KiB

How I can run a npx Gradle task with Kotlin/JS?

I have a project that includes some dependencies downloaded from NPM and source code in Typescript.

I want to use TypeScriptToLua to convert my Typescript files into Lua.

I see that Kotlin/JS has two

b76cbdaa41/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/npm/NpmProject.kt (L86)

fun useTool(
  exec: ExecSpec,
  tool: String,
  nodeArgs: List<String> = listOf(),
  args: List<String>
) {
  exec.workingDir = dir
  exec.executable = nodeExecutable
  exec.args = nodeArgs + require(tool) + args
}

92d200e093/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/dukat/DukatRunner.kt (L47)

npmProject.useTool(
  exec,
  "dukat/bin/dukat-cli.js",
  listOf(),
  args
)

711b882e71/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/targets/js/nodejs/NodeJsExec.kt

In build.gradle.kts I've configured the Kotlin Multiplatform plugin to download the typescript-to-lua dependency

plugins {
  kotlin("multiplatform")
}

kotlin {
  js(IR) {
    browser()
  }

  sourceSets {
    val jsMain: KotlinSourceSet by getting {
      dependencies {
        implementation(npm("typescript-to-lua", "1.1.1"))
      }
    }
  }
}

and this correctly downloads the NPM module binaries: build/js/packages/data-model/node_modules/.bin/tstl