Gradle to JavaScript Object Parser
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
-
TypeError: g2js.parseFile is not a function
cause Attempting to call `parseFile` on an object imported from the root `gradle-to-js` package instead of the `lib/parser` module.fixEnsure you are importing from the correct path: `const g2js = require('gradle-to-js/lib/parser');` -
SyntaxError: Cannot use import statement outside a module
cause Trying to use ESM `import` syntax for `gradle-to-js` in a Node.js environment that expects CommonJS modules.fixUse CommonJS `require()` syntax: `const g2js = require('gradle-to-js/lib/parser');` -
Error: ENOENT: no such file or directory, open 'path/to/buildfile'
cause The file path provided to `g2js.parseFile()` does not point to an existing or accessible file.fixDouble-check the file path. Ensure it's correct and that the Node.js process has read permissions for the specified file.
Warnings
- gotcha The parser is explicitly 'quick & dirty' and does not evaluate Gradle runtime logic, Groovy closures, or dynamic expressions within the build file. It produces a static, structural representation, which may not fully reflect the build file's behavior during actual Gradle execution.
- gotcha This library is primarily a CommonJS module. Direct `import` statements (ESM syntax) will likely lead to `SyntaxError: Cannot use import statement outside a module` in Node.js projects not explicitly configured for CJS/ESM interoperation.
- gotcha The primary entry point for the parser functionality is `gradle-to-js/lib/parser`, not the root `gradle-to-js` package. Attempting to `require('gradle-to-js')` directly will likely result in an object without the `parseFile` or `parseText` methods.
- gotcha The CLI usage examples demonstrate running the `index.js` directly via `./index.js` or `node ./index.js`. This implies the CLI might not be installed as a globally available command without manual `npm link` or an explicit `bin` entry in `package.json`.
Install
-
npm install gradle-to-js -
yarn add gradle-to-js -
pnpm add gradle-to-js
Imports
- parser
import g2js from 'gradle-to-js/lib/parser';
const g2js = require('gradle-to-js/lib/parser'); - parseFile
const { parseFile } = require('gradle-to-js/lib/parser');g2js.parseFile('path/to/buildfile').then(...); - parseText
const { parseText } = require('gradle-to-js/lib/parser');g2js.parseText('key "value"').then(...);
Quickstart
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);
});