// Debug const { DebugBuilder } = require("../utilities/debugBuilder.js"); const log = new DebugBuilder("client", "updateConfig"); // Modules const replace = require('replace-in-file'); const { getFullConfig } = require("./configHandler.js"); class Options { constructor(key, updatedValue) { this.files = "./.env"; // A regex of the line containing the key in the config file this.from = new RegExp(`${key}="?(.+)"?`, "g"); // Check to see if the value is a string and needs to be wrapped in double quotes if (Array(["string", "number"]).includes(typeof updatedValue)) this.to = `${key}="${updatedValue}",`; else this.to = `${key}=${updatedValue}`; } } /** * Wrapper to update the client's saved ID * @param updatedId The updated ID assigned to the bot */ exports.updateId = (updatedId) => { this.updateConfig('CLIENT_ID', updatedId); } /** * Wrapper to update any or all keys in the client config * * @param {Object} configObject Object with what keys you wish to update (node object format, will be converted) * @param {number} configObject.id The ID given to the node to update * @param {string} configObject.name The name of the node * @param {string} configObject.ip The IP the server can contact the node on * @param {number} configObject.port The port the server can contact the node on * @param {string} configObject.location The physical location of the node * @returns */ exports.updateClientConfig = (configObject) => { const runningConfig = getFullConfig(); var updatedKeys = [] const configKeys = Object.keys(configObject); if (configKeys.includes("id")) { if (runningConfig.id != configObject.id) { this.updateConfig('CLIENT_ID', configObject.id); updatedKeys.push({'CLIENT_ID': configObject.id}); process.env.CLIENT_ID = configObject.id; log.DEBUG("Updated ID to: ", configObject.id); } } if (configKeys.includes("name")) { if (runningConfig.name != configObject.name) { this.updateConfig('CLIENT_NAME', configObject.name); updatedKeys.push({'CLIENT_NAME': configObject.name}); process.env.CLIENT_NAME = configObject.name; log.DEBUG("Updated name to: ", configObject.name); } } if (configKeys.includes("ip")) { if (runningConfig.ip != configObject.ip) { this.updateConfig('CLIENT_IP', configObject.ip); updatedKeys.push({'CLIENT_IP': configObject.ip}); process.env.CLIENT_IP = configObject.ip; log.DEBUG("Updated ip to: ", configObject.ip); } } if (configKeys.includes("port")) { if (runningConfig.port != configObject.port) { this.updateConfig('CLIENT_PORT', configObject.port); updatedKeys.push({'CLIENT_PORT': configObject.port}); process.env.CLIENT_PORT = configObject.port; log.DEBUG("Updated port to: ", configObject.port); } } if (configKeys.includes("location")) { if (runningConfig.location != configObject.location) { this.updateConfig('CLIENT_LOCATION', configObject.location); updatedKeys.push({'CLIENT_LOCATION': configObject.location}); process.env.CLIENT_LOCATION = configObject.location; log.DEBUG("Updated location to: ", configObject.location); } } return updatedKeys; } /** * * @param {string} key The config file key to update with the value * @param {string} value The value to update the key with */ exports.updateConfig = function updateConfig(key, value) { const options = new Options(key, value); updateConfigFile(options, (updatedFiles) => { // Do Something log.DEBUG("Updated config file: ", updatedFiles); }) } /** * Wrapper to write changes to the file * @param options An instance of the Objects class specified to the key being updated * @param callback Callback when the files have been modified */ function updateConfigFile(options, callback){ replace(options, (error, changedFiles) => { if (error) return console.error('Error occurred:', error); log.VERBOSE('Modified files:', changedFiles); callback(changedFiles); }); }