Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nested module creation #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
2 changes: 1 addition & 1 deletion resource/modules/sample/sample.controller.js
Original file line number Diff line number Diff line change
@@ -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' );
Expand Down
3 changes: 2 additions & 1 deletion resource/modules/sample/sample.route.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -14,3 +14,4 @@ router.delete( '/:id', MODULE_SINGULAR_PASCALController.delete );


module.exports = router;

2 changes: 1 addition & 1 deletion resource/modules/sample/sample.service.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 11 additions & 2 deletions resource/project/system/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,23 @@ 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 )}`,
'Mapped': '✔',
'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( {
Expand All @@ -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();
} );
Expand All @@ -77,6 +85,7 @@ const loadModules = ( basePath, baseRoute, routerPaths ) => {
} );
};


router.use((req, res, next) => {
res.sendCalmResponse = CalmResponse;
res.sendCalmResponse.bind(res);
Expand Down
37 changes: 33 additions & 4 deletions src/module-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`));
Expand Down Expand Up @@ -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');
Expand All @@ -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);
Expand All @@ -66,13 +93,15 @@ 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);
// eslint-disable-next-line no-param-reassign
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);
Expand Down