From 83e0bd7341f11463f8ea308eadc0fec34b3f31f3 Mon Sep 17 00:00:00 2001 From: rakesh Date: Wed, 24 Jan 2024 16:02:35 +0530 Subject: [PATCH] nested module creation --- index.js | 23 ++++++++++-- resource/modules/sample/sample.controller.js | 2 +- resource/modules/sample/sample.route.js | 3 +- resource/modules/sample/sample.service.js | 2 +- resource/project/system/routes/index.js | 13 +++++-- src/module-generator.js | 37 +++++++++++++++++--- 6 files changed, 69 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 91473c6..dd3ce9e 100755 --- a/index.js +++ b/index.js @@ -204,11 +204,30 @@ async function main() { }else if(argumentsArr.length === 3 && argumentsArr[ 0 ] === 'generate' && argumentsArr[ 1 ] === 'module') { const isRootFile = fs.readdirSync(CURR_DIR).find(file => file === 'calmapi.json'); if(!isRootFile) { - throw new Error('Please Run inside a calmapi Project.'); + const modulePath = CURR_DIR.split('/').find(file => file === 'modules'); + if(modulePath) { + await moduleGenerator(argumentsArr[ 2 ]); + }else{ + throw new Error('Please Run inside a module folder.'); + + } + }else { + await moduleGenerator(argumentsArr[ 2 ]); + } + }else if(argumentsArr.length === 4 && argumentsArr[ 0 ] === 'generate' && argumentsArr[ 1 ] === 'module' && argumentsArr[ 3 ] === '--force') { + const isRootFile = fs.readdirSync(CURR_DIR).find(file => file === 'calmapi.json'); + if(!isRootFile) { + const modulePath = CURR_DIR.split('/').find(file => file === 'modules'); + if(modulePath) { + await moduleGenerator(argumentsArr[ 2 ]); + }else{ + throw new Error('Please Run inside a module folder.'); + + } }else { await moduleGenerator(argumentsArr[ 2 ]); } - }else { + } else { throw new Error('Invalid Command'); } diff --git a/resource/modules/sample/sample.controller.js b/resource/modules/sample/sample.controller.js index 258036a..2041f4f 100644 --- a/resource/modules/sample/sample.controller.js +++ b/resource/modules/sample/sample.controller.js @@ -1,5 +1,5 @@ 'use strict'; -const { CalmController } = require( '../../../system/core/CalmController' ); +const { CalmController } = require( 'PATH_LINKsystem/core/CalmController' ); const { MODULE_SINGULAR_PASCALService } = require( './MODULE_SINGULAR_KEBAB.service' ); const { MODULE_SINGULAR_PASCAL } = require( './MODULE_SINGULAR_KEBAB.model' ); const MODULE_SINGULAR_CAMELDTO = require( './MODULE_SINGULAR_KEBAB.dto' ); diff --git a/resource/modules/sample/sample.route.js b/resource/modules/sample/sample.route.js index 03da892..9622f14 100644 --- a/resource/modules/sample/sample.route.js +++ b/resource/modules/sample/sample.route.js @@ -1,6 +1,6 @@ 'use strict'; const MODULE_SINGULAR_PASCALController = require( './MODULE_SINGULAR_KEBAB.controller' ); -const AuthController = require( '../auth/auth.controller' ); +const AuthController = require( 'PATH_LINKauth/auth.controller' ); const express = require( 'express' ); const router = express.Router(); @@ -14,3 +14,4 @@ router.delete( '/:id', MODULE_SINGULAR_PASCALController.delete ); module.exports = router; + diff --git a/resource/modules/sample/sample.service.js b/resource/modules/sample/sample.service.js index 8fad2f9..e944645 100644 --- a/resource/modules/sample/sample.service.js +++ b/resource/modules/sample/sample.service.js @@ -1,5 +1,5 @@ 'use strict'; -const { CalmService } = require( '../../../system/core/CalmService' ); +const { CalmService } = require( 'PATH_LINKsystem/core/CalmService' ); class MODULE_SINGULAR_PASCALService extends CalmService { // Setting Global Populate to apply in Get All & Get Single diff --git a/resource/project/system/routes/index.js b/resource/project/system/routes/index.js index ee0592a..646f01e 100644 --- a/resource/project/system/routes/index.js +++ b/resource/project/system/routes/index.js @@ -35,7 +35,7 @@ const loadModules = ( basePath, baseRoute, routerPaths ) => { try { // eslint-disable-next-line global-require router.use( `/${baseRoute ? `${baseRoute}/` : ''}${urlPath || pluralize.plural( module )}`, require( path.resolve( modulesPath, basePath ? `${basePath }/` : '', module, moduleRoute ) ) ); - // console.log( `/${baseRoute ? `${baseRoute}/` : ''}${urlPath || pluralize.plural( module )}` ); + moduleMapper.push( { 'Module': module, 'Route': `/${baseRoute ? `${baseRoute}/` : ''}${urlPath || pluralize.plural( module )}`, @@ -43,6 +43,15 @@ const loadModules = ( basePath, baseRoute, routerPaths ) => { 'API Exposed': '✔' } ); + const subPaths = fs.readdirSync( `${modulesPath}/${basePath ? `${basePath}/` : ''}${module}` ).filter( p => { + return fs.lstatSync( path.resolve( modulesPath, basePath ? `${basePath }/` : '', module, p ) ).isDirectory(); + } ); + if( subPaths.length ) { + const newBasePath = `${basePath ? `${basePath}/` : ''}${module}`; + const newBaseRoute = `${baseRoute ? `${baseRoute}/` : ''}${urlPath || pluralize.plural( module )}`; + loadModules( newBasePath, newBaseRoute, subPaths ); + } + } catch ( e ) { if(e.message.includes('Router.use() requires')) { moduleMapper.push( { @@ -53,7 +62,6 @@ const loadModules = ( basePath, baseRoute, routerPaths ) => { } ); return; } - // console.error( e ); const subPaths = fs.readdirSync( `${modulesPath}/${basePath ? `${basePath}/` : ''}${module}` ).filter( p => { return fs.lstatSync( path.resolve( modulesPath, basePath ? `${basePath }/` : '', module, p ) ).isDirectory(); } ); @@ -77,6 +85,7 @@ const loadModules = ( basePath, baseRoute, routerPaths ) => { } ); }; + router.use((req, res, next) => { res.sendCalmResponse = CalmResponse; res.sendCalmResponse.bind(res); diff --git a/src/module-generator.js b/src/module-generator.js index 34ebe0c..3942096 100644 --- a/src/module-generator.js +++ b/src/module-generator.js @@ -5,14 +5,25 @@ const pluralize = require('pluralize'); const chalk = require('chalk'); const caseChanger = require('case'); -module.exports = async function(modulePath) { +module.exports = async function(modulePath, isForce = false) { try { const modulePathArr = modulePath.split('/'); - const finalModulePath = `${CURR_DIR}/src/modules`; - const finalModuleName = pluralize.singular(modulePathArr.pop()); + let finalModulePath = `${CURR_DIR}`; + + let finalModuleName = modulePathArr; + + if(!isForce) { + finalModuleName = pluralize.singular(modulePathArr.pop()); + } const kebabCase = caseChanger.kebab(finalModuleName); - const moduleDirPath = `${finalModulePath}/${kebabCase}`; + let moduleDirPath; + if(!finalModulePath.split('/').find(file => file === 'modules')) { + finalModulePath = `${CURR_DIR}/src/modules`; + moduleDirPath = `${finalModulePath}/${kebabCase}`; + }else{ + moduleDirPath = `${finalModulePath}/${finalModuleName}`; + } const templatePath = `${__dirname}/../resource/modules/sample`; console.log(chalk.blueBright(`Creating Module: ${finalModuleName}`)); @@ -40,6 +51,21 @@ async function createDirectoryContents(templatePath, moduleName, moduleWritePath // get stats about the current file const stats = fs.statSync(origFilePath); + const destinationPath = moduleWritePath.split('/'); + const pathCount = destinationPath.slice(destinationPath.indexOf('src')); + const pathLink = '../'; + let finalPathLink = ''; + let routeFilePathLink = ''; + let counter = pathCount.length; + let routeFilePathCounter = pathCount.length - 2; + while(counter > 0) { + finalPathLink += pathLink; + counter--; + } + while(routeFilePathCounter > 0) { + routeFilePathLink += pathLink; + routeFilePathCounter--; + } if (stats.isFile()) { let contents = fs.readFileSync(origFilePath, 'utf8'); @@ -50,6 +76,7 @@ async function createDirectoryContents(templatePath, moduleName, moduleWritePath case 'sample.controller.js': // eslint-disable-next-line no-param-reassign file = `${kebabCase}.controller.js`; + contents = contents.replace(/PATH_LINK/g, finalPathLink); contents = contents.replace(/MODULE_SINGULAR_PASCAL/g, PascalCase); contents = contents.replace(/MODULE_SINGULAR_CAMEL/g, camelCase); contents = contents.replace(/MODULE_SINGULAR_KEBAB/g, kebabCase); @@ -66,6 +93,7 @@ async function createDirectoryContents(templatePath, moduleName, moduleWritePath file = `${kebabCase}.model.js`; break; case 'sample.route.js': + contents = contents.replace(/PATH_LINK/g, routeFilePathLink); contents = contents.replace(/MODULE_SINGULAR_PASCAL/g, PascalCase); contents = contents.replace(/MODULE_SINGULAR_CAMEL/g, camelCase); contents = contents.replace(/MODULE_SINGULAR_KEBAB/g, kebabCase); @@ -73,6 +101,7 @@ async function createDirectoryContents(templatePath, moduleName, moduleWritePath file = `${kebabCase}.route.js`; break; case 'sample.service.js': + contents = contents.replace(/PATH_LINK/g, finalPathLink); contents = contents.replace(/MODULE_SINGULAR_PASCAL/g, PascalCase); contents = contents.replace(/MODULE_SINGULAR_CAMEL/g, camelCase); contents = contents.replace(/MODULE_SINGULAR_KEBAB/g, kebabCase);