From b2d719b1e317cce5a4846d50ed5683d9f6731fd5 Mon Sep 17 00:00:00 2001 From: Sunil Date: Sun, 20 Jun 2021 13:54:33 +0530 Subject: [PATCH] Multi Phrase Module Name Support for Module generation --- package-lock.json | 5 ++ package.json | 1 + resource/modules/sample/sample.controller.js | 16 ++--- resource/modules/sample/sample.model.js | 8 +-- resource/modules/sample/sample.route.js | 14 +++-- resource/modules/sample/sample.service.js | 4 +- resource/modules/sample/sample.settings.js | 6 ++ src/module-generator.js | 61 ++++++++++++-------- 8 files changed, 70 insertions(+), 45 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9b60ded..f46dd17 100644 --- a/package-lock.json +++ b/package-lock.json @@ -541,6 +541,11 @@ } } }, + "case": { + "version": "1.6.3", + "resolved": "https://registry.npmjs.org/case/-/case-1.6.3.tgz", + "integrity": "sha512-mzDSXIPaFwVDvZAHqZ9VlbyF4yyXRuX6IvB06WvPYkqJVO24kX1PPhv9bfpKNFZyxYFmmgo03HUiD8iklmJYRQ==" + }, "chai": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/chai/-/chai-4.3.4.tgz", diff --git a/package.json b/package.json index 77b70a9..ea3e8ac 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ }, "dependencies": { "axios": "^0.21.1", + "case": "^1.6.3", "chalk": "^4.1.1", "change-case": "^4.1.2", "inquirer": "^8.0.0", diff --git a/resource/modules/sample/sample.controller.js b/resource/modules/sample/sample.controller.js index 278bd1e..258036a 100644 --- a/resource/modules/sample/sample.controller.js +++ b/resource/modules/sample/sample.controller.js @@ -1,21 +1,21 @@ 'use strict'; const { CalmController } = require( '../../../system/core/CalmController' ); -const { SampleService } = require( './sample.service' ); -const { Sample } = require( './sample.model' ); -const sampleDTO = require( './sample.dto' ); +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' ); const autoBind = require( 'auto-bind' ), - sampleService = new SampleService( - new Sample().getInstance() + MODULE_SINGULAR_CAMELService = new MODULE_SINGULAR_PASCALService( + new MODULE_SINGULAR_PASCAL().getInstance() ); -class SampleController extends CalmController { +class MODULE_SINGULAR_PASCALController extends CalmController { constructor( service ) { super( service ); - this.dto = { ...this.dto, ...sampleDTO }; + this.dto = { ...this.dto, ...MODULE_SINGULAR_CAMELDTO }; autoBind( this ); } } -module.exports = new SampleController( sampleService ); +module.exports = new MODULE_SINGULAR_PASCALController( MODULE_SINGULAR_CAMELService ); diff --git a/resource/modules/sample/sample.model.js b/resource/modules/sample/sample.model.js index 40721bf..d046fa4 100644 --- a/resource/modules/sample/sample.model.js +++ b/resource/modules/sample/sample.model.js @@ -4,7 +4,7 @@ const { Schema } = require( 'mongoose' ); const uniqueValidator = require( 'mongoose-unique-validator' ); const { slugify } = require( '../../utils' ); -class Sample { +class MODULE_SINGULAR_PASCAL { initSchema() { const schema = new Schema( { @@ -31,7 +31,7 @@ class Sample { schema.plugin( uniqueValidator ); try { - mongoose.model( 'sample', schema ); + mongoose.model( 'MODULE_SINGULAR_CAMEL', schema ); } catch ( e ) { } @@ -40,8 +40,8 @@ class Sample { getInstance() { this.initSchema(); - return mongoose.model( 'sample' ); + return mongoose.model( 'MODULE_SINGULAR_CAMEL' ); } } -module.exports = { Sample }; +module.exports = { MODULE_SINGULAR_PASCAL }; diff --git a/resource/modules/sample/sample.route.js b/resource/modules/sample/sample.route.js index 0a420f7..03da892 100644 --- a/resource/modules/sample/sample.route.js +++ b/resource/modules/sample/sample.route.js @@ -1,14 +1,16 @@ 'use strict'; -const SampleController = require( './sample.controller' ); +const MODULE_SINGULAR_PASCALController = require( './MODULE_SINGULAR_KEBAB.controller' ); const AuthController = require( '../auth/auth.controller' ); const express = require( 'express' ); const router = express.Router(); -router.get( '/', AuthController.checkLogin, SampleController.getAll ); -router.get( '/:id', AuthController.checkLogin, SampleController.get ); -router.post( '/', AuthController.checkLogin, SampleController.insert ); -router.put( '/:id', AuthController.checkLogin, SampleController.update ); -router.delete( '/:id', AuthController.checkLogin, SampleController.delete ); +router.use(AuthController.checkLogin); + +router.get( '/', MODULE_SINGULAR_PASCALController.getAll ); +router.get( '/:id', MODULE_SINGULAR_PASCALController.get ); +router.post( '/', MODULE_SINGULAR_PASCALController.insert ); +router.put( '/:id', MODULE_SINGULAR_PASCALController.update ); +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 a15d9f6..4c4c9d3 100644 --- a/resource/modules/sample/sample.service.js +++ b/resource/modules/sample/sample.service.js @@ -1,10 +1,10 @@ 'use strict'; const { CalmService } = require( '../../../system/core/CalmService' ); -class SampleService extends CalmService { +class MODULE_SINGULAR_PASCALService extends CalmService { constructor( model ) { super( model ); } } -module.exports = { SampleService }; +module.exports = { MODULE_SINGULAR_PASCALService }; diff --git a/resource/modules/sample/sample.settings.js b/resource/modules/sample/sample.settings.js index ad9a93a..d3abf2c 100644 --- a/resource/modules/sample/sample.settings.js +++ b/resource/modules/sample/sample.settings.js @@ -1 +1,7 @@ 'use strict'; +/** + * If custom router url is required, export url as "moduleRoute" + */ +module.exports = { + +}; diff --git a/src/module-generator.js b/src/module-generator.js index b77bedf..86407cc 100644 --- a/src/module-generator.js +++ b/src/module-generator.js @@ -4,26 +4,31 @@ const fs = require('fs'); const pluralize = require('pluralize'); const { capitalCase } = require('change-case'); const chalk = require('chalk'); - +const caseChanger = require('case'); module.exports = async function(modulePath) { try { const modulePathArr = modulePath.split('/'); const finalModulePath = `${CURR_DIR}/src/modules`; const finalModuleName = pluralize.singular(modulePathArr.pop()); - // modulePathArr.splice(-1,1).forEach(filePath=>{ - // fs.mkdirSync(filePath.toLowerCase()) - // finalModulePath += filePath.toLowerCase(); - // }) - const moduleDirPath = `${finalModulePath}/${finalModuleName}`; + + const kebabCase = caseChanger.kebab(finalModuleName); + const moduleDirPath = `${finalModulePath}/${kebabCase}`; const templatePath = `${__dirname}/../resource/modules/sample`; - console.log(chalk.blueBright(`Creating directory ${finalModuleName} ...`)); + + console.log(chalk.blueBright(`Creating Module: ${finalModuleName}`)); + console.log(chalk.blueBright(`Creating Directory: ${kebabCase}`)); fs.mkdirSync(`${moduleDirPath}`); - console.log(chalk.greenBright('Module setup complete.')); + console.log(chalk.blueBright('Generating Files')); // eslint-disable-next-line no-use-before-define await createDirectoryContents(templatePath, finalModuleName, moduleDirPath); + console.log(chalk.blueBright('Module Generation Complete')); } catch (error) { - console.log(error); + if(error.code === 'EEXIST') { + console.error(chalk.redBright('Module already exists.')); + } else { + console.error(chalk.redBright(error.message)); + } } }; @@ -39,46 +44,52 @@ async function createDirectoryContents(templatePath, moduleName, moduleWritePath if (stats.isFile()) { let contents = fs.readFileSync(origFilePath, 'utf8'); - const smallCaseModuleName = moduleName.toLowerCase(); - const capitalCaseModuleName = capitalCase(moduleName); + const PascalCase = caseChanger.pascal(moduleName); + const camelCase = caseChanger.camel(moduleName); + const kebabCase = caseChanger.kebab(moduleName); switch (file) { case 'sample.controller.js': // eslint-disable-next-line no-param-reassign - file = `${moduleName}.controller.js`; - contents = contents.replace(/sample/g, smallCaseModuleName); - contents = contents.replace(/Sample/g, capitalCaseModuleName); + file = `${kebabCase}.controller.js`; + contents = contents.replace(/MODULE_SINGULAR_PASCAL/g, PascalCase); + contents = contents.replace(/MODULE_SINGULAR_CAMEL/g, camelCase); + contents = contents.replace(/MODULE_SINGULAR_KEBAB/g, kebabCase); break; case 'sample.dto.js': // eslint-disable-next-line no-param-reassign - file = `${moduleName}.dto.js`; + file = `${kebabCase}.dto.js`; break; case 'sample.model.js': - contents = contents.replace(/sample/g, smallCaseModuleName); - contents = contents.replace(/Sample/g, capitalCaseModuleName); + contents = contents.replace(/MODULE_SINGULAR_PASCAL/g, PascalCase); + contents = contents.replace(/MODULE_SINGULAR_CAMEL/g, camelCase); + contents = contents.replace(/MODULE_SINGULAR_KEBAB/g, kebabCase); // eslint-disable-next-line no-param-reassign - file = `${moduleName}.model.js`; + file = `${kebabCase}.model.js`; break; case 'sample.route.js': - contents = contents.replace(/sample/g, smallCaseModuleName); - contents = contents.replace(/Sample/g, capitalCaseModuleName); + contents = contents.replace(/MODULE_SINGULAR_PASCAL/g, PascalCase); + contents = contents.replace(/MODULE_SINGULAR_CAMEL/g, camelCase); + contents = contents.replace(/MODULE_SINGULAR_KEBAB/g, kebabCase); // eslint-disable-next-line no-param-reassign - file = `${moduleName}.route.js`; + file = `${kebabCase}.route.js`; break; case 'sample.service.js': - contents = contents.replace(/sample/g, smallCaseModuleName); - contents = contents.replace(/Sample/g, capitalCaseModuleName); + contents = contents.replace(/MODULE_SINGULAR_PASCAL/g, PascalCase); + contents = contents.replace(/MODULE_SINGULAR_CAMEL/g, camelCase); + contents = contents.replace(/MODULE_SINGULAR_KEBAB/g, kebabCase); // eslint-disable-next-line no-param-reassign - file = `${moduleName}.service.js`; + file = `${kebabCase}.service.js`; break; case 'sample.settings.js': // eslint-disable-next-line no-param-reassign - file = `${moduleName}.settings.js`; + file = `${kebabCase}.settings.js`; break; default: break; } const writePath = `${moduleWritePath}/${file}`; fs.writeFileSync(writePath, contents, 'utf8'); + console.log(chalk.greenBright(writePath)); } });