From bd583555d29f44fd2077e3a58eea74df73c81915 Mon Sep 17 00:00:00 2001 From: piff-saad Date: Sun, 12 Feb 2023 07:46:19 -0800 Subject: [PATCH 1/3] deleteModule API updated --- tools/cli/commands/deleteModule.js | 75 ++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 9 deletions(-) diff --git a/tools/cli/commands/deleteModule.js b/tools/cli/commands/deleteModule.js index 463e61c49..6941e6a9d 100644 --- a/tools/cli/commands/deleteModule.js +++ b/tools/cli/commands/deleteModule.js @@ -20,19 +20,76 @@ const { * @param moduleName - The name of a new module * @param old - The flag that describes if the command invoked for a new structure or not */ -function deleteModule({ logger, packageName, moduleName, old }) { - const modulePackageName = getModulePackageName(packageName, old); - const modulePath = computeModulePath(modulePackageName, old, moduleName); - const params = { logger, moduleName, modulePath, packageName, modulePackageName, old }; +function deleteModule(logger, templatePath, module, location) { + logger.info(`Deleting ${location} files…`); + + // pascalize + const Module = pascalize(module); + const startPath = `${__dirname}/../../..`; + const modulePath = `${startPath}/packages/${location}/src/modules/${module}`; + const commonGraphqlFile = 'commonGraphql.js'; + const commonGraphqlPath = `${startPath}/packages/${location}/src/modules/common/components/web/${commonGraphqlFile}`; if (fs.existsSync(modulePath)) { - deleteTemplates(params); - removeFromModules(params); - if (!old) removeDependency(params); + // remove module directory + shell.rm('-rf', modulePath); + + // change to destination directory + shell.cd(`${startPath}/packages/${location}/src/modules/`); + + // get module input data + const path = `${startPath}/packages/${location}/src/modules/index.js`; + let data = fs.readFileSync(path); + + // extract Feature modules + const re = /Feature\(([^()]+)\)/g; + const match = re.exec(data); + const modules = match[1].split(',').filter(featureModule => featureModule.trim() !== module); + + // remove import module line + const lines = data + .toString() + .split('\n') + .filter(line => line.match(`import ${module} from './${module}';`) === null); + fs.writeFileSync(path, lines.join('\n')); + + // remove module from Feature function + //shell.sed('-i', re, `Feature(${modules.toString().trim()})`, 'index.js'); + shell + .ShellString(shell.cat('index.js').replace(RegExp(re, 'g'), `Feature(${modules.toString().trim()})`)) + .to('index.js'); + + if (location === 'server') { + // change to database migrations directory + shell.cd(`${startPath}/packages/${location}/src/database/migrations`); + // check if any migrations files for this module exist + if (shell.find('.').filter(file => file.search(`_${Module}.js`) > -1).length > 0) { + let okMigrations = shell.rm(`*_${Module}.js`); + if (okMigrations) { + logger.info(chalk.green(`✔ Database migrations files successfully deleted!`)); + } + } + + // change to database seeds directory + shell.cd(`${startPath}/packages/${location}/src/database/seeds`); + // check if any seed files for this module exist + if (shell.find('.').filter(file => file.search(`_${Module}.js`) > -1).length > 0) { + let okSeeds = shell.rm(`*_${Module}.js`); + if (okSeeds) { + logger.info(chalk.green(`✔ Database seed files successfully deleted!`)); + } + } + } - logger.info(chalk.green(`✔ Module ${moduleName} for package ${packageName} successfully deleted!`)); + // continue only if directory does not jet exist + logger.info(chalk.green(`✔ Module for ${location} successfully deleted!`)); } else { - logger.info(chalk.red(`✘ Module ${moduleName} for package ${packageName} not found!`)); + logger.info(chalk.red(`✘ Module ${location} location for ${modulePath} not found!`)); + } + + if (fs.existsSync(commonGraphqlPath)) { + const graphqlQuery = `${Module}Query`; + deleteModuleFromCommonGraphqlFile(module, commonGraphqlPath, graphqlQuery); } } From bd032910efbac58c61d689226b6f50385fe394cc Mon Sep 17 00:00:00 2001 From: piff-saad Date: Sun, 12 Feb 2023 08:00:36 -0800 Subject: [PATCH 2/3] generateCommonGraphqlFile API created. --- tools/cli/helpers/util.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tools/cli/helpers/util.js b/tools/cli/helpers/util.js index e4de3c865..653f6a3f7 100644 --- a/tools/cli/helpers/util.js +++ b/tools/cli/helpers/util.js @@ -122,6 +122,31 @@ function computeRootModulesPath(moduleName) { * @param old - The flag that describes if the command invoked for a new structure or not * @returns {string} - Return the computed path */ + + +function generateCommonGraphqlFile(module, commonGraphqlPath, moduleGraphqlContainer) { + const importGraphqlContainer = `import ${moduleGraphqlContainer} from '../../../${module}/containers/${moduleGraphqlContainer}';\n`; + const exportGraphqlContainer = `\nexport default {\n ${moduleGraphqlContainer}\n};\n`; + + if (fs.existsSync(commonGraphqlPath)) { + const commonGraphqlData = fs.readFileSync(commonGraphqlPath); + const commonGraphql = commonGraphqlData.toString().trim(); + if (commonGraphql.length > 1) { + const index = commonGraphql.lastIndexOf("';"); + const computedIndex = index >= 0 ? index + 3 : false; + if (computedIndex) { + let computedCommonGraphql = + commonGraphql.slice(0, computedIndex) + + importGraphqlContainer + + commonGraphql.slice(computedIndex, commonGraphql.length); + computedCommonGraphql = computedCommonGraphql.replace(/(,|)\s};/g, `,\n ${moduleGraphqlContainer}\n};`); + return fs.writeFileSync(commonGraphqlPath, computedCommonGraphql); + } + } + } + return fs.writeFileSync(commonGraphqlPath, importGraphqlContainer + exportGraphqlContainer); +} + function computeModulePackageName(moduleName, packageName, old) { return old ? `./${moduleName}` : `@gqlapp/${decamelize(moduleName, { separator: '-' })}-${packageName}`; } @@ -250,4 +275,5 @@ module.exports = { deleteDir, getPathsSubdir, deleteStackDir, + generateCommonGraphqlFile, }; From 60afaafa8dba500d4b3d409b9a93a5c02f5bc5c0 Mon Sep 17 00:00:00 2001 From: piff-saad Date: Sun, 12 Feb 2023 10:24:13 -0800 Subject: [PATCH 3/3] generateField API added to util.js --- tools/cli/helpers/util.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tools/cli/helpers/util.js b/tools/cli/helpers/util.js index 653f6a3f7..ab5d5850e 100644 --- a/tools/cli/helpers/util.js +++ b/tools/cli/helpers/util.js @@ -257,6 +257,34 @@ function deleteStackDir(stackDirList) { }); } +function generateField(value, update = false) { + let result = ''; + const hasTypeOf = targetType => value.type === targetType || value.type.prototype instanceof targetType; + if (hasTypeOf(Boolean)) { + result += 'Boolean'; + } else if (hasTypeOf(DomainSchema.ID)) { + result += 'ID'; + } else if (hasTypeOf(DomainSchema.Int)) { + result += 'Int'; + } else if (hasTypeOf(DomainSchema.Float)) { + result += 'Float'; + } else if (hasTypeOf(String)) { + result += 'String'; + } else if (hasTypeOf(Date)) { + result += 'Date'; + } else if (hasTypeOf(DomainSchema.DateTime)) { + result += 'DateTime'; + } else if (hasTypeOf(DomainSchema.Time)) { + result += 'Time'; + } + + if (!update && !value.optional) { + result += '!'; + } + + return result; +} + module.exports = { getModulePackageName, getTemplatesPath, @@ -276,4 +304,5 @@ module.exports = { getPathsSubdir, deleteStackDir, generateCommonGraphqlFile, + generateField, };