From a5cb0847670583234624332a6310105cb3f75a98 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Mon, 4 Apr 2022 05:13:24 +0800 Subject: [PATCH] feat: remove global initialize Signed-off-by: MiaoWoo --- packages/core/src/exit.js | 15 +++++----- packages/core/src/initialize.js | 51 ++++++++++++++++++++++----------- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/packages/core/src/exit.js b/packages/core/src/exit.js index ac2efb0f..af68ca02 100644 --- a/packages/core/src/exit.js +++ b/packages/core/src/exit.js @@ -1,14 +1,13 @@ -function upgradeModules(core) { - if (base.version && global.ScriptEngineVersion != core['dist-tags']['latest']) { - var Paths = Java.type('java.nio.file.Paths') - base.save(Paths.get(root, "upgrade"), core['dist-tags']['latest']) - console.info('@ccms/core found new version ' + core['dist-tags']['latest'] + ' will upgrade after reboot!') - } -} function exit() { var http = require('@ccms/common/dist/http').default + function upgradeModules(core) { + if (base.version && global.ScriptEngineVersion != core['dist-tags']['latest']) { + var Paths = Java.type('java.nio.file.Paths') + base.save(Paths.get(root, "upgrade"), core['dist-tags']['latest']) + console.info('@ccms/core found new version ' + core['dist-tags']['latest'] + ' will upgrade after reboot!') + } + } upgradeModules(http.get('https://registry.npmmirror.com/@ccms/core')) - console.debug('exit finish!') } exit() diff --git a/packages/core/src/initialize.js b/packages/core/src/initialize.js index 8688e07e..ab825ee4 100644 --- a/packages/core/src/initialize.js +++ b/packages/core/src/initialize.js @@ -1,22 +1,34 @@ -var fs = require('@ccms/common/dist/fs') -function updateJar() { - if (!base.version) { - var pluginFolder = fs.file(fs.concat(root, '..')) - var updateFolder = fs.concat(pluginFolder, 'update') - } -} -function upgradeModules(core) { - if (base.version && global.ScriptEngineVersion != core['dist-tags']['latest']) { - var Paths = Java.type('java.nio.file.Paths') - base.save(Paths.get(root, "upgrade"), core['dist-tags']['latest']) - console.info('@ccms/core found new version ' + core['dist-tags']['latest'] + ' will upgrade after reboot!') - } -} -function initialize() { +global.initialize = function () { var mspmc = 'https://ms.yumc.pw/api/plugin/download/name/' + var artifact = 'https://ci.yumc.pw/job/Minecraft/job/MiaoScript/lastSuccessfulBuild/artifact' + var fs = require('@ccms/common/dist/fs') var http = require('@ccms/common/dist/http').default + function updateJar() { + var DocumentBuilderFactory = Java.type('javax.xml.parsers.DocumentBuilderFactory') + var URLDecoder = Java.type('java.net.URLDecoder') + var pom = DocumentBuilderFactory.newInstance().newDocumentBuilder() + .parse(artifact + "/pom.xml") + var latestVersion = pom.getElementsByTagName("version").item(0).getTextContent() + if (base.version != latestVersion) { + var pluginFolder = fs.file(fs.concat(root, '..')) + var updateFolder = fs.concat(pluginFolder, 'update') + fs.mkdirs(updateFolder) + var filePath = base.getInstance().class.classLoader.getURLs()[0] + var pluginFile = fs.file(URLDecoder.decode(filePath.getFile(), "UTF-8")) + var updateFile = fs.file(updateFolder, pluginFile.getName()) + http.download(artifact + "/target/MiaoScript.jar", updateFile.getAbsolutePath()) + console.info('MiaoScript found new version ' + latestVersion + ' will upgrade after reboot!') + } + } + function upgradeModules(core) { + if (base.version && global.ScriptEngineVersion != core['dist-tags']['latest']) { + var Paths = Java.type('java.nio.file.Paths') + base.save(Paths.get(root, "upgrade"), core['dist-tags']['latest']) + console.info('@ccms/core found new version ' + core['dist-tags']['latest'] + ' will upgrade after reboot!') + } + } var pluginFolder = fs.concat(root, 'plugins') var updateFolder = fs.concat(pluginFolder, 'update') var pluginFile = fs.concat(pluginFolder, 'MiaoScriptPackageManager.js') @@ -43,9 +55,14 @@ function initialize() { try { Java.type("org.bukkit.Bukkit") updateJar() - } catch (ignore) { + } catch (error) { + console.debug(error) + if (global.debug) { + console.ex(error) + } } upgradeModules(http.get('https://registry.npmmirror.com/@ccms/core')) console.debug('initialize finish!') } -initialize() +global.initialize() +delete global.initialize