Files
DRB-CnC/Client/utilities/updateConfig.js
2023-05-06 16:40:15 -04:00

52 lines
1.7 KiB
JavaScript

// Debug
const { DebugBuilder } = require("../utilities/debugBuilder.js");
const log = new DebugBuilder("client", "updateConfig");
// Modules
const replace = require('replace-in-file');
class Options {
constructor(key, updatedValue) {
this.files = "./config/clientConfig.js";
// 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 (typeof updatedValue === "string") 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('id', updatedId);
}
/**
*
* @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);
this.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
*/
exports.updateConfigFile = function updateConfigFile(options, callback){
replace(options, (error, changedFiles) => {
if (error) return console.error('Error occurred:', error);
log.DEBUG('Modified files:', changedFiles);
callback(changedFiles);
});
}