From b856fb0f7fc057ee8c5faffbfee151723bd5713d Mon Sep 17 00:00:00 2001 From: Artemy Date: Mon, 19 Jun 2023 17:25:17 +0300 Subject: [PATCH] Update handler.js --- components/handler.js | 101 +++++++++++++++++++++++++++--------------- 1 file changed, 65 insertions(+), 36 deletions(-) diff --git a/components/handler.js b/components/handler.js index 5da63a9..1b0741a 100644 --- a/components/handler.js +++ b/components/handler.js @@ -697,49 +697,78 @@ class Handler { }); } - async downloadToDirectory (directory, libraries, eventName) { - const libs = [] + async downloadToDirectory(directory, libraries, eventName) { + const libs = []; - await Promise.all(libraries.map(async library => { - if (!library) return - if (this.parseRule(library)) return - const lib = library.name.split(':') + await Promise.all( + libraries.map(async (library) => { + if (!library) return; + if (this.parseRule(library)) return; + const lib = library.name.split(":"); - let jarPath - let name - if (library.downloads && library.downloads.artifact && library.downloads.artifact.path) { - name = library.downloads.artifact.path.split('/')[library.downloads.artifact.path.split('/').length - 1] - jarPath = path.join(directory, this.popString(library.downloads.artifact.path)) - } else { - name = `${lib[1]}-${lib[2]}${lib[3] ? '-' + lib[3] : ''}.jar` - jarPath = path.join(directory, `${lib[0].replace(/\./g, '/')}/${lib[1]}/${lib[2]}`) - } - - const downloadLibrary = async library => { - if (library.url) { - const url = `${library.url}${lib[0].replace(/\./g, '/')}/${lib[1]}/${lib[2]}/${name}` - await this.downloadAsync(url, jarPath, name, true, eventName) - } else if (library.downloads && library.downloads.artifact) { - await this.downloadAsync(library.downloads.artifact.url, jarPath, name, true, eventName) + let jarPath; + let name; + if ( + library.downloads && + library.downloads.artifact && + library.downloads.artifact.path + ) { + name = + library.downloads.artifact.path.split("/")[ + library.downloads.artifact.path.split("/").length - 1 + ]; + jarPath = path.join( + directory, + this.popString(library.downloads.artifact.path) + ); + } else { + name = `${lib[1]}-${lib[2]}${lib[3] ? "-" + lib[3] : ""}.jar`; + jarPath = path.join( + directory, + `${lib[0].replace(/\./g, "/")}/${lib[1]}/${lib[2]}` + ); } - } - if (!fs.existsSync(path.join(jarPath, name))) downloadLibrary(library) - else if (library.downloads && library.downloads.artifact) { - if (!this.checkSum(library.downloads.artifact.sha1, path.join(jarPath, name))) downloadLibrary(library) - } + const downloadLibrary = async (library) => { + if (library.url) { + const url = `${library.url}${lib[0].replace(/\./g, "/")}/${ + lib[1] + }/${lib[2]}/${name}`; + await this.downloadAsync(url, jarPath, name, true, eventName); + } else if (library.downloads && library.downloads.artifact) { + await this.downloadAsync( + library.downloads.artifact.url, + jarPath, + name, + true, + eventName + ); + } + }; - counter++ - this.client.emit('progress', { - type: eventName, - task: counter, - total: libraries.length + if (!fs.existsSync(path.join(jarPath, name))) downloadLibrary(library); + else if (library.downloads && library.downloads.artifact) { + if ( + !this.checkSum( + library.downloads.artifact.sha1, + path.join(jarPath, name) + ) + ) + downloadLibrary(library); + } + + counter++; + this.client.emit("progress", { + type: eventName, + task: counter, + total: libraries.length, + }); + libs.push(`${jarPath}${path.sep}${name}`); }) - libs.push(`${jarPath}${path.sep}${name}`) - })) - counter = 0 + ); + counter = 0; - return libs + return libs; } async getClasses(classJson) {