Minor fixes
This commit is contained in:
@@ -18,7 +18,8 @@ async function installJava(mcVersion, customExec = null) {
|
|||||||
const installationPath = await sysinfo.determineInstallationPath(component, majorVersion, customExec)
|
const installationPath = await sysinfo.determineInstallationPath(component, majorVersion, customExec)
|
||||||
const requiredComponentDetails = await web.getJREComponentDetails(component, sysinfo.getPlatformKey())
|
const requiredComponentDetails = await web.getJREComponentDetails(component, sysinfo.getPlatformKey())
|
||||||
const requiredComponentManifest = await web.getJREComponentManifest(requiredComponentDetails[0].manifest.url)
|
const requiredComponentManifest = await web.getJREComponentManifest(requiredComponentDetails[0].manifest.url)
|
||||||
return await installer.downloadJavaComponent(requiredComponentManifest, installationPath)
|
|
||||||
|
return installer.downloadJavaComponent(requiredComponentManifest, installationPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@azures04/minecraft-java-manager",
|
"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",
|
"description": "Node module to check if java is installed and install it",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"java",
|
"java",
|
||||||
|
|||||||
+86
-83
@@ -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 emitter = new EventEmitter()
|
||||||
const concurrency = (options && options.concurrency) || 5
|
const concurrency = (options && options.concurrency) || 5
|
||||||
|
|
||||||
try {
|
// On lance le traitement de manière asynchrone SANS l'attendre ici
|
||||||
const files = extractDownloadableFiles(manifest)
|
// setImmediate garantit que l'appelant a le temps d'attacher ses listeners
|
||||||
const totalFiles = files.length
|
setImmediate(async () => {
|
||||||
const totalBytesExpected = files.reduce((sum, f) => { return sum + (f.size || 0) }, 0)
|
try {
|
||||||
|
const files = extractDownloadableFiles(manifest)
|
||||||
|
const totalFiles = files.length
|
||||||
|
const totalBytesExpected = files.reduce((sum, f) => sum + (f.size || 0), 0)
|
||||||
|
|
||||||
emitter.emit("start", {
|
emitter.emit("start", {
|
||||||
totalFiles: 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,
|
|
||||||
totalBytes: totalBytesExpected,
|
totalBytes: totalBytesExpected,
|
||||||
percent: totalBytesExpected > 0 ?
|
outputRoot
|
||||||
Math.round((currentTotal / totalBytesExpected) * 100) :
|
|
||||||
null
|
|
||||||
})
|
})
|
||||||
}
|
|
||||||
|
|
||||||
let cursor = 0
|
let completedFiles = 0
|
||||||
|
let failedFiles = 0
|
||||||
|
let totalBytesDownloaded = 0
|
||||||
|
const errors = []
|
||||||
|
const perFileProgress = {}
|
||||||
|
|
||||||
async function worker() {
|
function reportGlobalProgress() {
|
||||||
while (cursor < files.length) {
|
const currentTotal = Object.keys(perFileProgress).reduce((sum, key) => {
|
||||||
const currentIndex = cursor
|
return sum + perFileProgress[key]
|
||||||
cursor += 1
|
}, 0)
|
||||||
const fileEntry = files[currentIndex]
|
|
||||||
|
|
||||||
try {
|
emitter.emit("progress", {
|
||||||
const result = await downloadSingleFile(fileEntry, outputRoot, (progressInfo) => {
|
completedFiles,
|
||||||
perFileProgress[progressInfo.filePath] = progressInfo.bytesDownloaded
|
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()
|
reportGlobalProgress()
|
||||||
})
|
|
||||||
|
|
||||||
completedFiles += 1
|
} catch (err) {
|
||||||
totalBytesDownloaded += result.totalBytes
|
failedFiles += 1
|
||||||
perFileProgress[fileEntry.filePath] = result.totalBytes
|
errors.push({
|
||||||
|
filePath: fileEntry.filePath,
|
||||||
|
message: err.message
|
||||||
|
})
|
||||||
|
|
||||||
emitter.emit("fileComplete", result)
|
emitter.emit("fileError", {
|
||||||
reportGlobalProgress()
|
filePath: fileEntry.filePath,
|
||||||
|
message: err.message,
|
||||||
} catch (err) {
|
error: err
|
||||||
failedFiles += 1
|
})
|
||||||
errors.push({
|
}
|
||||||
filePath: fileEntry.filePath,
|
|
||||||
message: err.message
|
|
||||||
})
|
|
||||||
|
|
||||||
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 }
|
return { events: emitter, path: outputRoot }
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-4
@@ -34,7 +34,7 @@ function getPlatformKey() {
|
|||||||
return candidate
|
return candidate
|
||||||
}
|
}
|
||||||
|
|
||||||
return targets["linux"] ?? null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
async function determineInstallationPath(component, majorVersion, customInstallPath = null) {
|
async function determineInstallationPath(component, majorVersion, customInstallPath = null) {
|
||||||
@@ -51,9 +51,9 @@ async function determineInstallationPath(component, majorVersion, customInstallP
|
|||||||
case "win32":
|
case "win32":
|
||||||
return path.join("C:", "Program files", "Java", component, majorVersion.toString())
|
return path.join("C:", "Program files", "Java", component, majorVersion.toString())
|
||||||
case "linux":
|
case "linux":
|
||||||
return path.join("opt", "java", component, majorVersion)
|
return path.join("/", "opt", "java", component, majorVersion)
|
||||||
case "macos":
|
case "darwin":
|
||||||
return path.join("Library", "Java", "JavaVirtualMachines", `${component}-${majorVersion}`)
|
return path.join(process.env.HOME || "/", "Library", "Java", "JavaVirtualMachines", `${component}-${majorVersion}`)
|
||||||
default:
|
default:
|
||||||
throw new Error("Unsupported OS")
|
throw new Error("Unsupported OS")
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -20,7 +20,7 @@ async function getJREComponentDetails(componentRequested, os) {
|
|||||||
const mainifestRequest = await fetch(config.allRuntimes.get())
|
const mainifestRequest = await fetch(config.allRuntimes.get())
|
||||||
const manifest = await mainifestRequest.json()
|
const manifest = await mainifestRequest.json()
|
||||||
const component = manifest[os][componentRequested]
|
const component = manifest[os][componentRequested]
|
||||||
if (typeof component != "object") {
|
if (!typeof component == "object") {
|
||||||
throw new Error("No JRE Component found")
|
throw new Error("No JRE Component found")
|
||||||
}
|
}
|
||||||
return component
|
return component
|
||||||
|
|||||||
Reference in New Issue
Block a user