From 5f12d5d636a55570453e450233899d3dff673185 Mon Sep 17 00:00:00 2001 From: fadhlaouir Date: Fri, 15 Mar 2024 17:59:21 +0100 Subject: [PATCH] feat: Implement cli helpers --- cli/helpers.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 cli/helpers.js diff --git a/cli/helpers.js b/cli/helpers.js new file mode 100644 index 0000000..27a7810 --- /dev/null +++ b/cli/helpers.js @@ -0,0 +1,46 @@ +/* -------------------------------------------------------------------------- */ +/* DEPENDENCIES */ +/* -------------------------------------------------------------------------- */ +// packages +const path = require('path'); +const fs = require('fs').promises; + +/* -------------------------------------------------------------------------- */ +/* Helper functions */ +/* -------------------------------------------------------------------------- */ + +/** + * Capitalizes the first character of the entity name. + * @param {string} entity - The entity name + * @returns {string} - The capitalized entity name + */ +function capitalizeEntity(entity) { + return entity.charAt(0).toUpperCase() + entity.slice(1); +} + +/** + * Checks if the entity already exists in the project. + * If the entity already exists, it logs a message and exits the process. + * @param {string} entity - The entity name + * @returns {boolean} - Returns true if the entity exists, false otherwise + */ +async function isEntityExists(entity) { + // Capitalize the entity name + const ENTITY = capitalizeEntity(entity); + + // Construct the file path for the model file + const modelFilePath = path.join('src', 'models', `${ENTITY}.model.js`); + + try { + // Check if the model file exists + await fs.access(modelFilePath); + return true; // Model file exists + } catch (error) { + return false; // Model file does not exist + } +} + +module.exports = { + capitalizeEntity, + isEntityExists, +};