@@ -12,6 +12,12 @@ import prompts from 'prompts';
1212
1313import pkg from '../package.json' ;
1414
15+ interface CreateEmailArgs {
16+ jsx: boolean ;
17+ name: string ;
18+ outputPath: string ;
19+ }
20+
1521const cancelled = ( ) => {
1622 throw new Error ( chalk `{red ✖} Operation cancelled` ) ;
1723} ;
@@ -35,13 +41,39 @@ const isEmpty = (path: string) => {
3541} ;
3642const { log } = console ;
3743const normalizePath = ( filename : string ) => filename . split ( win32 . sep ) . join ( posix . sep ) ;
44+ const typeDep = ',\n"@types/react": "^18.2.0",\n"typescript": "^5.2.2"' ;
45+ const typeProps = `\nexport type TemplateProps = {
46+ email: string;
47+ name: string;
48+ }` ;
49+
50+ export const createEmail = async ( { jsx, name, outputPath } : CreateEmailArgs ) => {
51+ const data = {
52+ name,
53+ propsType : jsx ? '' : ': TemplateProps' ,
54+ typeProps : jsx ? '' : typeProps
55+ } ;
56+ const templatePath = resolve ( __dirname , '../generators/templates/email.mustache' ) ;
57+ const template = await readFile ( templatePath , 'utf8' ) ;
58+ const contents = mustache . render ( template , data ) ;
59+ const fileName = basename ( templatePath )
60+ . replace ( '_' , '' )
61+ . replace ( '.mustache' , jsx ? '.jsx' : '.tsx' ) ;
62+ const outPath = join ( outputPath , fileName ) ;
63+
64+ log ( 'Creating a new template at' , outPath ) ;
65+
66+ await writeFile ( outPath , contents , 'utf8' ) ;
67+
68+ return outPath ;
69+ } ;
3870
3971const run = async ( ) => {
4072 log ( intro ) ;
4173
4274 const argTargetDir = formatTargetDir ( process . argv [ 2 ] ) ;
4375 let targetPath = argTargetDir || defaultTargetDir ;
44- let result : prompts . Answers < 'projectName' | 'overwrite' > ;
76+ let result : prompts . Answers < 'projectName' | 'projectType' | ' overwrite'> ;
4577
4678 try {
4779 result = await prompts (
@@ -55,6 +87,15 @@ const run = async () => {
5587 } ,
5688 type : argTargetDir ? null : 'text'
5789 } ,
90+ {
91+ choices : [
92+ { title : 'TypeScript' , value : 'TypeScript' } ,
93+ { title : 'JavaScript' , value : 'JavaScript' }
94+ ] ,
95+ message : 'TypeScript or JavaScript:' ,
96+ name : 'projectType' ,
97+ type : 'select'
98+ } ,
5899 {
59100 message : ( ) =>
60101 targetPath === '.'
@@ -78,20 +119,23 @@ const run = async () => {
78119 return ;
79120 }
80121
81- const { overwrite, projectName } = result ;
122+ const { overwrite, projectName, projectType } = result ;
82123 const root = join ( process . cwd ( ) , targetPath ) ;
124+ const generatorsPath = resolve ( __dirname , '../generators' ) ;
125+ const jsx = projectType === 'JavaScript' ;
126+ const templates = await globby ( [ normalizePath ( join ( generatorsPath , '/*.*' ) ) ] ) ;
127+ const outputPath = join ( root , 'templates' ) ;
128+ const templateData = { name : projectName , typeDep : jsx ? '' : typeDep } ;
83129
84130 log ( chalk `\n{blue Creating Project} at: {dim ${ root } }` ) ;
85131
86- const generatorsPath = resolve ( __dirname , '../generators' ) ;
87- const templates = await globby ( [ normalizePath ( join ( generatorsPath , '/**/*.*' ) ) ] ) ;
88- const templateData = { name : projectName } ;
89-
90132 if ( overwrite && existsSync ( root ) ) clearDirectory ( root ) ;
91133
92- await mkdir ( join ( root , 'templates' ) , { recursive : true } ) ;
134+ await mkdir ( outputPath , { recursive : true } ) ;
93135
94136 for ( const path of templates ) {
137+ // eslint-disable-next-line no-continue
138+ if ( path . endsWith ( '_tsconfig.json' ) && jsx ) continue ;
95139 const template = await readFile ( path , 'utf8' ) ;
96140 const contents = mustache . render ( template , templateData ) ;
97141 const basePath = dirname ( path ) ;
@@ -101,6 +145,8 @@ const run = async () => {
101145 await writeFile ( outPath , contents , 'utf8' ) ;
102146 }
103147
148+ await createEmail ( { jsx, name : projectName , outputPath } ) ;
149+
104150 const packageManager = await detect ( ) ;
105151 const install =
106152 packageManager === 'yarn'
0 commit comments