I have an OpenAPI schema, and I want to make sure that it produces valid TypeScript code.
Using the Gradle Node Plugin I can generate TypeScript code from the openapi.yml file by calling openapi-typescript in an npx task.
import com.github.gradle.node.npm.task.NpxTask
import org.jetbrains.kotlin.util.parseSpaceSeparatedArgs
plugins {
id("com.github.node-gradle.node")
}
val openApiGenerateSchemaDTs by tasks.registering(NpxTask::class) {
description = "Generate TypeScript OpenAPI Schema"
val openApiYml = layout.projectDirectory.file("src/main/resources/openapi.yml")
inputs.file(openApiYml)
val schemaDTsFile = temporaryDir.resolve("schema.d.ts")
outputs.file(schemaDTsFile)
command.set("[email protected]")
args.set(
parseSpaceSeparatedArgs(
"${openApiYml.asFile} --output $schemaDTsFile"
)
)
}
This correctly generates TypeScript code based on the schema. However, the generated code might be invalid. To verify that it is, I want to use tsc to compile the code.
I can do this from the command line with this command:
npx \
--yes \ # required, otherwise npx command won't run if TypeScript isn't installed
-p [email protected] \
tsc build/tmp/openApiGenerateSchemaDTs/schema.d.ts \
--listFiles \
--noEmit \
--strict
When I try and recreate this command in a Gradle NpxTask
import com.github.gradle.node.npm.task.NpxTask
import org.jetbrains.kotlin.util.parseSpaceSeparatedArgs
val openApiCompileSchemaDTs by tasks.registering(NpxTask::class) {
val openApiSchemaDTs = openApiGenerateSchemaDTs.map { it.outputs.files.singleFile }
inputs.file(openApiSchemaDTs)
command.set("--yes -p [email protected] tsc")
args.set(
parseSpaceSeparatedArgs(
"${openApiSchemaDTs.get()} --yes --listFiles --noEmit --strict"
)
)
}
Then I get an error
> Task :openApiCompileSchemaDTs FAILED
/var/folders/wl/2bzbzknd077dyh2ww_6xjty80000gp/T/npx667633524.sh: line 2: .../build/tmp/openApiGenerateSchemaDTs/schema.d.ts: Permission denied
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':openApiCompileSchemaDTs'.
> Process 'command 'npx'' finished with non-zero exit value 126
If I set the command to be tsc and move the options into args...
val openApiCompileSchemaDTs by tasks.registering(NpxTask::class) {
val openApiSchemaDTs = openApiGenerateSchemaDTs.map { it.outputs.files.singleFile }
inputs.file(openApiSchemaDTs)
command.set("tsc")
args.set(
parseSpaceSeparatedArgs(
"--yes -p [email protected] ${openApiSchemaDTs.get()} --yes --listFiles --noEmit --strict"
)
)
}
Then I get another error
> Task :openApiCompileSchemaDTs FAILED
This is not the tsc command you are looking for
To get access to the TypeScript compiler, tsc, from the command line either:
- Use npm install typescript to first add TypeScript to your project before using npx
- Use yarn to avoid accidentally running code from un-installed packages
I'm sure it's just a case of setting the correct command and arguments, but I've tried a few different combinations with no success.
Versions
- Gradle 7.5.1
- Gradle Node Plugin 3.4.0