From 62ee550af75bfd1447a889e346644e52bd133d54 Mon Sep 17 00:00:00 2001 From: Azure Date: Sun, 6 Sep 2026 04:58:10 +0200 Subject: [PATCH] Minor fixes --- index.js | 3 +- package.json | 2 +- src/installer.js | 169 +++++++++++++++++++++++----------------------- src/systeminfo.js | 8 +-- src/web.js | 2 +- 5 files changed, 94 insertions(+), 90 deletions(-) diff --git a/index.js b/index.js index d145c65..703bec2 100644 --- a/index.js +++ b/index.js @@ -18,7 +18,8 @@ async function installJava(mcVersion, customExec = null) { const installationPath = await sysinfo.determineInstallationPath(component, majorVersion, customExec) const requiredComponentDetails = await web.getJREComponentDetails(component, sysinfo.getPlatformKey()) const requiredComponentManifest = await web.getJREComponentManifest(requiredComponentDetails[0].manifest.url) - return await installer.downloadJavaComponent(requiredComponentManifest, installationPath) + + return installer.downloadJavaComponent(requiredComponentManifest, installationPath) } module.exports = { diff --git a/package.json b/package.json index 7ebe6f9..e783c17 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@azures04/minecraft-java-manager", - "version": "0.0.2", + "version": "0.0.3", "description": "Node module to check if java is installed and install it", "keywords": [ "java", diff --git a/src/installer.js b/src/installer.js index ad25445..f306bd4 100644 --- a/src/installer.js +++ b/src/installer.js @@ -140,104 +140,107 @@ async function downloadSingleFile(fileEntry, outputRoot, onProgress) { } } -async function downloadJavaComponent(manifest, outputRoot, options) { +function downloadJavaComponent(manifest, outputRoot, options) { const emitter = new EventEmitter() const concurrency = (options && options.concurrency) || 5 - try { - const files = extractDownloadableFiles(manifest) - const totalFiles = files.length - const totalBytesExpected = files.reduce((sum, f) => { return sum + (f.size || 0) }, 0) + // On lance le traitement de manière asynchrone SANS l'attendre ici + // setImmediate garantit que l'appelant a le temps d'attacher ses listeners + setImmediate(async () => { + try { + const files = extractDownloadableFiles(manifest) + const totalFiles = files.length + const totalBytesExpected = files.reduce((sum, f) => sum + (f.size || 0), 0) - emitter.emit("start", { - totalFiles: totalFiles, - totalBytes: totalBytesExpected, - outputRoot: outputRoot - }) - - let completedFiles = 0 - let failedFiles = 0 - let totalBytesDownloaded = 0 - const errors = [] - - const perFileProgress = {} - - function reportGlobalProgress() { - const currentTotal = Object.keys(perFileProgress).reduce((sum, key) => { - return sum + perFileProgress[key] - }, 0) - - emitter.emit("progress", { - completedFiles: completedFiles, - totalFiles: totalFiles, - bytesDownloaded: currentTotal, + emitter.emit("start", { + totalFiles, totalBytes: totalBytesExpected, - percent: totalBytesExpected > 0 ? - Math.round((currentTotal / totalBytesExpected) * 100) : - null + outputRoot }) - } - let cursor = 0 + let completedFiles = 0 + let failedFiles = 0 + let totalBytesDownloaded = 0 + const errors = [] + const perFileProgress = {} - async function worker() { - while (cursor < files.length) { - const currentIndex = cursor - cursor += 1 - const fileEntry = files[currentIndex] + function reportGlobalProgress() { + const currentTotal = Object.keys(perFileProgress).reduce((sum, key) => { + return sum + perFileProgress[key] + }, 0) - try { - const result = await downloadSingleFile(fileEntry, outputRoot, (progressInfo) => { - perFileProgress[progressInfo.filePath] = progressInfo.bytesDownloaded + emitter.emit("progress", { + completedFiles, + totalFiles, + bytesDownloaded: currentTotal, + totalBytes: totalBytesExpected, + percent: totalBytesExpected > 0 + ? Math.round((currentTotal / totalBytesExpected) * 100) + : null + }) + } + + let cursor = 0 + + async function worker() { + while (cursor < files.length) { + const currentIndex = cursor + cursor += 1 + const fileEntry = files[currentIndex] + + try { + const result = await downloadSingleFile(fileEntry, outputRoot, (progressInfo) => { + perFileProgress[progressInfo.filePath] = progressInfo.bytesDownloaded + reportGlobalProgress() + }) + + completedFiles += 1 + totalBytesDownloaded += result.totalBytes + perFileProgress[fileEntry.filePath] = result.totalBytes + + emitter.emit("fileComplete", result) reportGlobalProgress() - }) - completedFiles += 1 - totalBytesDownloaded += result.totalBytes - perFileProgress[fileEntry.filePath] = result.totalBytes + } catch (err) { + failedFiles += 1 + errors.push({ + filePath: fileEntry.filePath, + message: err.message + }) - emitter.emit("fileComplete", result) - reportGlobalProgress() - - } catch (err) { - failedFiles += 1 - errors.push({ - filePath: fileEntry.filePath, - message: err.message - }) - - emitter.emit("fileError", { - filePath: fileEntry.filePath, - message: err.message, - error: err - }) + emitter.emit("fileError", { + filePath: fileEntry.filePath, + message: err.message, + error: err + }) + } } } + + const workerCount = Math.min(concurrency, files.length) + const workers = [] + + for (let i = 0; i < workerCount; i += 1) { + workers.push(worker()) + } + + await Promise.all(workers) + + emitter.emit("complete", { + totalFiles, + completedFiles, + failedFiles, + totalBytes: totalBytesDownloaded, + errors + }) + + } catch (err) { + emitter.emit("error", { + message: err.message, + error: err + }) } - - const workerCount = Math.min(concurrency, files.length) - const workers = [] - - for (let i = 0; i < workerCount; i += 1) { - workers.push(worker()) - } - - await Promise.all(workers) - - emitter.emit("complete", { - totalFiles: totalFiles, - completedFiles: completedFiles, - failedFiles: failedFiles, - totalBytes: totalBytesDownloaded, - errors: errors - }) - - } catch (err) { - emitter.emit("error", { - message: err.message, - error: err - }) - } + }) return { events: emitter, path: outputRoot } } diff --git a/src/systeminfo.js b/src/systeminfo.js index 7f5d2c5..06c2d55 100644 --- a/src/systeminfo.js +++ b/src/systeminfo.js @@ -34,7 +34,7 @@ function getPlatformKey() { return candidate } - return targets["linux"] ?? null + return null } async function determineInstallationPath(component, majorVersion, customInstallPath = null) { @@ -51,9 +51,9 @@ async function determineInstallationPath(component, majorVersion, customInstallP case "win32": return path.join("C:", "Program files", "Java", component, majorVersion.toString()) case "linux": - return path.join("opt", "java", component, majorVersion) - case "macos": - return path.join("Library", "Java", "JavaVirtualMachines", `${component}-${majorVersion}`) + return path.join("/", "opt", "java", component, majorVersion) + case "darwin": + return path.join(process.env.HOME || "/", "Library", "Java", "JavaVirtualMachines", `${component}-${majorVersion}`) default: throw new Error("Unsupported OS") } diff --git a/src/web.js b/src/web.js index e8aaeaa..1de3539 100644 --- a/src/web.js +++ b/src/web.js @@ -20,7 +20,7 @@ async function getJREComponentDetails(componentRequested, os) { const mainifestRequest = await fetch(config.allRuntimes.get()) const manifest = await mainifestRequest.json() const component = manifest[os][componentRequested] - if (typeof component != "object") { + if (!typeof component == "object") { throw new Error("No JRE Component found") } return component