-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
880 lines (848 loc) · 44.1 KB
/
gulpfile.js
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
'use strict';
const gulp = require('gulp');
const prompt = require('inquirer').createPromptModule();
const fs = require('fs');
const fsExtra = require('fs-extra');
const color = require('gulp-color');
const mkdirp = require('mkdirp');
const del = require('del');
const shell = require('gulp-shell');
const TEMPLATES = require('./resources/templates/file_templates/index');
const UTILS = require('./resources/helpers/index');
const {version} = require('./package.json');
let config;
try {
config = require(process.cwd() + '/winkit.conf');
} catch (e) {
config = {};
}
/**
* A string replacer.
* @param {string} match
* @param {string} name
* @returns {string}
*/
function generateContent(match, name = null) {
const dataArr = match.split(',');
const firstElArr = dataArr[0].split('.');
switch (firstElArr[0]) {
case 'eval':
return eval(dataArr[1]);
case 'path.relative':
return dataArr[2] === 'correspondingModelPath' ? './../server/models/Server' + name : './interface/Mappable';
case 'selectedServer':
return config.selectedServer === dataArr[1] ? dataArr[2] : dataArr[3];
case 'config':
return config[firstElArr[1]];
case 'ThisName':
if (firstElArr[1] && name[firstElArr[1].replace(/[^a-z]/gi, '')]) {
return name[firstElArr[1].replace(/[^a-z]/gi, '')]();
}
return name;
default:
return match;
}
}
/**
* Recursively triggers the create() method for elements of the taskList array.
* @param {string} name - Name of model.
* @param {elementTypes[]} taskList - Array of elementTypes enum members.
* @param {number} [counter = 0] - used in reccurence.
* @param {[]} [results = []] - used in reccurence.
* @returns {Promise<boolean>}
*/
async function parseTasks(name, taskList, counter = 0, results = []) {
if (taskList && taskList.length) {
switch(true) {
case taskList[counter].indexOf( UTILS.elementTypes.SERVICE.toUpperCase() ) > -1:
results.push( await create( UTILS.elementTypes.SERVICE, name ) );
break;
case taskList[counter].indexOf( UTILS.elementTypes.LIST.toUpperCase() ) > -1:
results.push( await create( UTILS.elementTypes.LIST, name ) );
break;
case taskList[counter].indexOf( UTILS.elementTypes.DETAIL.toUpperCase() ) > -1:
results.push( await create( UTILS.elementTypes.DETAIL, name ) );
break;
}
if (counter + 1 === taskList.length) {
return Promise.all(results);
} else {
parseTasks(name, taskList, ++counter, results);
}
} else {
return Promise.resolve(true);
}
}
/**
* Adds ngClassName import in file and pushedElement or ngClassName to an array that matches the arrayPositionRegex pattern.
* @param {string} ngClassName
* @param {string} modulePath - Path of amended file.
* @param {string} relativeClassPath - Relative path of ngClassName.
* @param {RegExp} arrayPositionRegex - Position of target array.
* @param {number} indent - The indentation of the element pushed to the array.
* @param {string} [pushedElement] - Optional element to be pushed to array instead of ngClassName.
* @returns {boolean}
*/
function addToModuleOrRouting(ngClassName, modulePath, relativeClassPath, arrayPositionRegex, indent = 4, pushedElement = null) {
const moduleContent = fs.readFileSync(modulePath, 'utf8');
if (moduleContent.includes(pushedElement || ngClassName)) {
return false;
}
const endOfImportsRegex = ngClassName ? UTILS.REGEXS.endOfImports : null;
const newContent = moduleContent
.replace(endOfImportsRegex, (m, p1, p2, p3) => {
return `${p1.trim()}\nimport {${ngClassName}} from '${relativeClassPath}';\n${p2.replace(/^\s+/,'')}\n${p3.replace(/^\s+/,'')}`;
})
.replace(arrayPositionRegex, match => {
const lastChar = match.slice(-1).trim();
return match.slice(0, -1).trim() + '\n'
+ ' '.repeat(indent) + (pushedElement || ngClassName)
+ ',\n' + (lastChar ? ' '.repeat(indent > 2 ? indent - 2 : 0) + lastChar : lastChar)
});
if (newContent) {
fs.writeFileSync(modulePath, newContent, 'utf8');
} else {
return true;
}
}
/**
* Generates 3 files inside modules/<name>/models/ folder: <name>.ts, Server<name>.ts and <name>DataFactory.ts.
* @param {string} name - Name of the model.
* @returns {Promise<boolean>}
*/
function createModelFiles(name) {
const nameLowerCase = name.toLowerCase();
return new Promise(resolve => {
mkdirp(`src/app/modules/${nameLowerCase}/models/`, (err) => {
if (err) {
resolve(false);
} else {
console.log(UTILS.dynamicTexts.createModel(name)[0]);
const modelContent = TEMPLATES.modelTemplate.replace(UTILS.REGEXS.fileTemplate, (_, match) => generateContent(match, name));
const serverModelContent = TEMPLATES.serverModelTemplate.replace(UTILS.REGEXS.fileTemplate, (_, match) => generateContent(match, name));
const dataFactoryContent = TEMPLATES.dataFactoryTemplate.replace(UTILS.REGEXS.fileTemplate, (_, match) => generateContent(match, name));
if (!modelContent || !serverModelContent || !dataFactoryContent) {
console.log(color(UTILS.staticTexts.aborting[0], UTILS.staticTexts.aborting[1]));
resolve(false);
}
fs.writeFileSync(`src/app/modules/${nameLowerCase}/models/${name}.ts`, modelContent, 'utf-8');
fs.writeFileSync(`src/app/modules/${nameLowerCase}/models/Server${name}.ts`, serverModelContent, 'utf-8');
fs.writeFileSync(`src/app/modules/${nameLowerCase}/models/${name}DataFactory.ts`, dataFactoryContent, 'utf-8');
console.log(color(UTILS.staticTexts.modelCreated[0], UTILS.staticTexts.modelCreated[1]));
resolve(true);
}
});
});
}
/**
* Generates the model's service file inside modules/<name>/service/ folder and provides the service in <name>.module.ts.
* @param {string} name - Name of the model.
* @returns {Promise<boolean>}
*/
function createServiceFiles(name) {
const nameLowerCase = name.toLowerCase();
return new Promise(resolve => {
mkdirp(`src/app/modules/${nameLowerCase}/service/`, (err) => {
if (err) {
resolve(false);
} else {
console.log(UTILS.dynamicTexts.createService(name)[0]);
const serviceFileRelative = `service/${nameLowerCase}.service`;
const serviceContent = TEMPLATES.serviceTemplate.replace(UTILS.REGEXS.fileTemplate, (_, match) => generateContent(match, name));
if (!serviceContent) {
console.log(color(UTILS.staticTexts.aborting[0], UTILS.staticTexts.aborting[1]));
resolve(false);
}
fs.writeFileSync(`src/app/modules/${nameLowerCase}/service/${nameLowerCase}.service.ts`, serviceContent, 'utf-8');
addToModuleOrRouting(name + 'Service', `src/app/modules/${nameLowerCase}/${nameLowerCase}.module.ts`, './' + serviceFileRelative, /providers\: \[[\w\s\n\,]*\]/m);
console.log(color(UTILS.staticTexts.serviceCreated[0], UTILS.staticTexts.serviceCreated[1]));
resolve(true);
}
});
});
}
/**
* Generates the model's detail files inside modules/<name>/<name>-detail/ folder, imports the detail component in <name>.module.ts and adds the detail route to <name>.routing.ts.
* @param {string} name - Name of the model.
* @returns {Promise<boolean>}
*/
function createDetailFiles(name) {
const nameLowerCase = name.toLowerCase();
return new Promise(resolve => {
mkdirp(`src/app/modules/${nameLowerCase}/${nameLowerCase}-detail/`, (err) => {
if (err) {
resolve(false);
} else {
console.log(UTILS.dynamicTexts.createDetail(name)[0]);
const detailFileRelative = `${nameLowerCase}-detail/${nameLowerCase}-detail.component`;
const detailTScontent = TEMPLATES.componentDetailViewModelTemplate.replace(UTILS.REGEXS.fileTemplate, (_, match) => generateContent(match, name));
const detailHTMLcontent = TEMPLATES.componentDetailViewTemplate.replace(UTILS.REGEXS.fileTemplate, (_, match) => generateContent(match, name));
const detailSCSScontent = TEMPLATES.componentDetailStyleTemplate.replace(UTILS.REGEXS.fileTemplate, (_, match) => generateContent(match, name));
if (!detailTScontent || !detailHTMLcontent || !detailSCSScontent) {
console.log(color(UTILS.staticTexts.aborting[0], UTILS.staticTexts.aborting[1]));
resolve(false);
}
fs.writeFileSync(`src/app/modules/${nameLowerCase}/${detailFileRelative}.ts`, detailTScontent, 'utf-8');
fs.writeFileSync(`src/app/modules/${nameLowerCase}/${detailFileRelative}.html`, detailHTMLcontent, 'utf-8');
fs.writeFileSync(`src/app/modules/${nameLowerCase}/${detailFileRelative}.scss`, detailSCSScontent, 'utf-8');
addToModuleOrRouting(name + 'DetailComponent',
`src/app/modules/${nameLowerCase}/${nameLowerCase}.module.ts`,
'./' + detailFileRelative,
/(declarations|exports): \[[\w\s\n\,]*\]/gm);
addToModuleOrRouting(name + 'DetailComponent',
`src/app/modules/${nameLowerCase}/${nameLowerCase}.routing.ts`,
`./${detailFileRelative}`,
/component\: PlatformLayoutComponent\,(?:(?:.|\s)*?)children:\s*\[(?:\s|.)/m, 8,
`{\n${' '.repeat(10)}canActivate: [AdminGuard],\n${' '.repeat(10)}path: '${nameLowerCase}/:id',\n${' '.repeat(10)}component: ${name}DetailComponent\n${' '.repeat(8)}}`);
console.log(color(UTILS.staticTexts.detailCreated[0], UTILS.staticTexts.detailCreated[1]));
resolve(true);
};
});
});
}
/**
* Generates the model's list files inside modules/<name>/<name>-list/ folder, imports the list component in <name>.module.ts and adds the list routes to <name>.routing.ts and @core/sidebar/sidebar-routes.config.ts.
* @param {string} name - Name of the model.
* @returns {Promise<boolean>}
*/
function createListFiles(name) {
const nameLowerCase = name.toLowerCase();
return new Promise(resolve => {
mkdirp(`src/app/modules/${nameLowerCase}/${nameLowerCase}-list/`, (err) => {
if (err) {
resolve(false);
} else {
console.log(UTILS.dynamicTexts.createList(name)[0]);
const listFileRelative = `${nameLowerCase}-list/${nameLowerCase}-list.component`;
const tsContent = TEMPLATES.componentListViewModelTemplate.replace(UTILS.REGEXS.fileTemplate, (_, match) => generateContent(match, name));
const htmlContent = TEMPLATES.componentListViewTemplate.replace(UTILS.REGEXS.fileTemplate, (_, match) => generateContent(match, name));
const scssContent = TEMPLATES.componentListStyleTemplate.replace(UTILS.REGEXS.fileTemplate, (_, match) => generateContent(match, name));
if (!tsContent || !htmlContent || !scssContent) {
console.log(color(UTILS.staticTexts.aborting[0], UTILS.staticTexts.aborting[1]));
resolve(false);
}
fs.writeFileSync(`src/app/modules/${nameLowerCase}/${listFileRelative}.ts`, tsContent, 'utf-8');
fs.writeFileSync(`src/app/modules/${nameLowerCase}/${listFileRelative}.html`, htmlContent, 'utf-8');
fs.writeFileSync(`src/app/modules/${nameLowerCase}/${listFileRelative}.scss`, scssContent, 'utf-8');
let arrayPositionRegex = /(declarations|exports): \[[\w\s\n\,]*\]/gm;
addToModuleOrRouting(name + 'ListComponent', `src/app/modules/${nameLowerCase}/${nameLowerCase}.module.ts`, './' + listFileRelative, arrayPositionRegex);
addToModuleOrRouting(name + 'ListComponent',
`src/app/modules/${nameLowerCase}/${nameLowerCase}.routing.ts`,
`./${listFileRelative}`,
/component\: PlatformLayoutComponent\,(?:(?:.|\s)*?)children:\s*\[(?:\s|.)/m, 8,
`{\n${' '.repeat(10)}canActivate: [AdminGuard],\n${' '.repeat(10)}path: '${nameLowerCase}-list',\n${' '.repeat(10)}component: ${name}ListComponent\n${' '.repeat(8)}}`);
prompt({
type: 'input',
name: 'iconName',
message: UTILS.dynamicTexts.provideIconForList(name)[0]
}).then(res => {
let iconName = res['iconName'];
if (iconName === null || iconName.length === 0) {
console.log(color(UTILS.staticTexts.creatingListNoIcon[0], UTILS.staticTexts.creatingListNoIcon[1]));
} else if (!/^[a-z0-9]{2,}(?:_[a-z0-9]{2,})*$/.test(iconName)) {
console.log(color(UTILS.staticTexts.incorrectIcon[0], UTILS.staticTexts.incorrectIcon[1]));
iconName = 'web_asset';
} else {
console.log(color(`\nCreating list with ${iconName} icon...\n`, 'YELLOW'));
}
addToModuleOrRouting(null,
`src/app/modules/${nameLowerCase}/${nameLowerCase}.routing.ts`,
null,
/ModuleRouting = \{(?:(?:.|\s)*)\}/m, 2,
`routeInfo: {\n${' '.repeat(4)}path: '/${nameLowerCase}-list',\n${' '.repeat(4)}title: '${name} list',\n${' '.repeat(4)}icon: '${iconName}',\n${' '.repeat(4)}autorizedUsers: [UserRole.ADMIN]\n${' '.repeat(2)}}`);
addToModuleOrRouting(`${name.toUpperCase()}_ROUTING`,
'src/app/@core/sidebar/sidebar-routes.config.ts',
`../../modules/${nameLowerCase}/${nameLowerCase}.routing`,
/export const SIDEBAR_ROUTES: RouteInfo\[\] \= \[(?:(?:.|\s)*)\]/m, 2,
`${name.toUpperCase()}_ROUTING.routeInfo`);
console.log(color(UTILS.staticTexts.listCreated[0], UTILS.staticTexts.listCreated[1]));
resolve(true);
});
};
});
});
}
/**
* Writes a new or overwrites old Mappable.ts model with passed primaryKey;
* @returns {boolean}
*/
function createMappableFile() {
const tsContent = TEMPLATES.mappableTemplate.replace(UTILS.REGEXS.fileTemplate, (_, match) => generateContent(match));
if (!tsContent) {
console.log(color(UTILS.staticTexts.aborting[0], UTILS.staticTexts.aborting[1]));
return false;
}
fs.writeFileSync('src/app/@core/models/Mappable.ts', tsContent, 'utf-8');
return true;
}
/**
* Generates the model's corresponding module, routing and configuration files.
* @param {string} name - Name of model.
* @returns {boolean}
*/
function createModuleFiles(name) {
const moduleContent = TEMPLATES.moduleTemplate.replace(UTILS.REGEXS.fileTemplate, (_, match) => generateContent(match, name));
const routingContent = TEMPLATES.moduleRoutingTemplate.replace(UTILS.REGEXS.fileTemplate, (_, match) => generateContent(match, name));
if (!moduleContent || !routingContent) {
console.log(color(UTILS.staticTexts.aborting[0], UTILS.staticTexts.aborting[1]));
return false;
}
fs.writeFileSync(`src/app/modules/${name.toLowerCase()}/${name.toLowerCase()}.module.ts`, moduleContent, 'utf-8');
fs.writeFileSync(`src/app/modules/${name.toLowerCase()}/${name.toLowerCase()}.routing.ts`, routingContent, 'utf-8');
fs.writeFileSync(`src/app/modules/${name.toLowerCase()}/${name.toLowerCase()}.conf.json`, TEMPLATES.configTemplate, 'utf-8');
addToModuleOrRouting(name + 'Module',
'src/app/modules/modules.module.ts',
`./${name.toLowerCase()}/${name.toLowerCase()}.module`,
/imports\: ?\[[\w\s\n\,\(\)\.]*\]/gm);
return true;
}
/**
* @param {elementTypes} elementType - Member of the elementTypes enum.
* @param {string} name - Model name.
* @param {elementTypes[]} taskList
* @returns {Promise<boolean>}
*/
function createFiles(elementType, name, taskList) {
console.log('\ncreating files for', elementType);
const nameLowerCase = name.toLowerCase();
switch(elementType) {
case UTILS.elementTypes.MODEL:
return createModelFiles(name).then( filesCreated => {
if (filesCreated) {
if (!taskList) {
return prompt({
type: 'checkbox',
name: 'tasks',
message: UTILS.dynamicTexts.whichElementsForModel(name),
choices: [
`${UTILS.elementTypes.SERVICE.toUpperCase()} => src/app/modules/${nameLowerCase}/service/${nameLowerCase
}.service.ts`,
`${UTILS.elementTypes.LIST.toUpperCase()} => src/app/modules/${nameLowerCase}/${nameLowerCase}-list`,
`${UTILS.elementTypes.DETAIL.toUpperCase()} => src/app/modules/${nameLowerCase}/${nameLowerCase}-detail`,
],
}).then( async res => {
return await parseTasks(name, res['tasks']);
});
} else {
return parseTasks(name, taskList);
}
} else {
return Promise.resolve(false);
}
});
case UTILS.elementTypes.SERVICE:
return createServiceFiles(name).then( filesCreated => filesCreated ? parseTasks(name, taskList) : Promise.resolve(false) );
case UTILS.elementTypes.LIST:
return createListFiles(name);
case UTILS.elementTypes.DETAIL:
return createDetailFiles(name);
}
}
/**
*
* @param {elementTypes} elementType - Member of the elementTypes enum.
* @param {string} [name] - Name of model.
* @param {elementTypes[]} taskList - Array containing members of elementTypes enum.
* @returns {Promise<boolean>}
*/
function create(elementType, name = null, taskList = null) {
if (!config.selectedServer) {
console.log(color(UTILS.staticTexts.notInitiated[0], UTILS.staticTexts.notInitiated[1]));
return Promise.resolve(false);
}
return new Promise(async resolve => {
const isValidName = UTILS.validateName(name);
if (isValidName) {
name = name[0].toUpperCase() + name.substr(1);
}
const { alreadyExists, moduleExists, modelAlreadyExists, serverModelAlreadyExists } = UTILS.checkAlreadyExist(elementType, name);
let errorMsg;
let createThisElementFirst;
let promptName = 'newName';
switch (true) {
case !name:
errorMsg = UTILS.staticTexts.provideName;
break;
case name.length < 3:
errorMsg = UTILS.staticTexts.nameTooShort;
break;
case !isValidName:
errorMsg = UTILS.staticTexts.nameIncorrect;
break;
case alreadyExists:
errorMsg = UTILS.dynamicTexts.nameAlreadyExists(elementType);
break;
case elementType !== UTILS.elementTypes.MODEL && !modelAlreadyExists && !serverModelAlreadyExists:
errorMsg = UTILS.dynamicTexts.nameModelNotFound(elementType, name, elementType === UTILS.elementTypes.SERVICE);
promptName = 'choise';
createThisElementFirst = UTILS.elementTypes.MODEL;
taskList = elementType === UTILS.elementTypes.SERVICE ? [elementType.toUpperCase()] : [UTILS.elementTypes.SERVICE.toUpperCase(), elementType.toUpperCase()];
break;
case elementType === UTILS.elementTypes.DETAIL || elementType === UTILS.elementTypes.LIST:
const serviceExists = UTILS.checkAlreadyExist(UTILS.elementTypes.SERVICE, name).alreadyExists;
if (!serviceExists) {
errorMsg = UTILS.dynamicTexts.nameServiceNotFound(elementType, name);
promptName = 'choise';
createThisElementFirst = UTILS.elementTypes.SERVICE;
taskList = [elementType.toUpperCase()];
}
break;
}
if (errorMsg && errorMsg.length) {
prompt({
type: 'input',
name: promptName,
message: errorMsg[0],
}).then(async res => {
if (promptName === 'newName' && res['newName'] && res['newName'].length) {
resolve(await create(elementType, res['newName'], taskList));
} else if (promptName === 'choise' && (!res["choise"] || res["choise"].toLowerCase()[0] === 'y')) {
console.log(UTILS.staticTexts.continuing[0]);
resolve(await create(createThisElementFirst, name, taskList));
} else {
console.log(color(UTILS.staticTexts.aborting[0], UTILS.staticTexts.aborting[1]));
resolve(false);
}
});
} else {
if (moduleExists) {
const filesCreated = await createFiles(elementType, name, taskList);
update(elementType, name, true);
resolve(filesCreated);
} else {
mkdirp(`src/app/modules/${name.toLowerCase()}/`, async (err) => {
if (err) {
resolve(false);
} else {
createModuleFiles(name);
const filesCreated = await createFiles(elementType, name, taskList);
update(elementType, name, true);
resolve(filesCreated);
}
});
}
}
});
}
function deleteModel(name = null) {
// TODO
return false;
}
/**
* Modifies content of a ServerModel file.
* @param {string} filePath - Path of the ServerModel file.
* @param {string} serverContent - Current content of the ServerModel file.
* @param {*[]} newPropArray - Array of objects with data for latest model properties.
* @param {string[]} [typesToImport] - Array of typescript type names found in newPropArray.
* @param {*[]} [propMap] - Modified newPropArray array with custom 'name' properties of objects.
*/
function writeNewServerContent(filePath, serverContent, newPropArray = [], typesToImport = null, propMap = null) {
const primaryKeyList = UTILS.getPrimaryKeysList(UTILS.elementTypes.SERVER_MODEL, config.primaryKey);
const propArray = primaryKeyList.concat(newPropArray);
if (propMap) {
propMap.unshift.apply(propMap, primaryKeyList);
}
const newServerContent = serverContent
.replace(UTILS.REGEXS.endOfImports, (m, p1, p2, p3) => typesToImport && typesToImport.length ? `${p1.trim()}\n\/\/ TODO verify the following imports: ${typesToImport.filter(type => RegExp(`\\b${type}\\b`).test(p1)).join(', ')};\n\n${p3.trim()}` : p1.trim() + '\n\n' + p3)
.replace(UTILS.REGEXS.modelPropDeclarations, (m, p1, p2, p3) => {
const currPropDeclarationsArray = (p3 + '\n').match(/^\s*.+;[\r\n]/gm) || [];
return p1.trim() + '\n' + propArray
.filter(prop => !prop.existsOnModelOnly)
.map( (prop, i) => {
const userPropName = propMap ? propMap[i].name : false;
const propDeclaration = ` ${userPropName || prop.serverName || prop.name}${prop.isOptional && !prop.hasOwnProperty('value') ? '?' : ''}${prop.type && !prop.hasOwnProperty('value') ? ': ' + (prop.serverType || prop.type) : ''}${prop.hasOwnProperty('value') ? ' = ' + JSON.stringify(prop.value) : ''}`;
if (!prop.isManuallyUpdated) {
return propDeclaration;
} else {
const skippedProp = currPropDeclarationsArray.filter(el => (new RegExp(`^\\s*${prop.name}[?:]`)).test(el))[0];
return skippedProp ? ' ' + skippedProp.trim().replace(/;*$/, '') : propDeclaration;
}
})
.join(';\n')
+ ';';
})
.replace(UTILS.REGEXS.serverModelMapMethods, (m, p1, p2, p3, p4, p5) => (
p1 + '\n' + primaryKeyList
.map( (prop, i) => {
return p2 === 'mapReverse'
? `${' '.repeat(8)}${p4}.${prop.mapReverseName || prop.name} = typeof ${p3}.${prop.mapReverseRelationship || prop.serverName || prop.name} !== 'undefined' ? ${p3}.${prop.mapReverseRelationship || prop.serverName || prop.name} : ${prop.hasOwnProperty('value') ? JSON.stringify(prop.value) : null}`
: `${' '.repeat(8)}${p4}.${prop.serverName || prop.name} = typeof ${p3}.${prop.relationship || prop.name} !== 'undefined' ? ${p3}.${prop.relationship || prop.name} : ${prop.hasOwnProperty('value') ? JSON.stringify(prop.value) : null}`;
})
.join(';\n')
+ ';'
))
.replace(UTILS.REGEXS.serverModelAttrGetters, (m, p1, p2, p3, p4) => {
const mappingTODOList = newPropArray.filter(el => (
p3.indexOf(`case '${el.name}':`) === -1
&& el.isManuallyUpdated
&& ((p2 === 'getMappedAttribute' && !el.existsOnModelOnly) || (p2 === 'getReverseMappedAttribute' && !el.existsOnServerOnly))
));
return p1 + (p3 && p3.length ? '\n' + ' '.repeat(12) + p3.trim() : '')
+ (mappingTODOList.length ? '\n' + mappingTODOList
.map(el => `${' '.repeat(12)}case '${el.name}':\n${' '.repeat(16)}// TODO provide logic for returning value of ${el.name};\n${' '.repeat(16)}throw new Error('Initialization logic for ${el.name} not provided!')`)
.join(';\n') + ';' : '')
+ p4
});
if (!newServerContent) {
console.log(color(UTILS.staticTexts.aborting[0], UTILS.staticTexts.aborting[1]));
return false;
}
fs.writeFileSync(filePath, newServerContent);
}
/**
* Modifies content of a Model file.
* @param {string} filePath - Path of the Model file.
* @param {string} currentContent - Current content of the Model file.
* @param {*[]} newPropArray - Array of objects with data for latest model properties.
* @param {string[]} [typesToImport = null] - Array of typescript type names found in newPropArray.
*/
function writeNewModelContent(filePath, currentContent, newPropArray = [], typesToImport = null) {
const primaryKeyList = UTILS.getPrimaryKeysList(UTILS.elementTypes.MODEL, config.primaryKey);
const propArray = primaryKeyList.concat(newPropArray);
const newContent = currentContent
.replace(UTILS.REGEXS.endOfImports, (m, p1, p2, p3) => typesToImport && typesToImport.length ? `${p1.trim()}\n\/\/ TODO verify the following imports: ${typesToImport.filter(type => RegExp(`\\b${type}\\b`).test(p1)).join(', ')};\n\n${p3.replace(/^\s+/m, '')}` : p1.trim() + '\n\n' + p3)
.replace(UTILS.REGEXS.modelPropDeclarations, (m, p1, p2, p3) => {
const currPropDeclarationsArray = (p3 + '\n').match(/^\s*.+;[\r\n]/gm) || [];
return p1.trim() + '\n' + propArray
.filter(prop => !prop.existsOnServerOnly)
.map( prop => {
const propDeclaration = ` ${prop.name}${prop.isOptional && !prop.hasOwnProperty('value') ? '?' : ''}${prop.type && !prop.hasOwnProperty('value') ? ': ' + prop.type : ''}${prop.hasOwnProperty('value') ? ' = ' + JSON.stringify(prop.value) : ''}`;
if (!prop.isManuallyUpdated) {
return propDeclaration;
} else {
const skippedProp = currPropDeclarationsArray.filter(el => (new RegExp(`^\\s*${prop.name}[?:]`)).test(el))[0];
return skippedProp ? ' ' + skippedProp.trim().replace(/;*$/, '') : propDeclaration;
}
})
.join(';\n') + ';';
})
.replace(UTILS.REGEXS.modelConstructor, (_, p1, p2, p3, p4, p5, p6) => {
const currConstructorArgs = (p3 + ',\n').match(/^\s*.+,[\r\n]/gm) || [];
const currConstructorDeclarations = (p6 + '\n').match(/^\s*.+;[\r\n]/gm) || [];
return p1 + propArray
.map( prop => {
if (typeof prop.relationship === 'string' || prop.existsOnServerOnly) {
return '';
} else if (!prop.isManuallyUpdated) {
return `${prop.name}?${prop.type && !prop.hasOwnProperty('value') ? ': ' + prop.type : ''}`;
} else {
const skippedProp = currConstructorArgs.filter(el => (new RegExp(`^\\s*${prop.name}[?:]`)).test(el))[0];
return skippedProp ? skippedProp.trim().replace(/,*$/, '') : '';
}
})
.filter(newPropDeclaration => newPropDeclaration.length)
.join(',\n' + ' '.repeat(14))
+ p4 + propArray
.filter(prop => !prop.existsOnServerOnly)
.map( prop => {
const propDeclaration = `this.${prop.name} = typeof ${prop.relationship || prop.name} !== 'undefined' ? ${prop.relationship || prop.name} : ${prop.hasOwnProperty('value') ? JSON.stringify(prop.value) : 'null'}`;
if (!prop.isManuallyUpdated) {
return propDeclaration;
} else {
const skippedProp = currConstructorDeclarations.filter(el => (new RegExp(`^\\s*this.${prop.name}`)).test(el))[0];
return skippedProp ? skippedProp.trim().replace(/;*$/, '') : `this.${prop.name} = null`;
}
})
.join(';\n' + ' '.repeat(4)) + ';';
});
if (!newContent) {
console.log(color(UTILS.staticTexts.aborting[0], UTILS.staticTexts.aborting[1]));
return false;
}
fs.writeFileSync(filePath, newContent);
}
/**
* Modifies the content of the Model's data factory file.
* @param {string} filePath - Path of the Model file.
* @param {string} currentContent - Current content of the Model file.
* @param {*[]} newPropArray - Array of objects with data for latest model properties.
*/
function writeNewDataFactoryContent(filePath, currentContent, newPropArray = []) {
const typesToImport = UTILS.getTypesToImport(newPropArray, '', /^\b[A-Z]\w*\b/);
const primaryKeyList = UTILS.getPrimaryKeysList(UTILS.elementTypes.DATA_FACTORY, config.primaryKey);
const propArray = primaryKeyList.concat(newPropArray);
const newContent = currentContent
.replace(UTILS.REGEXS.endOfImports, (m, p1, p2, p3) => typesToImport && typesToImport.length ? `${p1.replace(/\s+$/m, '')}\n\/\/ TODO verify the following imports: ${typesToImport.filter(type => RegExp(`\\b${type}\\b`).test(p1)).join(', ')};\n\n${p3.replace(/^\s+/m, '')}` : p1.trim() + '\n\n' + p3)
.replace(UTILS.REGEXS.formControlList, (_, p1, p2, p3) => propArray && propArray.length ? (
p1 + '\n' + ' '.repeat(6)
+ propArray.map( prop => '{' + Object.keys(prop).map( k => `${k}: ${prop[k]}` ).join(', ') + '}' )
.join(',\n' + ' '.repeat(6))
+ '\n' + ' '.repeat(4) + p3
): p1 + p3);
if (!newContent) {
console.log(color(UTILS.staticTexts.aborting[0], UTILS.staticTexts.aborting[1]));
return false;
}
fs.writeFileSync(filePath, newContent);
}
/**
* Implements the logic related to updating a Model file.
* @param {string} name - Model name.
* @param {{properties: any[], any}} moduleConfig
* @param {boolean} [runSilent = false] - Run update without command line output.
* @returns {Promise<boolean>}
*/
function updateModel(name, moduleConfig, runSilent = false) {
return new Promise(resolve => {
if (!runSilent) {
console.log(color(UTILS.dynamicTexts.updating(UTILS.elementTypes.MODEL, name)[0], UTILS.dynamicTexts.updating(UTILS.elementTypes.MODEL, name)[1]));
}
const {modelPath, serverModelPath} = UTILS.getModulePaths(name, true);
fs.readFile(modelPath, 'utf8', (err, currentContent) => {
if (err) {
return resolve(false)
}
const typesToImport = UTILS.getTypesToImport(moduleConfig['properties'], 'type');
const serverTypesToImport = UTILS.getTypesToImport(moduleConfig['properties'], 'serverType');
const successText = UTILS.dynamicTexts.updateSuccess(name, typesToImport);
writeNewModelContent(modelPath, currentContent, moduleConfig['properties'], typesToImport);
fs.readFile(serverModelPath, 'utf8', (err1, serverContent) => {
if (err1) {
return resolve(false);
}
if (runSilent) {
writeNewServerContent(serverModelPath, serverContent, moduleConfig['properties'], serverTypesToImport);
return resolve(true);
}
if (moduleConfig['properties'].length === 0) {
writeNewServerContent(serverModelPath, serverContent, []);
console.log(color(successText[0], successText[1]));
return resolve(true);
}
writeNewServerContent(serverModelPath, serverContent, moduleConfig['properties'], serverTypesToImport);
console.log(color(successText[0], successText[1]));
return resolve(true);
});
});
});
}
/**
* Implements the logic related to adding new properties to the model's detail.
* @param {string} name - Model name.
* @param {{properties: any[], any}} moduleConfig
* @param {boolean} [runSilent = false] - Run update without command line output.
* @returns {Promise<boolean>}
*/
function updateDetail(name, moduleConfig, runSilent = false) {
return new Promise(resolve => {
if (!runSilent) {
console.log(color(UTILS.dynamicTexts.updating(name, UTILS.elementTypes.DETAIL)[0], UTILS.dynamicTexts.updating(name, UTILS.elementTypes.DETAIL)[1]));
}
const {dataFactoryPath} = UTILS.getModulePaths(name, true);
const formControlList = moduleConfig['properties']
.filter( el => el.hasOwnProperty('htmlConfig') && !el.isManuallyUpdated )
.map( el => {
let formControl = {name: `'${el.name}'`, type: 'FormControlType.TEXT', ...el.htmlConfig};
formControl.primaryKey = el.primaryKey;
return formControl;
});
fs.readFile(dataFactoryPath, 'utf8', (err, dataFactoryContent) => {
if (err) {
return resolve(false)
}
writeNewDataFactoryContent(dataFactoryPath, dataFactoryContent, formControlList);
if (!runSilent) {
console.log(color(UTILS.staticTexts.detailUpdated[0], UTILS.staticTexts.detailUpdated[1]));
}
return resolve(true)
});
});
}
/**
* Handles the initial logic related to updating elements.
* @param {elementTypes} elementType - Member of the elementTypes enum.
* @param {string} name - Model name.
* @param {boolean} [runSilent = false] - Run update without command line output.
* @returns {boolean}
*/
async function update(elementType, name, runSilent = false) {
const {modelAlreadyExists, serverModelAlreadyExists, detailAlreadyExist} = UTILS.checkAlreadyExist(null, name);
if (UTILS.nonUpdateableModels.includes(name.toLowerCase())) {
console.log(color(UTILS.dynamicTexts.nonUpdateableModel(name)[0], UTILS.dynamicTexts.nonUpdateableModel(name)[1]));
return false;
}
if (!modelAlreadyExists) {
console.log(color(UTILS.dynamicTexts.updateModelNotFound(name)[0], UTILS.dynamicTexts.updateModelNotFound(name)[1]));
return false;
}
if (!serverModelAlreadyExists) {
console.log(color(UTILS.dynamicTexts.updateServerModelNotFound(name)[0], UTILS.dynamicTexts.updateServerModelNotFound(name)[1]));
return false;
}
const moduleConfig = require(process.cwd() + '/' + UTILS.getModulePaths(name).config);
const moduleProperties = moduleConfig['properties'];
if (moduleProperties && moduleProperties.length > 1) {
const duplicateNamesList = UTILS.getDuplicateValuesByPropName(moduleProperties, 'name');
if (duplicateNamesList && duplicateNamesList.length) {
console.log(color(UTILS.dynamicTexts.duplicatePropNamesFound('name', duplicateNamesList)[0], UTILS.dynamicTexts.duplicatePropNamesFound('name', duplicateNamesList)[1]));
return false;
}
}
name = name[0].toUpperCase() + name.substr(1);
switch (elementType) {
case UTILS.elementTypes.MODEL:
const modelUpdated = await updateModel(name, moduleConfig, runSilent);
if (modelUpdated && detailAlreadyExist) {
update(UTILS.elementTypes.DETAIL, name, runSilent);
} else {
return;
}
break;
case UTILS.elementTypes.DETAIL:
if (!detailAlreadyExist) {
console.log(color(UTILS.dynamicTexts.detailNotFound(name)[0], UTILS.dynamicTexts.detailNotFound(name)[1]));
return;
}
return await updateDetail(name, moduleConfig, runSilent);
default:
return;
}
}
async function cloneRepo() {
return new Promise(async resolve => {
console.log(color(UTILS.staticTexts.cloning[0], UTILS.staticTexts.cloning[1]));
function onError(e) {
console.log(e);
resolve(false);
}
fsExtra.copy(`${__dirname}/resources/templates/project_template`, './', err => {
if (err) {
onError(err)
}
console.log(color(UTILS.staticTexts.cloned[0], UTILS.staticTexts.cloned[1]));
resolve(true);
})
});
}
async function configure() {
return new Promise(async resolve => {
const fileToCheck = 'src/environments/environment.ts';
if (!fs.existsSync(fileToCheck)) {
const serverTypePrompt = {
type: 'list',
name: 'serverType',
message: UTILS.staticTexts.selectServer[0],
choices: ['strapi', 'firestore']
};
const primaryKeyPrompt = {
type: 'input',
name: 'primaryKey',
transformer: val => val.replace(/[^\w]/g, ''),
message: '[optional] Provide custom primary key (min. 2 characters) which will be used for all models or leave empty to use default key:'
};
prompt([serverTypePrompt, primaryKeyPrompt]).then(async (res) => {
console.log(color(UTILS.dynamicTexts.configuring(res['serverType'])[0], UTILS.dynamicTexts.configuring(res['serverType'])[1]));
config.selectedServer = res["serverType"];
if (res['primaryKey'] && res['primaryKey'].replace(/[^\w]/g, '').length >= 2) {
config.primaryKey = res['primaryKey'].replace(/[^\w]/g, '');
} else {
console.log(color(UTILS.staticTexts.invalidPrimaryKey[0], UTILS.staticTexts.invalidPrimaryKey[1]));
config.primaryKey = 'id';
}
const newConfigContent = JSON.stringify(config, null, 4);
fs.writeFileSync('./winkit.conf.json', newConfigContent);
fs.writeFileSync('./.winkitrc', `v${version}`);
createMappableFile();
Promise.all([
new Promise((resolve1, reject1) => {
gulp.src(['implementableServers/' + res["serverType"] + '/services/**/*'])
.pipe(gulp.dest('src/app/@core/services/'))
.on('error', reject1)
.on('end', resolve1);
}),
new Promise((resolve2, reject2) => {
gulp.src(['implementableServers/' + res["serverType"] + '/environments/**/*'])
.pipe(gulp.dest('src/environments/'))
.on('error', reject2)
.on('end', resolve2);
}),
new Promise((resolve3, reject3) => {
gulp.src(['implementableServers/' + res["serverType"] + '/user/**/*'])
.pipe(gulp.dest('src/app/modules/user/service/'))
.on('error', reject3)
.on('end', resolve3);
}),
new Promise((resolve4, reject4) => {
gulp.src(['implementableServers/' + res["serverType"] + '/media-manager/**/*'])
.pipe(gulp.dest('src/app/shared/components/media-manager/'))
.on('error', reject4)
.on('end', resolve4);
})
]).then(() => {
del.sync(['implementableServers/**']);
update(UTILS.elementTypes.MODEL, 'user', true);
console.log(color(UTILS.staticTexts.configured[0], UTILS.staticTexts.configured[1]));
console.log(color(UTILS.staticTexts.enjoy[0], UTILS.staticTexts.enjoy[1]));
gulp.src('./package.json', {read: false})
.pipe(shell([
'npm run firstRun'
]))
.on('end', () => {
resolve(true);
})
.on('error', () => {
resolve(false);
});
});
});
} else {
console.log(color(UTILS.staticTexts.alreadyInitialized[0], UTILS.staticTexts.alreadyInitialized[1]));
resolve(false);
}
});
}
async function init(projectName) {
return new Promise(async (resolve, reject) => {
const fileToCheck = projectName + '/src/main.ts';
if (!fs.existsSync(fileToCheck)) {
mkdirp(projectName, async (err) => {
if (err) {
resolve(false);
} else {
process.chdir(`${projectName}/`);
const res = await cloneRepo();
if (res) {
resolve(await configure());
} else {
resolve(false);
}
}
});
} else {
console.log(color(UTILS.staticTexts.alreadyCloned[0], UTILS.staticTexts.alreadyCloned[1]));
resolve(await configure());
}
});
}
gulp.task('init', async () => {
return await init();
});
gulp.task('create:model', async () => {
return await create(UTILS.elementTypes.MODEL, null);
});
gulp.task('update:model', async () => {
return await update(UTILS.elementTypes.MODEL, null);
});
gulp.task('create:service', async () => {
return await create(UTILS.elementTypes.SERVICE, null);
});
gulp.task('create:detail', async () => {
return await create(UTILS.elementTypes.DETAIL, null);
});
gulp.task('create:list', async () => {
return await create(UTILS.elementTypes.LIST, null);
});
gulp.task('delete:model', async () => {
return await deleteModel(null);
});
// Export all methods
module.exports = {
init: init,
createModel: (name) => create(UTILS.elementTypes.MODEL, name),
createService: (name) => create(UTILS.elementTypes.SERVICE, name),
createList: (name) => create(UTILS.elementTypes.LIST, name),
createDetail: (name) => create(UTILS.elementTypes.DETAIL, name),
updateModel: (name) => update(UTILS.elementTypes.MODEL, name),
updateDetail: (name) => update(UTILS.elementTypes.DETAIL, name),
deleteModel: (name) => deleteModel(name)
};