Skip to content

zucchero-sintattico/typescript-gradle-plugin

Repository files navigation

Typescript Gradle Plugin

Goal

Integrate typescript compilation in a gradle project.

Description

The plugin allow to compile typescript files in a gradle project, producing javascript files from the typescript ones. Also, it can be used to run directly the typescript file.

Node

In addition, the plugin let you manage the node installation. You can pick the version of node to install and the plugin will download it for you.

Usage

Just apply it to your project:

plugins {
    id("io.github.zucchero-sintattico.typescript-gradle-plugin") version "<version>"
}

Note: you should create a package.json and tsconfig.json in the root of your project with your desired configuration.

Other examples

During the compilation, will run the command npm run build:

typescript {
    buildCommandExecutable = BuildCommandExecutable.NPM
    buildCommand = "run build"
}

Configuration

The plugin could be configured as follows (default values are shown):

typescript {
    entrypoint = "app.js"            // the entrypoint for execution within the output directory
    outputDir = "build/dist"         // the output directory
    tsConfig = "tsconfig.json"       // the tsconfig file
    buildCommandExecutable = DEFAULT // (DEFAULT, NODE, NPM, NPX) the executable for custom build command
    buildCommand = ""                // the custom command to run during the compilation
}

node {
    shouldInstall = false        // if true, the plugin will download the specified version of node

    // If specified both, zipUrl will be used
    zipUrl = ""                  // the url to download the node zip
    ---OR---
    version = "21.7.1"          // the version of node to download
}

Available tasks

  • CheckNode: checks if node is installed. If specified, it will download it.
  • npmDependencies: installs the npm dependencies from the package.json.
  • compileTypescript: compiles the typescript file. Output is in the outputDir. If buildCommandExecutable is not DEFAULT, it will run the specified command in buildCommand using the specified executable in buildCommandExecutable
  • runJS: runs the javascript file specified in the entrypoint.
  • build: alias for compileTypescript.

Implementation

Notes on the implementation

The critical part of the plugin is the node installation one. The plugin will download the node distribution zip in a temporary directory and extract it in the project build directory. Given that the node installation is a one-time operation, avoiding the download every time. Instead, the plugin will check if the node is installed and skip the download if it is already there.

In order to get the exact path of the node installation at runtime every run, the plugin will save it in a file in the project build directory (nodePaths.properties). This allow the plugin to get the path between different executions.

The shell command execution, in order to run typescript and node commands, is done using this utility library.

Tasks dependencies

graph TD
    npmDependencies --> CheckNode
    compileTypescript --> npmDependencies
    build --> compileTypescript
    runJS --> compileTypescript
Loading