Skip to content

Commit

Permalink
feat(ngModule): create ngModule and fill routing
Browse files Browse the repository at this point in the history
  • Loading branch information
afontainec committed Oct 22, 2020
1 parent d830f62 commit dd86486
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
40 changes: 32 additions & 8 deletions cliApp/views/angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const routerFile = 'router.ts';

const createFile = async (tableName, values, config) => {
const APP_PATH = getAngularAppPath(config);
const directory = await buildService(values, APP_PATH);
await buildRouter(values, APP_PATH, directory);
await buildModule(values, APP_PATH);
await buildService(values, APP_PATH);
await createNewComponent(values, APP_PATH);
await createShowComponent(values, APP_PATH);
await createEditComponent(values, APP_PATH);
Expand All @@ -25,6 +25,27 @@ const getAngularAppPath = (config) => {
return config.views.angular || '.';
};

// #region MODULE
const buildModule = async (values, APP_PATH) => {
const command = ngGenerateModule(values, APP_PATH);
const result = execSync(command).toString();
const filename = getRouterPath(APP_PATH, result);
await buildRouter(values, APP_PATH, filename);
};

const ngGenerateModule = (values, APP_PATH) => {
const NAME = values.KEBAB_CASE;
return ngGenerate('module', APP_PATH, NAME, '--routing');
};

const getRouterPath = (APP_PATH, result) => {
const lines = result.split(/\r?\n/);
const [,, routingFile] = getTSFile(lines);
return path.join(APP_PATH, routingFile);
};


// #endregion

// #region SERVICE
const buildService = async (values, APP_PATH) => {
Expand Down Expand Up @@ -139,23 +160,25 @@ const buildSchemaPath = (first, second, third) => {
};


const ngGenerate = (schema, APP_PATH, schemaPath) => {
const command = `cd '${APP_PATH}' && ng generate ${schema} '${schemaPath}'`;
const ngGenerate = (schema, APP_PATH, schemaPath, flags = '') => {
const command = `cd '${APP_PATH}' && ng generate ${schema} '${schemaPath}' ${flags}`;
return command;
};

const getTSFile = (lines) => {
let tsFile;
let tsTestFile;
let routingFile;
for (let i = 0; i < lines.length; i++) {
const element = lines[i];
if (element.includes('.ts') && element.startsWith('CREATE')) {
const pieces = element.split(' ');
if (element.includes('.spec.ts')) [, tsTestFile] = pieces;
else if (element.includes('-routing.module.ts')) [, routingFile] = pieces;
else [, tsFile] = pieces;
}
}
return [tsFile, tsTestFile];
return [tsFile, tsTestFile, routingFile];
};

const getHTMLFile = (lines) => {
Expand All @@ -169,10 +192,11 @@ const getHTMLFile = (lines) => {
throw new Error(`Could not create angular: missing TS FILE in ${lines}`);
};

const buildRouter = (values, APP_PATH, directory) => {
directory = path.dirname(directory);
const buildRouter = (values, APP_PATH, routerPath) => {
const directory = path.dirname(routerPath);
const filename = path.basename(routerPath);
const sample = path.join(SAMPLE_DIR, routerFile);
const service = new FileCreator(sample, directory, 'router.ts');
const service = new FileCreator(sample, directory, filename);
return service.create(values, true);
};

Expand Down
13 changes: 10 additions & 3 deletions example/views/angular/router.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { New$PASCAL_CASE$Component } from './new-$KEBAB_CASE$/new-$KEBAB_CASE$.component';
import { Edit$PASCAL_CASE$Component } from './edit-$KEBAB_CASE$/edit-$KEBAB_CASE$.component';
import { Show$PASCAL_CASE$Component } from './show-$KEBAB_CASE$/show-$KEBAB_CASE$.component';
import { Index$PASCAL_CASE$Component } from './index-$KEBAB_CASE$/index-$KEBAB_CASE$.component';
import { Routes } from '@angular/router';

const $CAMEL_CASE$Routes: Routes = [{

const routes: Routes = [{
path: '$SNAKE_CASE$/new',
component: New$PASCAL_CASE$Component,
data: { title: 'New $PASCAL_CASE$' },
Expand All @@ -22,4 +25,8 @@ const $CAMEL_CASE$Routes: Routes = [{
data: { title: 'Edit $PASCAL_CASE$' },
}];

export { $CAMEL_CASE$Routes };
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class $PASCAL_CASE$RoutingModule { }

0 comments on commit dd86486

Please sign in to comment.