11import fs from "fs"
22import path from "path"
33import dotenv from "dotenv"
4+ import chalk from "chalk"
45
56dotenv . config ( )
7+ const programSuffix = `${ chalk . gray ( `[` ) } ${ chalk . green ( `EwT` ) } ${ chalk . gray ( `/bin]` ) } `
8+ const debugModeSuffix = chalk . gray ( `[debug]` )
69const isDevelopment = process . env . TOOLS_ENV === "development"
710const LoggerType = Object . freeze ( {
811 INFO : "Info" ,
@@ -17,30 +20,27 @@ const getAllEntityTypes = () => Object.values(EntityTypes).map((entity, index, a
1720
1821const Logger = ( text , type = LoggerType . EMPTY ) => {
1922 const typeFormatted = ( type === LoggerType . EMPTY ) ? `${ type } ` : `${ type } :`
20- if ( isDevelopment ) console . log ( `${ typeFormatted } ` , text )
23+ if ( isDevelopment ) console . log ( `${ programSuffix } ${ debugModeSuffix } ${ typeFormatted } ` , text )
2124}
2225
2326const formatFlagsString = ( args ) => {
2427 if ( ! Array . isArray ( args ) || args . length === 0 ) return "" ;
25- let lastIsAttr = false
2628 let isPreviousDump = false
2729
2830 return args . map ( ( token , index ) => {
2931 if ( index === 0 ) return token
3032 if ( token . startsWith ( "-" ) || isPreviousDump ) {
31- lastIsAttr = true
3233 if ( token . includes ( "dump" ) ) { isPreviousDump = true } else { isPreviousDump = false }
3334 return "+" + token
3435 }
3536
36- lastIsAttr = false
3737 return ";" + token
3838 } ) . join ( "" )
3939}
4040
4141const flagsHandler = ( flags ) => {
4242 const paramWithValue = flags . split ( "+" )
43- const flagsObject = new Object ( ) ;
43+ const flagsObject = { }
4444 paramWithValue . forEach ( token => {
4545 let [ attr , value ] = token . split ( ";" )
4646 if ( ! value ) value = true
@@ -55,7 +55,7 @@ const flagsHandler = (flags) => {
5555
5656const args = process . argv . slice ( 2 )
5757if ( args . length <= 0 ) {
58- console . log ( " Usage: . /ewtools.bat make:blog \ "title\"" )
58+ console . log ( ` ${ programSuffix } Usage: ${ chalk . green ( `. /ewtools.bat make:blog "title"` ) } ` )
5959 process . exit ( )
6060}
6161const [ command , title , ...restArgs ] = args
@@ -79,38 +79,38 @@ const EntityTypes = Object.freeze({
7979} )
8080
8181if ( args . length < 2 ) {
82- console . log ( " Usage: . /ewtools.bat make:blog \ "title\"" )
82+ console . log ( ` ${ programSuffix } Usage: ${ chalk . green ( `. /ewtools.bat make:blog "title"` ) } ` )
8383 process . exit ( 1 )
8484}
8585
86- const handleError = ( error , filename , action = ActionTypes . MAKE ) => {
87- if ( error ) return console . error ( `Error creating file: ${ error . message } ` )
86+ const handleError = ( error , filename , { type , action = ActionTypes . MAKE } = { } ) => {
87+ if ( error ) return console . error ( `${ programSuffix } Error creating file: ${ error . message } ` )
8888 const currentAction = ( action === ActionTypes . MAKE ) ? "created" : "removed"
89- console . log ( `File "${ filename } " ${ currentAction } successfully.` )
89+ console . log ( `${ programSuffix } File "${ type } / ${ filename } " ${ currentAction } successfully.` )
9090}
9191
9292const generateFile = ( title , entityType , getContent ) => {
93- const { filename, filepath } = generateFilePath ( title , entityType ) ;
94- const content = getContent ( title , flagsObject ) ;
95- fs . writeFile ( filepath , content , error => handleError ( error , filename ) ) ;
96- } ;
93+ const { filename, filepath } = generateFilePath ( title , entityType )
94+ const content = getContent ( title , flagsObject )
95+ fs . writeFile ( filepath , content , error => handleError ( error , filename , { type : entityType } ) )
96+ }
9797
9898const generateBlogFile = ( title ) => generateFile ( title , EntityTypes . BLOG , getDefaultBlogContent )
9999
100100const generateProjectFile = ( title ) => generateFile ( title , EntityTypes . PROJECT , getDefaultProjectContent )
101101
102102const generatePrototypeFile = ( title ) => generateFile ( title , EntityTypes . PROTOTYPE , getDefaultPrototypeContent )
103103
104- const removeFile = ( title , type ) => {
105- const { filename, filepath } = generateFilePath ( title , type )
106- fs . rm ( filepath , ( error ) => handleError ( error , filename , ActionTypes . DELETE ) )
104+ const removeFile = ( title , entityType ) => {
105+ const { filename, filepath } = generateFilePath ( title , entityType )
106+ fs . rm ( filepath , ( error ) => handleError ( error , filename , { action : ActionTypes . DELETE , type : entityType } ) )
107107}
108108
109109const generateFilePath = ( title , type ) => {
110110 let date = flagsObject . d || new Date ( ) . toISOString ( )
111111 const [ year , month , day ] = date . split ( "T" ) [ 0 ] . split ( "-" )
112112 if ( ! ( year && month && day ) ) {
113- console . log ( " Invalid date format. Please provide -d yyyy-mm-dd" )
113+ console . log ( ` ${ programSuffix } Invalid date format. Please provide -d yyyy-mm-dd` )
114114 process . exit ( 1 )
115115 }
116116 const formattedDate = type === EntityTypes . PROTOTYPE ? year : `${ year } -${ month } -${ day } `
@@ -120,17 +120,17 @@ const generateFilePath = (title, type) => {
120120 return { filename, filepath }
121121}
122122
123+ const tagsHandler = ( tags ) => typeof tags === "string" ? tags . split ( "," ) . map ( tag => `\n - ${ tag } ` ) . join ( "" ) : ""
124+
123125const getDefaultBlogContent = ( title , flags = { } ) => `---
124126title: ${ title }
125127description:
126128author:
127129draft: true
128130date: ${ new Date ( ) . toISOString ( ) }
129131tags:
130- - post
131- ${ typeof flags . t === "string" ? flags . t . split ( "," ) . map ( tag => ` - ${ tag } ` ) . join ( "\n" ) : "" } ---
132-
133- `
132+ - post${ tagsHandler ( flags . t ) }
133+ ---\n`
134134const getDefaultProjectContent = ( title , flags = { } ) => `---
135135title: ${ title }
136136description:
@@ -140,8 +140,8 @@ image:
140140linkDemo:
141141linkCode:
142142tags:
143- - project
144- ${ typeof flags . t === "string" ? flags . t . split ( "," ) . map ( tag => ` - ${ tag } ` ) . join ( "\n" ) : "" } ---` ;
143+ - project${ tagsHandler ( flags . t ) }
144+ ---\n`
145145const getDefaultPrototypeContent = ( title , flags = { } ) => `---
146146title: ${ title }
147147status: 1
@@ -151,9 +151,8 @@ linkDemo:
151151language:
152152code: |-
153153tags:
154- - prototype
155- ${ typeof flags . t === "string" ? flags . t . split ( "," ) . map ( tag => ` - ${ tag } ` ) . join ( "\n" ) : "" } ---
156- `
154+ - prototype${ tagsHandler ( flags . t ) }
155+ ---\n`
157156
158157const handleMake = ( type , title ) => {
159158 switch ( type ) {
@@ -167,7 +166,7 @@ const handleMake = (type, title) => {
167166 generatePrototypeFile ( title )
168167 break
169168 default :
170- console . log ( `Invalid Type, Valid Types: ${ getAllEntityTypes ( ) } ` )
169+ console . log ( `${ programSuffix } Invalid Type, Valid Types: ${ getAllEntityTypes ( ) } ` )
171170 }
172171}
173172
@@ -183,7 +182,7 @@ const handleDelete = (type, title) => {
183182 removeFile ( title , EntityTypes . PROTOTYPE )
184183 break
185184 default :
186- console . log ( `Invalid Type, Valid Types: ${ getAllEntityTypes ( ) } ` )
185+ console . log ( `${ programSuffix } Invalid Type, Valid Types: ${ getAllEntityTypes ( ) } ` )
187186 }
188187}
189188
@@ -196,5 +195,5 @@ if (!flagsObject.dump && !flagsObject.dp) switch (action) {
196195 handleDelete ( type , title )
197196 break
198197 default :
199- console . log ( `Invalid Action. Valid Actions: ${ getAllActionTypes ( ) } ` )
198+ console . log ( `${ programSuffix } Invalid Action. Valid Actions: ${ getAllActionTypes ( ) } ` )
200199}
0 commit comments