52 lines
1.6 KiB
JavaScript
52 lines
1.6 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 = "./.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);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @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);
|
|
});
|
|
} |