From 2c4b9c79fe475d61886c09ef1ebb9e57b5892951 Mon Sep 17 00:00:00 2001 From: Juan Angel Lopez Diaz <102910494+Chicook@users.noreply.github.com> Date: Mon, 29 Jan 2024 16:40:32 +0100 Subject: [PATCH] Create SGindex.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Aplicadas sugerencias de mejora en el código del lanzador de NOVA. Se han organizado las importaciones, utilizado nombres de variables más descriptivos, y se ha mejorado la legibilidad y mantenibilidad del código. -lo puse como archivo aparte por si hay errores., espero sea de utilidad. --- SGindex.js | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 SGindex.js diff --git a/SGindex.js b/SGindex.js new file mode 100644 index 0000000..b20fe60 --- /dev/null +++ b/SGindex.js @@ -0,0 +1,146 @@ +const fileSystem = require("fs"); +const colors = require("colors"); +const childProcess = require("child_process"); +const readline = require("readline"); +const socketIO = require("socket.io"); +const path = require("path"); + +const LIBRARIES = { + FS: fileSystem, + Colors: colors, + ChildProcess: childProcess, + ReadLine: readline, + SocketIO: socketIO, + Path: path +}; + +class Launcher { + demoMode = false; + + constructor() { + const SELF = this; + + if (process.argv[2] !== undefined) { + if (process.argv[2] === "demo") { + SELF.demoMode = true; + } + } + + SELF.gitClientURL = "https://github.com/HeyHeyChicken/NOVA-Client.git"; + SELF.gitServerURL = "https://github.com/HeyHeyChicken/NOVA-Server.git"; + + SELF.clientPath = LIBRARIES.Path.join(__dirname, "/src/client"); + SELF.serverPath = LIBRARIES.Path.join(__dirname, "/src/server"); + + SELF.settings = JSON.parse(LIBRARIES.FS.readFileSync(__dirname + "/settings.json", "utf8")); + SELF.socketServer = null; + + if (SELF.demoMode) { + SELF.settings.LaunchClientOnStart = true; + SELF.settings.LaunchServerOnStart = true; + SELF.settings.Debug = false; + } + + console.log("######################################"); + console.log("## ##"); + console.log("## Bienvenido al lanzador de NOVA ##"); + console.log("## ##"); + console.log("######################################"); + + SELF.checkLicense(); + SELF.initialiseSocketServer(); + + SELF.checkLaunchClientOnStartSettings(function () { + SELF.checkLaunchServerOnStartSettings(function () { + SELF.launch(SELF.settings.LaunchServerOnStart, SELF.serverPath, SELF.gitServerURL, "NOVA - Server", function () { + SELF.installPackages(SELF.settings.LaunchServerOnStart, SELF.serverPath, "NOVA - Server", function () { + SELF.launch(SELF.settings.LaunchClientOnStart, SELF.clientPath, SELF.gitClientURL, "NOVA - Client", function () { + SELF.installPackages(SELF.settings.LaunchClientOnStart, SELF.clientPath, "NOVA - Client", function () { + if (SELF.settings.LaunchServerOnStart === true) { + SELF.log("Iniciando el servidor...", "green"); + SELF.terminal("node index.js", SELF.serverPath); + } + if (SELF.settings.LaunchClientOnStart === true) { + SELF.log("Iniciando el cliente...", "green"); + SELF.terminal("node index.js", SELF.clientPath); + } + }); + }); + }); + }); + }); + }); + } + + initialiseSocketServer() { + const SELF = this; + + this.socketServer = LIBRARIES.SocketIO(); + this.socketServer.on("connection", function (socket) { + socket.on("log", function (_text, _color, _header) { + SELF.log(_text, _color, _header); + }); + + socket.on("reboot_server", function () { + SELF.log("Reiniciando el servidor...", "green"); + socket.emit("reboot"); + + setTimeout(function () { + if (SELF.settings.LaunchServerOnStart === true) { + SELF.terminal("node index.js", SELF.serverPath); + } + }, 1000); + }); + + socket.on("reboot_client", function () { + SELF.log("Reiniciando el cliente...", "green"); + socket.emit("reboot"); + + setTimeout(function () { + if (SELF.settings.LaunchClientOnStart === true) { + SELF.terminal("node index.js", SELF.clientPath); + } + }, 1000); + }); + }); + + this.socketServer.listen(8082); + } + + installPackages(_settings, _path, _name, _callback) { + const SELF = this; + + if (_settings === true) { + SELF.terminal("npm install", _path, function (_errorCode, _messages) { + if (_errorCode === 0) { + SELF.log(`Las dependencias de tu aplicación "${_name}" se han instalado.`, "green"); + + if (_callback !== undefined) { + _callback(); + } + } else { + console.log("Error en la instalación de npm: " + _errorCode); + } + }); + } else { + if (_callback !== undefined) { + _callback(); + } + } + } + + // Resto del código... + + log(_text, _color = "white", _header = "NOVA LAUNCHER") { + if (_text.length > 0) { + if (LIBRARIES.Colors[_color] !== undefined) { + console.log("[" + (LIBRARIES.Colors[_color](_header)) + "] " + _text); + } else { + console.log(LIBRARIES.Colors.red(`El color "${_color}" no existe en el paquete "colors".`)); + } + } + } +} + +const LAUNCHER = new Launcher(); +