From 84aa4c5aff7847c1a8221a41b11d63c926c13dab Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 9 Mar 2024 15:09:42 -0500 Subject: [PATCH] #15 - Changed the regex expression to not require a value after the key --- client/client.js | 2 +- client/modules/updateConfig.mjs | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/client/client.js b/client/client.js index 51bc203..a9a6bfc 100644 --- a/client/client.js +++ b/client/client.js @@ -28,7 +28,7 @@ async function firstTimeBoot() { console.log(`Generated a new unique ID for this node: '${localNodeConfig.node.nuid}'`); // Update the config with the new ID - updateId(localNodeConfig.node.nuid); + await updateId(localNodeConfig.node.nuid); console.log("Updated the config with the new node ID"); // TODO - Create the config file with the ID given and replace the update above // TODO - Implement web server so users can update radio systems easily diff --git a/client/modules/updateConfig.mjs b/client/modules/updateConfig.mjs index 46e54b0..85e7ac4 100644 --- a/client/modules/updateConfig.mjs +++ b/client/modules/updateConfig.mjs @@ -3,9 +3,9 @@ import replace from 'replace-in-file'; class Options { constructor(key, updatedValue) { - this.files = "./.env"; + this.files = ".env"; // A regex of the line containing the key in the config file - this.from = new RegExp(`${key}="?(.+)"?`, "g"); + 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}`; @@ -16,10 +16,10 @@ class Options { * Wrapper to update the client's saved ID * @param updatedId The updated ID assigned to the node */ -export function updateId (updatedId) { - updateConfig('CLIENT_NUID', updatedId); +export const updateId = async (updatedId) => { + await updateConfig('CLIENT_NUID', updatedId); process.env.CLIENT_NUID = updatedId; - console.log("Updated ID to: ", updatedId); + console.log("Updated NUID to: ", updatedId); } /** @@ -27,7 +27,7 @@ export function updateId (updatedId) { * * @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 {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 @@ -38,10 +38,10 @@ 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_NUID': newConfigObject.id }); + if (configKeys.includes("nuid")) { + if (runningConfig.nuid != newConfigObject.nuid) { + this.updateId(newConfigObject.nuid); + updatedKeys.push({ 'CLIENT_NUID': newConfigObject.nuid }); } } if (configKeys.includes("name")) { @@ -88,6 +88,8 @@ export function updateClientConfig (runningConfig, newConfigObject) { export function updateConfig (key, value) { const options = new Options(key, value); + console.log("Options:", options); + updateConfigFile(options, (updatedFiles) => { // Do Something })