Skip to content

Commit

Permalink
Multi Phrase Module Name Support for Module generation
Browse files Browse the repository at this point in the history
  • Loading branch information
sunilksamanta committed Jun 20, 2021
1 parent 961819c commit b2d719b
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 45 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 8 additions & 8 deletions resource/modules/sample/sample.controller.js
Original file line number Diff line number Diff line change
@@ -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 );
8 changes: 4 additions & 4 deletions resource/modules/sample/sample.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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( {
Expand All @@ -31,7 +31,7 @@ class Sample {

schema.plugin( uniqueValidator );
try {
mongoose.model( 'sample', schema );
mongoose.model( 'MODULE_SINGULAR_CAMEL', schema );
} catch ( e ) {

}
Expand All @@ -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 };
14 changes: 8 additions & 6 deletions resource/modules/sample/sample.route.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions resource/modules/sample/sample.service.js
Original file line number Diff line number Diff line change
@@ -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 };
6 changes: 6 additions & 0 deletions resource/modules/sample/sample.settings.js
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
'use strict';
/**
* If custom router url is required, export url as "moduleRoute"
*/
module.exports = {

};
61 changes: 36 additions & 25 deletions src/module-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
};

Expand All @@ -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));
}

});
Expand Down

0 comments on commit b2d719b

Please sign in to comment.