Gradle to JavaScript Object Parser

2.0.1 · maintenance · verified Sun Apr 19

gradle-to-js is a JavaScript library designed to parse Gradle build files (`.gradle`) into plain JavaScript objects. Currently at version 2.0.1, it offers a 'quick & dirty' approach, focusing on extracting key-value pairs and block structures rather than executing or accurately representing runtime evaluations within the Gradle script. This means complex logic, closures, or dynamic expressions found in Gradle files will not be processed, resulting in a simplified, static representation. The library primarily supports CommonJS imports and is best suited for scenarios where a basic, structural understanding of a Gradle file is needed, rather than a full, executable interpretation. Its release cadence appears infrequent, and it is likely in maintenance mode, reflecting its specific and somewhat limited parsing scope.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to parse a Gradle build file into a JavaScript object using `parseFile` and then logs the structured representation. It includes setup for a temporary file and error handling.

const g2js = require('gradle-to-js/lib/parser');
const fs = require('fs');
const path = require('path');

// Create a dummy Gradle file for demonstration
const tempGradleFile = path.join(__dirname, 'temp.gradle');
fs.writeFileSync(tempGradleFile, `
apply plugin: 'java'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.slf4j:slf4j-api:1.7.30'
    testImplementation 'junit:junit:4.13'
}

myBlock {
    key1 "value1"
    key2 123
}
`);

g2js.parseFile(tempGradleFile)
  .then(function(representation) {
    console.log('Parsed Gradle file representation:');
    console.log(JSON.stringify(representation, null, 2));
  })
  .catch(function(error) {
    console.error('Error parsing Gradle file:', error);
  })
  .finally(() => {
    // Clean up the dummy file
    fs.unlinkSync(tempGradleFile);
  });

view raw JSON →