const { execSync } = require("child_process") const fs = require("fs") const path = require("path") const os = require("os") const download = require("download") const AdmZip = require("adm-zip") const JAVA_DOWNLOAD_URL = { win32: "https://download.oracle.com/java/17/archive/jdk-17.0.12_windows-x64_bin.zip", darwin: "https://download.oracle.com/java/17/archive/jdk-17.0.12_macos-x64_bin.tar.gz", linux: "https://download.oracle.com/java/17/archive/jdk-17.0.12_linux-aarch64_bin.tar.gz", } function checkJavaVersion(requiredVersion = "17") { try { const javaVersionOutput = execSync("java -version", { encoding: "utf8", stdio: "pipe" }) const versionMatch = javaVersionOutput.match(/"(\d+\.\d+)(\.\d+)?_\d+"/) if (versionMatch) { const installedVersion = versionMatch[1] console.log(`Java is installed. Version: ${installedVersion}`) if (parseFloat(installedVersion) >= parseFloat(requiredVersion)) { console.log("Required Java version is already installed.") return true } else { console.log(`Installed Java version (${installedVersion}) is less than required version (${requiredVersion}).`) return false } } else { console.log("Java is installed but version could not be determined.") return false } } catch (error) { console.log("Java is not installed.") return false } } async function downloadAndInstallJava(extractPath = null) { const platform = os.platform() const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "java-installer-")) const downloadUrl = JAVA_DOWNLOAD_URL[platform] if (!downloadUrl) { console.error("Unsupported platform for Java installation.") return } const zipPath = path.join(tempDir, "java-package.zip") await download(downloadUrl, tempDir, { filename: "java-package.zip" }) const extractionDir = extractPath || tempDir const zip = new AdmZip(zipPath) zip.extractAllTo(extractionDir, true) fs.unlinkSync(zipPath) } async function main(customExtractPath) { const requiredVersion = "17" const isJavaInstalled = checkJavaVersion(requiredVersion) if (!isJavaInstalled) { if (!fs.existsSync(path.join(customExtractPath, "jdk-17.0.12", "bin", os.platform() == "win32" ? "java.exe" : "java"))) { await downloadAndInstallJava(customExtractPath || path.join(__dirname, "..", "..", "java")) } } else { console.log("No further action is required.") } } async function getPath(customExtractPath) { const requiredVersion = "17" const isJavaInstalled = checkJavaVersion(requiredVersion) if (!isJavaInstalled) { if (!fs.existsSync(path.join(customExtractPath, "jdk-17.0.12", "bin", os.platform() == "win32" ? "java.exe" : "java"))) { await main(customExtractPath) } else { return path.join(customExtractPath, "jdk-17.0.12", "bin", os.platform() == "win32" ? "java.exe" : "java") } } else { console.log("No further action is required.") } } module.exports = { main }