UNPKG

@nx/gradle

Version:

The Nx Plugin for Gradle allows Gradle tasks to be run through Nx

85 lines (84 loc) 3.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getGradleExecFile = getGradleExecFile; exports.execGradleAsync = execGradleAsync; exports.findGraldewFile = findGraldewFile; const devkit_1 = require("@nx/devkit"); const node_child_process_1 = require("node:child_process"); const node_fs_1 = require("node:fs"); const node_path_1 = require("node:path"); const run_commands_impl_1 = require("nx/src/executors/run-commands/run-commands.impl"); /** * For gradle command, it needs to be run from the directory of the gradle binary * @returns gradle binary file name */ function getGradleExecFile() { return process.platform.startsWith('win') ? '.\\gradlew.bat' : './gradlew'; } /** * This function executes gradle with the given arguments * @param gradleBinaryPath absolute path to gradle binary * @param args args passed to gradle * @param execOptions exec options * @returns promise with the stdout buffer */ function execGradleAsync(gradleBinaryPath, args, execOptions = {}) { return new Promise((res, rej) => { const cp = (0, node_child_process_1.execFile)(gradleBinaryPath, args, { cwd: (0, node_path_1.dirname)(gradleBinaryPath), shell: true, windowsHide: true, env: process.env, maxBuffer: run_commands_impl_1.LARGE_BUFFER, ...execOptions, }); let stdout = Buffer.from(''); cp.stdout?.on('data', (data) => { stdout += data; }); cp.stderr?.on('data', (data) => { stdout += data; }); cp.on('exit', (code) => { if (code === 0) { res(stdout); } else { rej(stdout); } }); }); } /** * This function recursively finds the nearest gradlew file in the workspace * @param originalFileToSearch the original file to search for * @param wr workspace root * @param currentSearchPath the path to start searching for gradlew file * @returns the relative path of the gradlew file to workspace root, throws an error if gradlew file is not found * It will return gradlew.bat file on windows and gradlew file on other platforms */ function findGraldewFile(originalFileToSearch, wr = devkit_1.workspaceRoot, currentSearchPath) { currentSearchPath ??= originalFileToSearch; const parent = (0, node_path_1.dirname)(currentSearchPath); if (currentSearchPath === parent) { throw new devkit_1.AggregateCreateNodesError([ [ originalFileToSearch, new Error('No Gradlew file found. Run "gradle init"'), ], ], []); } const gradlewPath = (0, node_path_1.join)(parent, 'gradlew'); const gradlewBatPath = (0, node_path_1.join)(parent, 'gradlew.bat'); if (process.platform.startsWith('win')) { if ((0, node_fs_1.existsSync)((0, node_path_1.join)(wr, gradlewBatPath))) { return gradlewBatPath; } } else { if ((0, node_fs_1.existsSync)((0, node_path_1.join)(wr, gradlewPath))) { return gradlewPath; } } return findGraldewFile(originalFileToSearch, wr, parent); }