Skip to content

Commit

Permalink
feat(ui): module generate feature added
Browse files Browse the repository at this point in the history
ui generate

Fixes #5
  • Loading branch information
sumitparakh committed Dec 17, 2018
1 parent df0b77c commit faa18c1
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/klingon-ui/src/app/cli/cli.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class CliService {
key !== 'enum-name' &&
key !== 'interface-name' &&
key !== 'interface-type' &&
key !== 'module-name' &&
key !== 'app'
)
.map(key => `--${key}=${values[key]}`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,13 @@ <h6 class="sub-title">Configure other flags for the create command</h6>
<app-drop-down [open]="false" contentHeight="750px" [disabled]="false">

<h3 class="title" style="padding-bottom:5px;">
<mat-checkbox [disabled]="false">&nbsp;Module</mat-checkbox>
<mat-checkbox [disabled]="false" [checked]="generateConfig.module">&nbsp;Module</mat-checkbox>
</h3>
<!-- <mat-icon>settings</mat-icon> -->
<h6 class="sub-title">Configure other flags for the create command</h6>
<main class="content">
<app-generate-module [index]="i" [form]="form" (onModuleAdded)="addModule($event)" (onModuleRemoved)="removeModule($event,index)"></app-generate-module>
</main>
</app-drop-down>

<!-- Generate a pipe -->
Expand Down Expand Up @@ -187,10 +190,10 @@ <h6 class="sub-title">View command history and logs</h6>

<section>
<p class="button-container">
<button mat-raised-button color="warn" [disabled]="form.invalid || (!generateConfig.component && !generateConfig.class && !generateConfig.directive && !generateConfig.enum && !generateConfig.interface)" (click)="stop()">
<button mat-raised-button color="warn" [disabled]="form.invalid || (!generateConfig.component && !generateConfig.class && !generateConfig.directive && !generateConfig.enum && !generateConfig.interface && !generateConfig.module)" (click)="stop()">
<mat-icon>stop</mat-icon> Stop
</button>
<button mat-raised-button color="primary" [disabled]="form.invalid || (!generateConfig.component && !generateConfig.class && !generateConfig.directive && !generateConfig.enum && !generateConfig.interface)" (click)="generate()">
<button mat-raised-button color="primary" [disabled]="form.invalid || (!generateConfig.component && !generateConfig.class && !generateConfig.directive && !generateConfig.enum && !generateConfig.interface && !generateConfig.module)" (click)="generate()">
<mat-icon>play_circle_outline</mat-icon> Generate
</button>
</p>
Expand Down
52 changes: 51 additions & 1 deletion packages/klingon-ui/src/app/cli/generate/generate.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class CliGenerateComponent extends FlagsComponent implements OnInit {

form: FormGroup;

generateConfig: any = { component: false, class: false, directive: false, enum: false, interface: false };
generateConfig: any = { component: false, class: false, directive: false, enum: false, interface: false, module: false };

constructor(public cli: CliService) {
super();
Expand Down Expand Up @@ -82,6 +82,17 @@ export class CliGenerateComponent extends FlagsComponent implements OnInit {
this.form.controls.interfaces['controls'] = [];
this.generateConfig.interface = false;
}

// Generate modules if all required fields are entered.
if (this.isValid(this.form.controls.modules)) {
const moduleFormGroups: FormGroup[] = this.form.controls.modules['controls'];
moduleFormGroups.forEach(async moduleGroup => {
await new Promise(resolve => setTimeout(resolve, 0, this.generateModule(moduleGroup)));
});
this.form.controls.modules['controls'] = [];
this.generateConfig.module = false;
}

}

/**
Expand Down Expand Up @@ -224,6 +235,34 @@ export class CliGenerateComponent extends FlagsComponent implements OnInit {
});
}

/**
* Generate Modules
*/
generateModule(module: FormGroup) {
return new Promise((resolve, reject) => {
this.isWorking = true;
this.cli
.runNgCommand(
`generate module ${module.value['module-name']} ${this.cli.serialize(
module.value)}`,
this.form.value['root-dir'] + '/' + this.form.value['app-name'])
.subscribe((data: any) => {
this.isWorking = false;

if (data.stderr) {
this.onStdErr.next(data.stderr);
reject(data);
} else {
if (data.exit === 0) {
resolve(data);
} else {
this.onStdOut.next(data.stdout);
}
}
});
});
}

/**
* Event handler of onComponentAdded Event.
*
Expand Down Expand Up @@ -263,6 +302,13 @@ export class CliGenerateComponent extends FlagsComponent implements OnInit {
});
}

addModule(module: FormGroup) {
module.valueChanges.subscribe((data: any) => {
this.generateConfig.module = this.isValid(this.form.controls.modules);
console.log(this.generateConfig);
});
}

/**
* Event handler of onComponentRemoved Event.
* It checks total number of valid components after a component is removed and enable generate form accordingly
Expand Down Expand Up @@ -291,6 +337,10 @@ export class CliGenerateComponent extends FlagsComponent implements OnInit {
this.generateConfig.interface = this.isValid(this.form.controls.interfaces);
}

removeModule(index) {
this.generateConfig.module = this.isValid(this.form.controls.modules);
}

/**
* Check if required fields of all added components are filled and then check/uncheck checkbox accordingly
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.module-form mat-form-field {
width: 100%
}

mat-action-row {
border: none;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,68 @@
<p>
module works!
<p *ngIf="form.get('modules').controls.length === 0">
Please add module to continue
</p>
<app-drop-down *ngFor="let module of form.get('modules').controls; let i = index">
<h3 class="title component-title">
<span>
{{ module.value['module-name'] ? module.value['module-name'] : 'Module '+(i+1) }}
</span>
</h3>
<h6 class="sub-title">Application Module</h6>
<main class="content" [formGroup]="module">

<div class="module-form">
<p>
<mat-form-field>
<input formControlName="module-name" matInput placeholder="The name of the NgModule."
required>
<mat-hint align="start">The name of the NgModule.</mat-hint>
</mat-form-field>
</p>
<p>
<mat-form-field>
<input formControlName="module" matInput placeholder="The declaring NgModule. (-m)">
<mat-hint align="start">The declaring NgModule.</mat-hint>
</mat-form-field>
</p>
<p>
<mat-form-field>
<input formControlName="project" matInput placeholder="The name of the project. (-m)">
<mat-hint align="start">The name of the project.</mat-hint>
</mat-form-field>
</p>
<p>
<mat-form-field>
<mat-select formControlName="routing-scope" placeholder="The scope for the new routing module. (Default: Child)">
<mat-option *ngFor="let value of routingScopes" [value]="value">
{{value}}
</mat-option>
</mat-select>
<mat-hint align="start">The scope for the new routing module.</mat-hint>
</mat-form-field>
</p>
<p>
<mat-list>
<mat-list-item>
<mat-checkbox formControlName="routing">When true, creates a routing module (Default: false).</mat-checkbox>
</mat-list-item>
<mat-list-item>
<mat-checkbox formControlName="flat">Flag to indicate if a dir is created.</mat-checkbox>
</mat-list-item>
</mat-list>
</p>
</div>
</main>
<div class="action">
<button mat-icon-button type="button" (click)="removeModule(i);">
<mat-icon aria-label="Example icon-button with a heart icon">remove_circle</mat-icon>
</button>
</div>

</app-drop-down>
<mat-action-row>
<button mat-mini-fab color="primary" type="button" (click)="addNewModule($event)">
<i class="material-icons action">
add
</i>
</button>
</mat-action-row>
Original file line number Diff line number Diff line change
@@ -1,15 +1,48 @@
import { Component, OnInit } from '@angular/core';
import { Component, Input, EventEmitter, Output } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';

@Component({
selector: 'app-module',
selector: 'app-generate-module',
templateUrl: './module.component.html',
styleUrls: ['./module.component.css']
})
export class ModuleComponent implements OnInit {
export class ModuleComponent {

constructor() { }
@Input()
public form: FormGroup;

ngOnInit() {
@Input()
public index: number;

@Output()
onModuleAdded: EventEmitter<FormGroup> = new EventEmitter<FormGroup>();

@Output()
onModuleRemoved: EventEmitter<any> = new EventEmitter<any>();

formControls: FormControl[];

routingScopes: string[] = ['Child', 'Root'];

static buildModuleForm() {
return new FormGroup({
'module-name': new FormControl('', Validators.required),
'module': new FormControl(''),
'project': new FormControl(''),
'routing-scope': new FormControl(''),
'routing': new FormControl(false),
'flat': new FormControl(false)
});
}

addNewModule(event) {
const formGroup = ModuleComponent.buildModuleForm();
this.form.controls.modules['controls'].push(formGroup);
this.onModuleAdded.emit(formGroup);
}

removeModule(index) {
this.form.controls.modules['controls'].splice(index, 1);
this.onModuleRemoved.emit(index);
}
}

0 comments on commit faa18c1

Please sign in to comment.