22
33const path = require ( 'path' ) ;
44const fastifyGenTs = require ( './fastify-generator' ) ;
5- const { start, get } = require ( 'prompt' ) ;
5+ const { prompt } = require ( 'prompts' ) ;
6+ const { say } = require ( 'cfonts' ) ;
67
78const options = [
8- {
9- description : " Fastify Plugin Structure with Mongoose and Swagger" ,
10- defaultName : " fastify-plugin-mongoose-swagger" ,
11- templateName : " plugin-structure-mongoose" ,
12- dependencies : " dotenv fastify fastify-cors fastify-oas mongoose" ,
13- devDependencies : " @types/cors @types/jest @types/node jest nodemon ts-jest ts-node typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint"
14- } , {
15- description : " Fastify Plugin Structure with TypeORM and Swagger" ,
16- defaultName : " fastify-plugin-typeorm-swagger" ,
17- templateName : " plugin-structure-typeorm" ,
18- dependencies : " dotenv fastify fastify-cors fastify-oas fastify-plugin mongodb reflect-metadata typeorm" ,
19- devDependencies : " @types/cors @types/jest @types/node jest nodemon ts-jest ts-node typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint"
20- } , {
21- description : " Fastify with Mongoose and Express Directory Structure" ,
22- defaultName : " fastify-express-mongoose" ,
23- templateName : " express-structure-mongoose" ,
24- dependencies : " dotenv fastify fastify-cors mongoose" ,
25- devDependencies : " @types/cors @types/jest @types/node jest nodemon ts-jest ts-node typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint"
26- } , {
27- description : " Fastify with TypeORM and Express Directory Structure" ,
28- defaultName : " fastify-express-typeorm" ,
29- templateName : " express-structure-typeorm" ,
30- dependencies : " dotenv fastify fastify-cors fastify-plugin mongodb reflect-metadata typeorm" ,
31- devDependencies : " @types/cors @types/jest @types/node jest nodemon ts-jest ts-node typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint"
32- } ,
9+ {
10+ description : ' Fastify Plugin Structure with Mongoose and Swagger' ,
11+ defaultName : ' fastify-plugin-mongoose-swagger' ,
12+ templateName : ' plugin-structure-mongoose' ,
13+ dependencies : ' dotenv fastify fastify-cors fastify-oas mongoose' ,
14+ devDependencies : ' @types/cors @types/jest @types/node jest nodemon ts-jest ts-node typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint'
15+ } , {
16+ description : ' Fastify Plugin Structure with TypeORM and Swagger' ,
17+ defaultName : ' fastify-plugin-typeorm-swagger' ,
18+ templateName : ' plugin-structure-typeorm' ,
19+ dependencies : ' dotenv fastify fastify-cors fastify-oas fastify-plugin mongodb reflect-metadata typeorm' ,
20+ devDependencies : ' @types/cors @types/jest @types/node jest nodemon ts-jest ts-node typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint'
21+ } , {
22+ description : ' Fastify with Mongoose and Express Directory Structure' ,
23+ defaultName : ' fastify-express-mongoose' ,
24+ templateName : ' express-structure-mongoose' ,
25+ dependencies : ' dotenv fastify fastify-cors mongoose' ,
26+ devDependencies : ' @types/cors @types/jest @types/node jest nodemon ts-jest ts-node typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint'
27+ } , {
28+ description : ' Fastify with TypeORM and Express Directory Structure' ,
29+ defaultName : ' fastify-express-typeorm' ,
30+ templateName : ' express-structure-typeorm' ,
31+ dependencies : ' dotenv fastify fastify-cors fastify-plugin mongodb reflect-metadata typeorm' ,
32+ devDependencies : ' @types/cors @types/jest @types/node jest nodemon ts-jest ts-node typescript @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint'
33+ } ,
3334] ;
34- const schema = {
35- properties : {
36- option : {
37- description : 'Select a options for the type of project' ,
38- pattern : / ^ [ 1 - 4 ] { 0 , 1 } $ / ,
39- message : 'Specify a number in range of 4 corresponding options' ,
40- required : true
41- } ,
42- path : {
43- description : '[OPTIONAL] Specify directory where create the files, if no path is provided, current directory will be used' ,
44- message : '[OPTIONAL] Specify directory where create the files, if no path is provided, current directory will be used' ,
45- default : './' ,
46- required : false
47- } ,
48- projectName : {
49- description : '[OPTIONAL] Specify project name' ,
50- pattern : / ^ ( @ [ a - z 0 - 9 - ~ ] [ a - z 0 - 9 - ._ ~ ] * \/ ) ? [ a - z 0 - 9 - ~ ] [ a - z 0 - 9 - ._ ~ ] * $ / ,
51- message : '[OPTIONAL] Specify project name' ,
52- required : false
53- }
54- }
55- } ;
5635
5736
5837const getDest = ( option , destFolder , projectName ) => {
59- return `${ path . join ( process . cwd ( ) , destFolder ) } /${ projectName || option . defaultName } ` ;
60- }
38+ return `${ path . join ( process . cwd ( ) , destFolder ) } /${ projectName || option . defaultName } ` ;
39+ } ;
6140
6241
6342const initiate = async ( option , path , projectName ) => {
64- try {
65- const destination = getDest ( option , path , projectName ) ;
66- console . log ( `Choose the number for corresponding option.\n${ destination } ` ) ;
67- await fastifyGenTs ( option , destination ) ;
68- console . log ( 'Project setup complete!' ) ;
69- } catch ( error ) {
70- console . error ( error ) ;
71- }
43+ try {
44+ const destination = getDest ( option , path , projectName ) ;
45+ await fastifyGenTs ( option , destination ) ;
46+ console . log ( '\x1b[32m\x1b[40m%s\x1b[0m' , `Project setup complete! at ${ destination } ` ) ;
47+ } catch ( error ) {
48+ console . error ( error ) ;
49+ }
7250} ;
7351
7452
53+ say ( 'fastify typescript generator' , {
54+ font : 'chrome' ,
55+ align : 'center' ,
56+ colors : [ 'system' ] ,
57+ background : 'transparent' ,
58+ letterSpacing : 1 ,
59+ lineHeight : 1 ,
60+ space : true ,
61+ } ) ;
62+
63+
64+ say ( 'open-devs' , {
65+ font : 'chrome' ,
66+ align : 'right' ,
67+ colors : [ 'system' ] ,
68+ background : 'transparent' ,
69+ letterSpacing : 1 ,
70+ lineHeight : 1 ,
71+ space : true ,
72+ gradient : [ '#fff' , '#89d8d3' ] ,
73+ } ) ;
74+
75+
7576console . log ( '\x1b[33m\x1b[40m%s\x1b[0m' , 'Setting up new Fastify TypeScript project...\n' ) ;
76- console . log ( '\x1b[33m\x1b[40m%s\x1b[0m' , `Available options:\n` ) ;
77- options . map ( ( el , index ) => console . log ( `${ index + 1 } . ${ el . description } -> ${ el . templateName } \n` ) ) ;
7877
7978
80- start ( ) ;
79+ const onCancel = ( ) => {
80+ console . log ( '\x1b[31m%s\x1b[0m' , 'Project creation failed!' ) ;
81+ } ;
82+
83+
84+ ( async ( ) => {
85+ const questions = [
86+ {
87+ type : 'select' ,
88+ name : 'option' ,
89+ message : 'Select a option for the type of project' ,
90+ validate : value => value >= 1 && value <= 4 ? true : 'Specify number in the range of 1 - 4' ,
91+ suggest : ( input , choices ) => choices . filter ( i => i . value ) ,
92+ choices : options . map ( ( el , index ) => ( { value : index , title : el . templateName , description : el . description } ) ) ,
93+ fallback : {
94+ title : 'Using default' ,
95+ value : 1
96+ }
97+ } ,
98+ {
99+ type : 'text' ,
100+ name : 'projectName' ,
101+ message : 'Specify project name' ,
102+ validate : projectName => / ^ ( @ [ a - z 0 - 9 - ~ ] [ a - z 0 - 9 - ._ ~ ] * \/ ) ? [ a - z 0 - 9 - ~ ] [ a - z 0 - 9 - ._ ~ ] * $ / . test ( projectName ) ? true : 'Invalid project name, please follow npm guidelines'
103+ } ,
104+ {
105+ type : 'text' ,
106+ name : 'path' ,
107+ message : '[OPTIONAL] Specify directory where you want to create the project, if no path is provided current directory will be used.' ,
108+ fallback : {
109+ title : 'Using default' ,
110+ value : './'
111+ }
112+ }
113+ ] ;
114+
115+ const answers = await prompt ( questions , { onCancel } ) ;
116+
117+ if ( answers . option , answers . path , answers . projectName ) {
118+ console . log ( '\x1b[33m\x1b[40m%s\x1b[0m' , 'Starting Project creation...' ) ;
119+ initiate ( options [ answers . option ] , answers . path , answers . projectName ) ;
120+ }
81121
82122
83- get ( schema , ( error , argv ) => {
84- if ( error ) {
85- console . error ( `Prompt error: ${ error } ` ) ;
86- throw error ;
87- } else {
88- console . log ( `Option Chosen:\n${ options [ argv . option ] . templateName } \nDirectory Chosen/Default:\n${ argv . path } \nProject Name:\n${ argv . projectName || options [ argv . option ] . defaultName } ` ) ;
89- console . log ( `Starting Project creation...` ) ;
90- initiate ( options [ argv . option - 1 ] , argv . path , argv . projectName ) ;
91- }
92- } ) ;
123+ } ) ( ) ;
0 commit comments