@@ -3,39 +3,39 @@ import path from 'path';
33import commander from 'commander' ;
44import capitalize from '../utils/capitalize' ;
55import replaceMask from '../utils/replaceMask' ;
6+ import Log from '../Log' ;
67
7- let validatorName : string = '' ;
8+ const createValidator = ( name : string ) => {
9+ if ( name ) {
10+ Log . Instance . infoHeap ( `Creating the validator` ) ;
811
9- commander
10- . name ( `recife-cli validator` )
11- . arguments ( '<validator-name>' )
12- . action ( name => ( validatorName = name ) )
13- . allowUnknownOption ( false )
14- . parse ( process . argv ) ;
15-
16- if ( validatorName ) {
17- validatorName = capitalize ( validatorName . replace ( / V a l i d a t o r | \. t s | \. j s / g, '' ) ) ;
12+ name = capitalize ( name . replace ( / V a l i d a t o r | \. t s | \. j s / g, '' ) ) ;
1813
19- validatorName += 'Validator' ;
14+ name += 'Validator' ;
2015
21- const source = path . join ( __dirname , '/../../templates/validator/template' ) ;
22- const target = path . join (
23- process . cwd ( ) ,
24- 'src/validators' ,
25- `${ validatorName } .ts`
26- ) ;
16+ const source = path . join ( __dirname , '/../../templates/validator/template' ) ;
17+ const target = path . join ( process . cwd ( ) , 'src/validators' , `${ name } .ts` ) ;
2718
28- try {
29- const contentFile = fs . readFileSync ( source ) . toString ( ) ;
30- fs . writeFileSync ( target , replaceMask ( contentFile , { name : validatorName } ) ) ;
19+ try {
20+ const contentFile = fs . readFileSync ( source ) . toString ( ) ;
21+ fs . writeFileSync ( target , replaceMask ( contentFile , { name } ) ) ;
3122
32- console . info ( `\x1b[36mCreating the validator ${ validatorName } .` , '\x1b[0m' ) ;
33- console . info ( `Path: ${ target } ` , '\x1b[0m\n' ) ;
34- } catch ( err ) {
35- console . log ( `\x1b[31m${ err } \x1b[0m` ) ;
23+ Log . Instance . successHeap ( `The validator ${ name } created.` ) ;
24+ Log . Instance . info ( `Path: ${ target } \n\n` ) ;
25+ } catch ( err ) {
26+ Log . Instance . exception ( err ) ;
27+ }
28+ } else {
29+ Log . Instance . errorHeap ( `Specify the name validator.` ) ;
30+ Log . Instance . info (
31+ `For example: recife-cli validator User\nRun --help for more information`
32+ ) ;
3633 }
37- } else {
38- console . error ( '\x1b[31mSpecify the name validator.' , '\x1b[0m' ) ;
39- console . log ( ` For example: recife-cli validator User` ) ;
40- console . log ( ` Run --help for more information\n` ) ;
41- }
34+ } ;
35+
36+ commander
37+ . name ( `recife-cli validator` )
38+ . arguments ( '<validator-name>' )
39+ . action ( name => createValidator ( name ) )
40+ . allowUnknownOption ( false )
41+ . parse ( process . argv ) ;
0 commit comments