Files
DRB-CnC/Client/utilities/updateConfig.js
Logan Cusano 4e1b82c557 Init WIP Bot
2022-12-11 06:09:25 -05:00

41 lines
1.3 KiB
JavaScript

// Debug
const debug = require('debug')('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) => {
const options = new Options("id", updatedId);
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);
debug('Modified files:', changedFiles);
callback(changedFiles);
});
}