@@ -4,22 +4,7 @@ import { createComponentMetaCheckerByJsonConfig } from 'vue-component-meta'
44import { destr } from 'destr'
55import JSON5 from 'json5'
66import type { Email } from '../../types/email'
7- import { createError , defineEventHandler , useStorage } from '#imports'
8-
9- const rootDir = process . cwd ( )
10- const checker = createComponentMetaCheckerByJsonConfig (
11- rootDir ,
12- {
13- extends : `${ rootDir } /tsconfig.json` ,
14- skipLibCheck : true ,
15- include : [ 'emails/**/*' ] ,
16- exclude : [ ] ,
17- } ,
18- {
19- forceUseTs : true ,
20- printer : { newLine : 1 } ,
21- } ,
22- )
7+ import { createError , defineEventHandler , useRuntimeConfig , useStorage } from '#imports'
238
249function stripeTypeScriptInternalTypesSchema ( type : any ) : any {
2510 if ( ! type )
@@ -55,6 +40,21 @@ function stripeTypeScriptInternalTypesSchema(type: any): any {
5540export default defineEventHandler ( async ( ) => {
5641 try {
5742 const nitroEmails = await useStorage ( 'assets:emails' ) . getKeys ( )
43+ const rootDir = useRuntimeConfig ( ) . public . vueEmail . emailsDir || process . cwd ( )
44+
45+ const checker = createComponentMetaCheckerByJsonConfig (
46+ rootDir ,
47+ {
48+ extends : path . join ( rootDir , '..' , 'tsconfig.json' ) ,
49+ skipLibCheck : true ,
50+ include : [ './emails/**/*.vue' ] ,
51+ exclude : [ ] ,
52+ } ,
53+ {
54+ forceUseTs : true ,
55+ printer : { newLine : 1 } ,
56+ } ,
57+ )
5858
5959 const emails : Email [ ] = await Promise . all (
6060 nitroEmails . map ( async ( email ) => {
@@ -64,85 +64,93 @@ export default defineEventHandler(async () => {
6464 const emailData = JSON . parse ( data )
6565 const emailPath = path . join (
6666 rootDir ,
67- 'emails' ,
6867 email . replaceAll ( ':' , '/' ) ,
6968 )
70- const { props } = checker . getComponentMeta ( emailPath )
71- let emailProps = ( props ) . filter ( prop => ! prop . global ) . sort ( ( a , b ) => {
72- if ( ! a . required && b . required )
73- return 1
7469
75- if ( a . required && ! b . required )
76- return - 1
70+ let destructuredProps = [ ]
7771
78- if ( a . type === 'boolean' && b . type !== 'boolean' )
79- return 1
72+ try {
73+ const { props } = checker . getComponentMeta ( emailPath )
74+ let emailProps = ( props ) . filter ( prop => ! prop . global ) . sort ( ( a , b ) => {
75+ if ( ! a . required && b . required )
76+ return 1
8077
81- if ( a . type !== 'boolean' && b . type === 'boolean' )
82- return - 1
78+ if ( a . required && ! b . required )
79+ return - 1
8380
84- return 0
85- } )
86- emailProps = emailProps . map ( stripeTypeScriptInternalTypesSchema )
87- const destructuredProps = emailProps . map ( ( prop ) => {
88- const destructuredType = prop . type . split ( '|' ) . map ( ( type ) => {
89- type = type . trim ( )
90- const value = prop . default
81+ if ( a . type === 'boolean' && b . type !== 'boolean' )
82+ return 1
9183
92- if ( type === 'string' ) {
93- return {
94- type : 'string' ,
95- value : destr ( value ) ?? '' ,
84+ if ( a . type !== 'boolean' && b . type === 'boolean' )
85+ return - 1
86+
87+ return 0
88+ } )
89+
90+ emailProps = emailProps . map ( stripeTypeScriptInternalTypesSchema )
91+ destructuredProps = emailProps . map ( ( prop ) => {
92+ const destructuredType = prop . type . split ( '|' ) . map ( ( type ) => {
93+ type = type . trim ( )
94+ const value = prop . default
95+
96+ if ( type === 'string' ) {
97+ return {
98+ type : 'string' ,
99+ value : destr ( value ) ?? '' ,
100+ }
96101 }
97- }
98102
99- if ( type === 'number' ) {
100- return {
101- type : 'number' ,
102- value : destr ( value ) || 0 ,
103+ if ( type === 'number' ) {
104+ return {
105+ type : 'number' ,
106+ value : destr ( value ) || 0 ,
107+ }
103108 }
104- }
105109
106- if ( type === 'boolean' ) {
107- return {
108- type : 'boolean' ,
109- value : destr ( value ) || false ,
110+ if ( type === 'boolean' ) {
111+ return {
112+ type : 'boolean' ,
113+ value : destr ( value ) || false ,
114+ }
110115 }
111- }
112116
113- if ( type === 'object' || type . includes ( 'Record' ) || type . includes ( 'Record<' ) ) {
114- return {
115- type : 'object' ,
116- value : value ? JSON5 . parse ( value ) : { } ,
117+ if ( type === 'object' || type . includes ( 'Record' ) || type . includes ( 'Record<' ) ) {
118+ return {
119+ type : 'object' ,
120+ value : value ? JSON5 . parse ( value ) : { } ,
121+ }
117122 }
118- }
119123
120- if ( type === 'array' || type . includes ( '[]' ) || type . includes ( 'Array' ) || type . includes ( 'Array<' ) ) {
121- return {
122- type : 'array' ,
123- value : value ? JSON5 . parse ( value ) : [ ] ,
124+ if ( type === 'array' || type . includes ( '[]' ) || type . includes ( 'Array' ) || type . includes ( 'Array<' ) ) {
125+ return {
126+ type : 'array' ,
127+ value : value ? JSON5 . parse ( value ) : [ ] ,
128+ }
129+ }
130+
131+ if ( type === 'Date' ) {
132+ return {
133+ type : 'date' ,
134+ value : value ? eval ( value ) : new Date ( ) . toISOString ( ) ,
135+ }
124136 }
125- }
126137
127- if ( type === 'Date' ) {
128138 return {
129- type : 'date ' ,
130- value : value ? eval ( value ) : new Date ( ) . toISOString ( ) ,
139+ type : 'string ' ,
140+ value : value ?? '' ,
131141 }
132- }
142+ } )
133143
134144 return {
135- type : 'string' ,
136- value : value ?? '' ,
145+ label : prop . name ,
146+ type : destructuredType [ 0 ] . type ,
147+ value : destructuredType [ 0 ] . value ,
137148 }
138149 } )
139-
140- return {
141- label : prop . name ,
142- type : destructuredType [ 0 ] . type ,
143- value : destructuredType [ 0 ] . value ,
144- }
145- } )
150+ }
151+ catch ( error ) {
152+ console . warn ( 'Error destructuring props' , error )
153+ }
146154
147155 const content = ( await useStorage ( 'assets:emails' ) . getItem (
148156 email ,
0 commit comments