Skip to content

Commit de08554

Browse files
committed
feat(ui): generate feature for enum added
ui generate Fixes #5
1 parent f453263 commit de08554

File tree

7 files changed

+147
-18
lines changed

7 files changed

+147
-18
lines changed

packages/klingon-ui/src/app/cli/cli.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export class CliService {
4545
key !== 'component-name' &&
4646
key !== 'class-name' &&
4747
key !== 'directive-name' &&
48+
key !== 'enum-name' &&
4849
key !== 'app'
4950
)
5051
.map(key => `--${key}=${values[key]}`)

packages/klingon-ui/src/app/cli/generate/directive/directive.component.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { FormGroup, FormControl, Validators } from '@angular/forms';
66
templateUrl: './directive.component.html',
77
styleUrls: ['./directive.component.css']
88
})
9-
export class DirectiveComponent implements OnInit {
9+
export class DirectiveComponent {
1010

1111
@Input()
1212
public form: FormGroup;
@@ -36,11 +36,6 @@ export class DirectiveComponent implements OnInit {
3636
});
3737
}
3838

39-
constructor() { }
40-
41-
ngOnInit() {
42-
}
43-
4439
addNewDirective(event) {
4540
const formGroup = DirectiveComponent.buildDirectiveForm();
4641
this.form.controls.directives['controls'].push(formGroup);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.enum-form mat-form-field {
2+
width: 100%
3+
}
4+
5+
mat-action-row {
6+
border: none;
7+
}
Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,49 @@
1-
<p>
2-
enum works!
1+
<p *ngIf="form.get('enums').controls.length === 0">
2+
Please add enum to continue
33
</p>
4+
<app-drop-down *ngFor="let enum of form.get('enums').controls; let i = index">
5+
<h3 class="title component-title">
6+
<span>
7+
{{ enum.value['enum-name'] ? enum.value['enum-name'] : 'Enum '+(i+1) }}
8+
</span>
9+
</h3>
10+
<h6 class="sub-title">Enum</h6>
11+
<main class="content" [formGroup]="enum">
12+
13+
<div class="enum-form">
14+
<p>
15+
<mat-form-field>
16+
<input formControlName="enum-name" matInput placeholder="The name of the new enum."
17+
required>
18+
<mat-hint align="start">The name of the new enum.</mat-hint>
19+
</mat-form-field>
20+
</p>
21+
<p>
22+
<mat-form-field>
23+
<input formControlName="project" matInput placeholder="The name of the project">
24+
<mat-hint align="start">The name of the project</mat-hint>
25+
</mat-form-field>
26+
</p>
27+
<p>
28+
<mat-list>
29+
<mat-list-item>
30+
<mat-checkbox formControlName="lint-fix">When true, applies lint fixes after generating the directive.</mat-checkbox>
31+
</mat-list-item>
32+
</mat-list>
33+
</p>
34+
</div>
35+
</main>
36+
<div class="action">
37+
<button mat-icon-button type="button" (click)="removeEnum(i);">
38+
<mat-icon aria-label="Example icon-button with a heart icon">remove_circle</mat-icon>
39+
</button>
40+
</div>
41+
42+
</app-drop-down>
43+
<mat-action-row>
44+
<button mat-mini-fab color="primary" type="button" (click)="addNewEnum($event)">
45+
<i class="material-icons action">
46+
add
47+
</i>
48+
</button>
49+
</mat-action-row>
Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,44 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
2+
import { FormGroup, FormControl, Validators } from '@angular/forms';
23

34
@Component({
4-
selector: 'app-enum',
5+
selector: 'app-generate-enum',
56
templateUrl: './enum.component.html',
67
styleUrls: ['./enum.component.css']
78
})
8-
export class EnumComponent implements OnInit {
9+
export class EnumComponent {
910

10-
constructor() { }
11+
@Input()
12+
public form: FormGroup;
1113

12-
ngOnInit() {
14+
@Input()
15+
public index: number;
16+
17+
@Output()
18+
onEnumAdded: EventEmitter<FormGroup> = new EventEmitter<FormGroup>();
19+
20+
@Output()
21+
onEnumRemoved: EventEmitter<any> = new EventEmitter<any>();
22+
23+
formControls: FormControl[];
24+
25+
static buildEnumForm() {
26+
return new FormGroup({
27+
'enum-name': new FormControl('', Validators.required),
28+
'project': new FormControl(''),
29+
'lint-fix': new FormControl(false)
30+
});
31+
}
32+
33+
addNewEnum(event) {
34+
const formGroup = EnumComponent.buildEnumForm();
35+
this.form.controls.enums['controls'].push(formGroup);
36+
this.onEnumAdded.emit(formGroup);
37+
}
38+
39+
removeEnum(index) {
40+
this.form.controls.enums['controls'].splice(index, 1);
41+
this.onEnumRemoved.emit(index);
1342
}
1443

1544
}

packages/klingon-ui/src/app/cli/generate/generate.component.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,13 @@ <h6 class="sub-title">Configure other flags for the create command</h6>
8383
<app-drop-down [open]="false" contentHeight="750px" [disabled]="false">
8484

8585
<h3 class="title" style="padding-bottom:5px;">
86-
<mat-checkbox [disabled]="false">&nbsp;Enum</mat-checkbox>
86+
<mat-checkbox [disabled]="false" [checked]="generateConfig.enum">&nbsp;Enum</mat-checkbox>
8787
</h3>
8888
<!-- <mat-icon>settings</mat-icon> -->
8989
<h6 class="sub-title">Configure other flags for the create command</h6>
90+
<main class="content">
91+
<app-generate-enum [index]="i" [form]="form" (onEnumAdded)="addEnum($event)" (onEnumRemoved)="removeEnum($event,index)"></app-generate-enum>
92+
</main>
9093
</app-drop-down>
9194

9295
<!-- Generate a guard -->
@@ -181,10 +184,10 @@ <h6 class="sub-title">View command history and logs</h6>
181184

182185
<section>
183186
<p class="button-container">
184-
<button mat-raised-button color="warn" [disabled]="form.invalid || (!generateConfig.component && !generateConfig.class && !generateConfig.directive)" (click)="stop()">
187+
<button mat-raised-button color="warn" [disabled]="form.invalid || (!generateConfig.component && !generateConfig.class && !generateConfig.directive && !generateConfig.enum)" (click)="stop()">
185188
<mat-icon>stop</mat-icon> Stop
186189
</button>
187-
<button mat-raised-button color="primary" [disabled]="form.invalid || (!generateConfig.component && !generateConfig.class && !generateConfig.directive)" (click)="generate()">
190+
<button mat-raised-button color="primary" [disabled]="form.invalid || (!generateConfig.component && !generateConfig.class && !generateConfig.directive && !generateConfig.enum)" (click)="generate()">
188191
<mat-icon>play_circle_outline</mat-icon> Generate
189192
</button>
190193
</p>

packages/klingon-ui/src/app/cli/generate/generate.component.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class CliGenerateComponent extends FlagsComponent implements OnInit {
1212

1313
form: FormGroup;
1414

15-
generateConfig: any = { component: false, class: false, directive: false };
15+
generateConfig: any = { component: false, class: false, directive: false, enum: false };
1616

1717
constructor(public cli: CliService) {
1818
super();
@@ -62,6 +62,16 @@ export class CliGenerateComponent extends FlagsComponent implements OnInit {
6262
this.form.controls.directives['controls'] = [];
6363
this.generateConfig.directive = false;
6464
}
65+
66+
// Generate enums if all required fields are entered.
67+
if (this.isValid(this.form.controls.enums)) {
68+
const enumFormGroups: FormGroup[] = this.form.controls.enums['controls'];
69+
enumFormGroups.forEach(async enumGroup => {
70+
await new Promise(resolve => setTimeout(resolve, 0, this.generateEnum(enumGroup)));
71+
});
72+
this.form.controls.enums['controls'] = [];
73+
this.generateConfig.enum = false;
74+
}
6575
}
6676

6777
/**
@@ -120,7 +130,7 @@ export class CliGenerateComponent extends FlagsComponent implements OnInit {
120130
}
121131

122132
/**
123-
* Generate Classes
133+
* Generate Directives
124134
*/
125135
generateDirective(directive: FormGroup) {
126136
return new Promise((resolve, reject) => {
@@ -147,6 +157,34 @@ export class CliGenerateComponent extends FlagsComponent implements OnInit {
147157
});
148158
}
149159

160+
/**
161+
* Generate Enums
162+
*/
163+
generateEnum(directive: FormGroup) {
164+
return new Promise((resolve, reject) => {
165+
this.isWorking = true;
166+
this.cli
167+
.runNgCommand(
168+
`generate enum ${directive.value['enum-name']} ${this.cli.serialize(
169+
directive.value)}`,
170+
this.form.value['root-dir'] + '/' + this.form.value['app-name'])
171+
.subscribe((data: any) => {
172+
this.isWorking = false;
173+
174+
if (data.stderr) {
175+
this.onStdErr.next(data.stderr);
176+
reject(data);
177+
} else {
178+
if (data.exit === 0) {
179+
resolve(data);
180+
} else {
181+
this.onStdOut.next(data.stdout);
182+
}
183+
}
184+
});
185+
});
186+
}
187+
150188
/**
151189
* Event handler of onComponentAdded Event.
152190
*
@@ -172,6 +210,12 @@ export class CliGenerateComponent extends FlagsComponent implements OnInit {
172210
});
173211
}
174212

213+
addEnum(_enum: FormGroup) {
214+
_enum.valueChanges.subscribe((data: any) => {
215+
this.generateConfig.enum = this.isValid(this.form.controls.enums);
216+
console.log(this.generateConfig);
217+
});
218+
}
175219
/**
176220
* Event handler of onComponentRemoved Event.
177221
* It checks total number of valid components after a component is removed and enable generate form accordingly
@@ -192,6 +236,10 @@ export class CliGenerateComponent extends FlagsComponent implements OnInit {
192236
this.generateConfig.directive = this.isValid(this.form.controls.directives);
193237
}
194238

239+
removeEnum(index) {
240+
this.generateConfig.enum = this.isValid(this.form.controls.enums);
241+
}
242+
195243
/**
196244
* Check if required fields of all added components are filled and then check/uncheck checkbox accordingly
197245
*/

0 commit comments

Comments
 (0)