Init Commit
Server
- Working init discord bot
- Modular events and commands
- Has access to the socket server
- Working init socket server
- Need to work on getting access to discord bot
- Working init web server
Currently working on breaking out the init of the socket server
Client
- Working init socket client
Currently working on the discord bot to join voice channels
This commit is contained in:
107
client/modules/updateConfig.mjs
Normal file
107
client/modules/updateConfig.mjs
Normal file
@@ -0,0 +1,107 @@
|
||||
// Modules
|
||||
import replace from 'replace-in-file';
|
||||
|
||||
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 node
|
||||
*/
|
||||
export function updateId (updatedId) {
|
||||
updateConfig('CLIENT_ID', updatedId);
|
||||
process.env.CLIENT_ID = updatedId;
|
||||
console.log("Updated ID to: ", updatedId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper to update any or all keys in the client config
|
||||
*
|
||||
* @param {Object} runningConfig Running config object
|
||||
* @param {Object} newConfigObject Object with what keys you wish to update (node object format, will be converted)
|
||||
* @param {number} newConfigObject.id The ID given to the node to update
|
||||
* @param {string} newConfigObject.name The name of the node
|
||||
* @param {string} newConfigObject.ip The IP the server can contact the node on
|
||||
* @param {number} newConfigObject.port The port the server can contact the node on
|
||||
* @param {string} newConfigObject.location The physical location of the node
|
||||
* @returns
|
||||
*/
|
||||
export function updateClientConfig (runningConfig, newConfigObject) {
|
||||
var updatedKeys = []
|
||||
const configKeys = Object.keys(newConfigObject);
|
||||
|
||||
if (configKeys.includes("id")) {
|
||||
if (runningConfig.id != newConfigObject.id) {
|
||||
this.updateId(newConfigObject.id);
|
||||
updatedKeys.push({ 'CLIENT_ID': newConfigObject.id });
|
||||
}
|
||||
}
|
||||
if (configKeys.includes("name")) {
|
||||
if (runningConfig.name != newConfigObject.name) {
|
||||
this.updateConfig('CLIENT_NAME', newConfigObject.name);
|
||||
updatedKeys.push({ 'CLIENT_NAME': newConfigObject.name });
|
||||
process.env.CLIENT_NAME = newConfigObject.name;
|
||||
console.log("Updated name to: ", newConfigObject.name);
|
||||
}
|
||||
}
|
||||
if (configKeys.includes("ip")) {
|
||||
if (runningConfig.ip != newConfigObject.ip) {
|
||||
this.updateConfig('CLIENT_IP', newConfigObject.ip);
|
||||
updatedKeys.push({ 'CLIENT_IP': newConfigObject.ip });
|
||||
process.env.CLIENT_IP = newConfigObject.ip;
|
||||
console.log("Updated ip to: ", newConfigObject.ip);
|
||||
}
|
||||
}
|
||||
if (configKeys.includes("port")) {
|
||||
if (runningConfig.port != newConfigObject.port) {
|
||||
this.updateConfig('CLIENT_PORT', newConfigObject.port);
|
||||
updatedKeys.push({ 'CLIENT_PORT': newConfigObject.port });
|
||||
process.env.CLIENT_PORT = newConfigObject.port;
|
||||
console.log("Updated port to: ", newConfigObject.port);
|
||||
}
|
||||
}
|
||||
if (configKeys.includes("location")) {
|
||||
if (runningConfig.location != newConfigObject.location) {
|
||||
this.updateConfig('CLIENT_LOCATION', newConfigObject.location);
|
||||
updatedKeys.push({ 'CLIENT_LOCATION': newConfigObject.location });
|
||||
process.env.CLIENT_LOCATION = newConfigObject.location;
|
||||
console.log("Updated location to: ", newConfigObject.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
|
||||
*/
|
||||
export function updateConfig (key, value) {
|
||||
const options = new Options(key, value);
|
||||
|
||||
updateConfigFile(options, (updatedFiles) => {
|
||||
// Do Something
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
console.log('Updated config file: ', changedFiles);
|
||||
callback(changedFiles);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user