Skip to content

Commit

Permalink
fix: Add getValueFromObject to @cocreate/utils
Browse files Browse the repository at this point in the history
This change adds a new method to the @cocreate/utils library to retrieve the value of an object's nested property using dot notation. This has eliminated code repetition on the server.js file and improved its overall code cleanliness.
  • Loading branch information
frankpagan committed Jun 15, 2023
1 parent bcf4155 commit cefebf6
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const readline = require('readline');
const os = require('os');
const path = require('path');
const fs = require('fs');
const { dotNotationToObject } = require('@cocreate/utils');
const { dotNotationToObject, getValueFromObject } = require('@cocreate/utils');

module.exports = async function (items, env = true, global = true) {
async function promptForInput(question) {
Expand Down Expand Up @@ -58,20 +58,26 @@ module.exports = async function (items, env = true, global = true) {
await getConfig(choice);
}
} else if (variable) {
variables[`{{${key}}}`] = value || await promptForInput(prompt || `${key}: `);
let variableValue = localConfig[key] || globalConfig[key]
if (!variableValue) {
variables[`{{${variable}}}`] = value || await promptForInput(prompt || `${variable}: `);
} else {
variables[`{{${variable}}}`] = Object.keys(variableValue)[0]
}
} else {
// TODO: handle dotnotation
if (value || value === "") {
config[key] = value;
if (global)
update = true;
} else if (process.env[key]) {
// TODO: if JSON.String object.parse()
config[key] = process.env[key];
} else {
if (localConfig[key]) {
config[key] = localConfig[key];
} else if (globalConfig[key]) {
config[key] = globalConfig[key];
let localKey, globalKey
if (localKey = getValueFromObject(localConfig, key)) {
config[key] = localKey;
} else if (globalKey = getValueFromObject(globalConfig, key)) {
config[key] = globalKey;
} else if (prompt || prompt === '') {
config[key] = await promptForInput(prompt || `${key}: `);
if (global) update = true;
Expand Down Expand Up @@ -117,5 +123,6 @@ module.exports = async function (items, env = true, global = true) {
};
}

return dotNotationToObject(config);
config = dotNotationToObject(config);
return config
}

0 comments on commit cefebf6

Please sign in to comment.