-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
186 lines (158 loc) · 8.32 KB
/
main.ts
File metadata and controls
186 lines (158 loc) · 8.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import * as fs from 'fs';
import {
createAddElementFunction, createCancelChangeFunction, createEnableChangeFunction, createRemoveElementFunction,
createUpdateElementFunction,
generateFormGroupDefinitions,
generatePlural, generateStandardTsOutput,
preAppendix, removeDisabledAttributes, removeFormControlNameAttributes
} from "./utils/utils";
import "reflect-metadata";
import {BaseStyle} from "./styleModels/BaseStyle";
import {IonicStyle} from "./styleModels/IonicStyle";
import {BasicLanguageStyle} from "./languageModels/basicLanguageStyle";
import {TestClass} from "./testClasses/test";
import {EnglishLanguageStyle} from "./languageModels/englishStyle";
export const start = "\n\t\t\t\t";
let test = new TestClass();
let filePath = process.argv[2];
let className = "";
if(process.argv[3])
className = process.argv[3];
generateFormComponent(filePath, className);
async function generateFormComponent(filePath, className) {
console.log("starting generation ...");
if(fs.existsSync(filePath)) {
//dynamic import of file
let file = await <any>import(filePath);
let splitFilePathArr = filePath.split("/");
let fileName = splitFilePathArr[splitFilePathArr.length-1];
let pathTo = filePath.substring(0,filePath.length-fileName.length);
if(className == "") {
let fileSrc = fs.readFileSync(filePath).toString();
className = fileSrc.substring(fileSrc.indexOf("class ") + "class ".length).split(' ')[0];
}
console.log("generating form component for class: " + className + " ...");
let classToGenerate: any = new file[className]();
let lwcClassTitle = className.toLowerCase();
let pluralClassTitle = generatePlural(lwcClassTitle);
let selector = (classToGenerate[':selector'])?classToGenerate[':selector']:lwcClassTitle+"-component";
let listActivated = classToGenerate[':ngFor'];
let styleClass: BaseStyle = classToGenerate[':styleClass'];
if(!styleClass)
throw new Error("You have to define own class as style class through using a decorator like @Ionic");
let languageStyleClass: BasicLanguageStyle = classToGenerate[':languageStyle'] || new EnglishLanguageStyle();
console.log(languageStyleClass);
let formGroupActivated = classToGenerate[":formGroup"];
let formGroupProperty = className+"Form";
let formGroupDefinitions = "";
let ngFor = "",
formArrayDiv = "",
addBtn = "",
editBtn = "",
deleteBtn = "",
actionBtns = "",
addFunc = "",
removeFunc = "",
updateFunc = "",
enableChangeFunc = "",
cancelChangeFunc = "",
propertyDefinitionType = "",
propertyDefintionName = "",
formGroup = "";
if(listActivated) {
propertyDefintionName = pluralClassTitle;
if(formGroupActivated) {
propertyDefinitionType = "any[] = []";
ngFor = ` formArrayName='${pluralClassTitle}' *ngFor='let ${lwcClassTitle} of ${formGroupProperty}.get("${pluralClassTitle}").controls; let i = index'`;
formGroupDefinitions+=`\n\t\t\t${pluralClassTitle}: [[]]`;
formArrayDiv = `\n\t\t\t<div class='formWrapper' [formGroupName]='i'>`;
} else {
propertyDefinitionType = `${className}[] = []`;
ngFor = ` *ngFor='let ${className.toLowerCase()} of ${pluralClassTitle}; let i = index'`;
}
addBtn = "\n\t\t<:row class='addElementRow'>" +
"\n\t\t\t<:button class='standardBtn' (click)='add" + className + "()'>" + languageStyleClass.add(className) + "</button>" +
"\n\t\t</:row>";
editBtn = start+"<:col *ngIf='!"+lwcClassTitle+".changeActivated' col-2 class=\"centeredContent\">" +
start+"\t<:button class='standardBtn editBtn' (click)='enableChange("+lwcClassTitle+")'>"+languageStyleClass.edit(className)+"</:button>" +
start+"</:col>";
deleteBtn = start+'<:col col-2 class="centeredContent">' +
start+'\t<:button class="standardBtn deleteBtn" (click)="delete'+className+'('+((formGroupActivated)?"i":lwcClassTitle)+')">'+languageStyleClass.delete(className)+'</:button>' +
start+'</:col>';
actionBtns =
start+'<:col class="centeredContent actionBtnsCol" col-2 *ngIf="'+lwcClassTitle+'.changeActivated">'+
start+'\t<:row>'+
start+'\t\t<:col col-6 class="centeredContent">'+
styleClass.acceptBtn(className, languageStyleClass.save(className)) +
start+'\t\t</:col>'+
start+'\t\t<:col col-6 class="centeredContent">'+
styleClass.closeBtn(className, languageStyleClass.close(className)) +
start+'\t\t</:col>'+
start+'\t</:row>'+
start+'</:col>';
addFunc = createAddElementFunction(className, pluralClassTitle, classToGenerate, formGroupActivated);
removeFunc = createRemoveElementFunction(className, pluralClassTitle, formGroupActivated);
updateFunc = createUpdateElementFunction(className);
enableChangeFunc = createEnableChangeFunction(lwcClassTitle);
cancelChangeFunc = createCancelChangeFunction(className);
} else {
propertyDefintionName = lwcClassTitle;
propertyDefinitionType = className;
}
let tsOutput = generateStandardTsOutput(className, selector, classToGenerate[":classPrefix"], lwcClassTitle+".html");
let htmlOutput = styleClass.beginning(className, formGroupActivated, formGroupProperty)
+ addBtn
+ `\n\t\t<:row`
// check if *ngFor should be applied
+ ngFor
+" class='itemRow'>"
+formArrayDiv
+editBtn
+actionBtns;
let scssOutput = selector + " {\n\n}";
tsOutput += `\n\n\tpublic ${propertyDefintionName}: ${propertyDefinitionType};`;
tsOutput = preAppendix(tsOutput, `import { ${className} } from "../${fileName}";\n`);
htmlOutput += classToGenerate[':html-output'] + deleteBtn;
htmlOutput += styleClass.ending(formGroupActivated, listActivated);
htmlOutput = styleClass.replace(htmlOutput);
if(formGroupActivated) {
let formGroupPropertyDeclaration = formGroupProperty+": FormGroup;";
tsOutput = preAppendix(tsOutput, "import { FormBuilder, FormGroup, Validators "+((listActivated)?", FormArray":"")+"} from '@angular/forms';\n");
tsOutput += "\n\t"+formGroupPropertyDeclaration+"\n";
tsOutput += "\n\tconstructor(public formBuilder: FormBuilder) {" +
`\n\t\tthis.${formGroupProperty} = this.formBuilder.group({`+
((listActivated)?formGroupDefinitions:generateFormGroupDefinitions(classToGenerate)) +
"\n\t\t});" +
"\n\t}"
} else {
tsOutput += "\n\tconstructor(){}";
htmlOutput = removeFormControlNameAttributes(htmlOutput);
}
if(!listActivated)
htmlOutput = removeDisabledAttributes(htmlOutput, lwcClassTitle);
tsOutput += addFunc
+ removeFunc
+ updateFunc
+ enableChangeFunc
+ cancelChangeFunc;
tsOutput += "\n}";
if(!fs.existsSync(pathTo+lwcClassTitle))
fs.mkdirSync(pathTo+lwcClassTitle);
let fullPath = pathTo+lwcClassTitle+"/"+lwcClassTitle;
fs.writeFileSync(fullPath+".html", htmlOutput);
fs.writeFileSync(fullPath+".scss", scssOutput);
if(classToGenerate[':generateModule']) {
let tsModuleOutput = "";
if(classToGenerate[':ionicPage'] && classToGenerate[":ionic"]) {
tsModuleOutput = (<IonicStyle>styleClass).generatePageModule(className);
tsOutput = preAppendix(tsOutput, "import { IonicPage } from 'ionic-angular';\n");
}
else
tsModuleOutput = styleClass.generateModule(className);
fs.writeFileSync(fullPath+".module.ts", tsModuleOutput);
}
fs.writeFileSync(fullPath+".ts", tsOutput);
} else {
throw new Error("File doesn't exist");
}
}