// 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 const updateId = async (updatedId) => { await updateConfig('CLIENT_NUID', updatedId); process.env.CLIENT_NUID = updatedId; console.log("Updated NUID 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.nuid 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("nuid")) { if (runningConfig.nuid != newConfigObject.nuid) { this.updateId(newConfigObject.nuid); updatedKeys.push({ 'CLIENT_NUID': newConfigObject.nuid }); } } 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); console.log("Options:", options); 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); }); }