@@ -4,22 +4,7 @@ import { createComponentMetaCheckerByJsonConfig } from 'vue-component-meta'
4
4
import { destr } from 'destr'
5
5
import JSON5 from 'json5'
6
6
import 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'
23
8
24
9
function stripeTypeScriptInternalTypesSchema ( type : any ) : any {
25
10
if ( ! type )
@@ -55,6 +40,21 @@ function stripeTypeScriptInternalTypesSchema(type: any): any {
55
40
export default defineEventHandler ( async ( ) => {
56
41
try {
57
42
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
+ )
58
58
59
59
const emails : Email [ ] = await Promise . all (
60
60
nitroEmails . map ( async ( email ) => {
@@ -64,85 +64,93 @@ export default defineEventHandler(async () => {
64
64
const emailData = JSON . parse ( data )
65
65
const emailPath = path . join (
66
66
rootDir ,
67
- 'emails' ,
68
67
email . replaceAll ( ':' , '/' ) ,
69
68
)
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
74
69
75
- if ( a . required && ! b . required )
76
- return - 1
70
+ let destructuredProps = [ ]
77
71
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
80
77
81
- if ( a . type !== 'boolean' && b . type === 'boolean' )
82
- return - 1
78
+ if ( a . required && ! b . required )
79
+ return - 1
83
80
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
91
83
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
+ }
96
101
}
97
- }
98
102
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
+ }
103
108
}
104
- }
105
109
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
+ }
110
115
}
111
- }
112
116
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
+ }
117
122
}
118
- }
119
123
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
+ }
124
136
}
125
- }
126
137
127
- if ( type === 'Date' ) {
128
138
return {
129
- type : 'date ' ,
130
- value : value ? eval ( value ) : new Date ( ) . toISOString ( ) ,
139
+ type : 'string ' ,
140
+ value : value ?? '' ,
131
141
}
132
- }
142
+ } )
133
143
134
144
return {
135
- type : 'string' ,
136
- value : value ?? '' ,
145
+ label : prop . name ,
146
+ type : destructuredType [ 0 ] . type ,
147
+ value : destructuredType [ 0 ] . value ,
137
148
}
138
149
} )
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
+ }
146
154
147
155
const content = ( await useStorage ( 'assets:emails' ) . getItem (
148
156
email ,
0 commit comments