@@ -62,7 +62,7 @@ export class MakeCrudCommand extends BaseCommand {
62
62
)
63
63
64
64
if ( addId ) {
65
- this . properties . push ( { name : 'id' , type : 'increments' } )
65
+ this . properties . push ( { name : 'id' , type : 'increments' , custom : false } )
66
66
}
67
67
68
68
const addTimestamps = await this . prompt . confirm (
@@ -106,7 +106,7 @@ export class MakeCrudCommand extends BaseCommand {
106
106
107
107
options . forEach ( option => ( optionsObj [ option ] = true ) )
108
108
109
- this . properties . push ( { name, type, ...optionsObj } )
109
+ this . properties . push ( { name, type, custom : true , ...optionsObj } )
110
110
111
111
addMoreProps = await this . prompt . confirm (
112
112
'Do you want to add more properties?'
@@ -117,12 +117,14 @@ export class MakeCrudCommand extends BaseCommand {
117
117
this . properties . push ( {
118
118
name : 'createdAt' ,
119
119
type : 'Date' ,
120
+ custom : false ,
120
121
isCreateDate : true
121
122
} )
122
123
123
124
this . properties . push ( {
124
125
name : 'updatedAt' ,
125
126
type : 'Date' ,
127
+ custom : false ,
126
128
isUpdateDate : true
127
129
} )
128
130
}
@@ -131,10 +133,12 @@ export class MakeCrudCommand extends BaseCommand {
131
133
this . properties . push ( {
132
134
name : 'deletedAt' ,
133
135
type : 'Date' ,
136
+ custom : false ,
134
137
isDeleteDate : true
135
138
} )
136
139
}
137
140
141
+ console . log ( )
138
142
const task = this . logger . task ( )
139
143
140
144
if ( Config . get ( 'rc.commands.make:crud.model.enabled' , true ) ) {
@@ -156,8 +160,21 @@ export class MakeCrudCommand extends BaseCommand {
156
160
task . addPromise ( 'Creating service' , ( ) => this . makeService ( ) )
157
161
}
158
162
163
+ if ( Config . get ( 'rc.commands.make:crud.controller-test.enabled' , true ) ) {
164
+ task . addPromise ( 'Creating e2e tests for controller' , ( ) =>
165
+ this . makeControllerTest ( )
166
+ )
167
+ }
168
+
169
+ if ( Config . get ( 'rc.commands.make:crud.service-test.enabled' , true ) ) {
170
+ task . addPromise ( 'Creating unitary tests for service' , ( ) =>
171
+ this . makeServiceTest ( )
172
+ )
173
+ }
174
+
159
175
await task . run ( )
160
176
177
+ console . log ( )
161
178
this . logger . success ( `CRUD ({yellow} "${ this . name } ") successfully created.` )
162
179
}
163
180
@@ -170,6 +187,7 @@ export class MakeCrudCommand extends BaseCommand {
170
187
)
171
188
172
189
let properties = ''
190
+ let definitions = ''
173
191
174
192
this . properties . forEach ( ( p , i ) => {
175
193
const property = Json . copy ( p )
@@ -224,16 +242,28 @@ export class MakeCrudCommand extends BaseCommand {
224
242
properties += ` @Column()\n public ${ property . name } : ${ property . type } `
225
243
}
226
244
245
+ const type = {
246
+ string : 'this.faker.string.sample()' ,
247
+ number : 'this.faker.number.int({ max: 10000000 })' ,
248
+ boolean : 'this.faker.datatype.boolean()' ,
249
+ Date : 'this.faker.date.anytime()'
250
+ }
251
+
252
+ if ( property . custom ) {
253
+ definitions += ` ${ property . name } : ${ type [ property . type ] } `
254
+ }
255
+
227
256
if ( this . properties . length - 1 !== i ) {
228
257
properties += '\n\n'
258
+ if ( definitions . length ) definitions += ',\n'
229
259
}
230
260
} )
231
261
232
262
await this . generator
233
263
. fileName ( this . toCase ( this . name ) )
234
264
. destination ( destination )
235
265
. template ( 'crud-model' )
236
- . properties ( { properties } )
266
+ . properties ( { properties, definitions } )
237
267
. setNameProperties ( true )
238
268
. make ( )
239
269
@@ -332,10 +362,19 @@ export class MakeCrudCommand extends BaseCommand {
332
362
Path . services ( )
333
363
)
334
364
365
+ let properties = ''
366
+
367
+ this . properties
368
+ . filter ( p => p . custom )
369
+ . forEach ( p => {
370
+ properties += `'${ p . name } ', `
371
+ } )
372
+
335
373
this . generator
336
374
. fileName ( this . toCase ( `${ this . name } Controller` ) )
337
375
. destination ( destination )
338
376
. properties ( {
377
+ properties : properties . slice ( 0 , properties . length - 2 ) ,
339
378
serviceImportPath : new Generator ( )
340
379
. fileName ( this . toCase ( `${ this . name } Service` ) )
341
380
. destination ( serviceDest )
@@ -366,10 +405,19 @@ export class MakeCrudCommand extends BaseCommand {
366
405
Path . models ( )
367
406
)
368
407
408
+ let propertiesToUpdate = ''
409
+
410
+ this . properties
411
+ . filter ( p => p . custom )
412
+ . forEach ( property => {
413
+ propertiesToUpdate += ` ${ this . nameLower } .${ property . name } = body.${ property . name } \n`
414
+ } )
415
+
369
416
await this . generator
370
417
. fileName ( this . toCase ( `${ this . name } Service` ) )
371
418
. destination ( destination )
372
419
. properties ( {
420
+ propertiesToUpdate,
373
421
idType : this . isMongo ? 'string' : 'number' ,
374
422
modelImportPath : new Generator ( )
375
423
. fileName ( this . toCase ( this . name ) )
@@ -386,4 +434,128 @@ export class MakeCrudCommand extends BaseCommand {
386
434
387
435
await this . rc . pushTo ( 'services' , importPath ) . save ( )
388
436
}
437
+
438
+ public async makeControllerTest ( ) {
439
+ this . cleanGenerator ( )
440
+
441
+ const destination = Config . get (
442
+ 'rc.commands.make:crud.controller-test.destination' ,
443
+ Path . tests ( 'e2e' )
444
+ )
445
+
446
+ const modelDest = Config . get (
447
+ 'rc.commands.make:crud.model.destination' ,
448
+ Path . models ( )
449
+ )
450
+
451
+ let createBody = ''
452
+ let updateBody = ''
453
+ let showAssertBody = `id: ${ this . nameLower } .id, `
454
+ let createAssertBody = ''
455
+ let updateAssertBody = `id: ${ this . nameLower } .id, `
456
+
457
+ this . properties
458
+ . filter ( p => p . custom )
459
+ . forEach ( property => {
460
+ const type = {
461
+ string : `'string'` ,
462
+ number : 1 ,
463
+ boolean : true ,
464
+ Date : 'new Date()'
465
+ }
466
+
467
+ createBody += `${ property . name } : ${ type [ property . type ] } , `
468
+ updateBody += `${ property . name } : ${ type [ property . type ] } , `
469
+ showAssertBody += `${ property . name } : ${ type [ property . type ] } , `
470
+ createAssertBody += `'${ property . name } ', `
471
+ updateAssertBody += `${ property . name } : ${ type [ property . type ] } , `
472
+ } )
473
+
474
+ await this . generator
475
+ . fileName ( this . toCase ( `${ this . name } ControllerTest` ) )
476
+ . destination ( destination )
477
+ . properties ( {
478
+ createBody : createBody . slice ( 0 , createBody . length - 2 ) ,
479
+ updateBody : updateBody . slice ( 0 , updateBody . length - 2 ) ,
480
+ showAssertBody : showAssertBody . slice ( 0 , showAssertBody . length - 2 ) ,
481
+ createAssertBody : createAssertBody . slice (
482
+ 0 ,
483
+ createAssertBody . length - 2
484
+ ) ,
485
+ updateAssertBody : updateAssertBody . slice (
486
+ 0 ,
487
+ updateAssertBody . length - 2
488
+ ) ,
489
+ modelImportPath : new Generator ( )
490
+ . fileName ( this . toCase ( this . name ) )
491
+ . destination ( modelDest )
492
+ . getImportPath ( ) ,
493
+ crudNamePascal : this . namePascal ,
494
+ crudNamePascalPlural : String . pluralize ( this . namePascal ) ,
495
+ crudNameLower : this . nameLower ,
496
+ crudNameLowerPlural : String . pluralize ( this . nameLower )
497
+ } )
498
+ . template ( 'crud-controller-test' )
499
+ . setNameProperties ( true )
500
+ . make ( )
501
+ }
502
+
503
+ public async makeServiceTest ( ) {
504
+ this . cleanGenerator ( )
505
+
506
+ const destination = Config . get (
507
+ 'rc.commands.make:crud.service-test.destination' ,
508
+ Path . tests ( 'unit' )
509
+ )
510
+
511
+ const modelDest = Config . get (
512
+ 'rc.commands.make:crud.model.destination' ,
513
+ Path . models ( )
514
+ )
515
+
516
+ const serviceDest = Config . get (
517
+ 'rc.commands.make:crud.service.destination' ,
518
+ Path . services ( )
519
+ )
520
+
521
+ let createBody = ''
522
+ let updateBody = ''
523
+
524
+ this . properties
525
+ . filter ( p => p . custom )
526
+ . forEach ( property => {
527
+ const type = {
528
+ string : `'string'` ,
529
+ number : 1 ,
530
+ boolean : true ,
531
+ Date : 'new Date()'
532
+ }
533
+
534
+ createBody += `${ property . name } : ${ type [ property . type ] } , `
535
+ updateBody += `${ property . name } : ${ type [ property . type ] } , `
536
+ } )
537
+
538
+ await this . generator
539
+ . fileName ( this . toCase ( `${ this . name } ServiceTest` ) )
540
+ . destination ( destination )
541
+ . properties ( {
542
+ createBody : createBody . slice ( 0 , createBody . length - 2 ) ,
543
+ updateBody : updateBody . slice ( 0 , updateBody . length - 2 ) ,
544
+ modelImportPath : new Generator ( )
545
+ . fileName ( this . toCase ( this . name ) )
546
+ . destination ( modelDest )
547
+ . getImportPath ( ) ,
548
+ serviceImportPath : new Generator ( )
549
+ . fileName ( this . toCase ( `${ this . name } Service` ) )
550
+ . destination ( serviceDest )
551
+ . getImportPath ( ) ,
552
+ crudNamePascal : this . namePascal ,
553
+ crudNamePascalPlural : String . pluralize ( this . namePascal ) ,
554
+ crudNameLower : this . nameLower ,
555
+ crudNameLowerPlural : String . pluralize ( this . nameLower )
556
+ } )
557
+ . template ( 'crud-service-test' )
558
+ . setNameProperties ( true )
559
+ . make ( )
560
+ }
389
561
}
0 commit comments