@@ -4,22 +4,6 @@ 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
- )
23
7
24
8
function stripeTypeScriptInternalTypesSchema ( type : any ) : any {
25
9
if ( ! type )
@@ -55,6 +39,21 @@ function stripeTypeScriptInternalTypesSchema(type: any): any {
55
39
export default defineEventHandler ( async ( ) => {
56
40
try {
57
41
const nitroEmails = await useStorage ( 'assets:emails' ) . getKeys ( )
42
+ const rootDir = useRuntimeConfig ( ) . public . vueEmail . emailsDir || process . cwd ( )
43
+
44
+ const checker = createComponentMetaCheckerByJsonConfig (
45
+ rootDir ,
46
+ {
47
+ extends : path . join ( rootDir , '..' , 'tsconfig.json' ) ,
48
+ skipLibCheck : true ,
49
+ include : [ '**/*.vue' ] ,
50
+ exclude : [ ] ,
51
+ } ,
52
+ {
53
+ forceUseTs : true ,
54
+ printer : { newLine : 1 } ,
55
+ } ,
56
+ )
58
57
59
58
const emails : Email [ ] = await Promise . all (
60
59
nitroEmails . map ( async ( email ) => {
@@ -64,85 +63,93 @@ export default defineEventHandler(async () => {
64
63
const emailData = JSON . parse ( data )
65
64
const emailPath = path . join (
66
65
rootDir ,
67
- 'emails' ,
68
66
email . replaceAll ( ':' , '/' ) ,
69
67
)
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
68
75
- if ( a . required && ! b . required )
76
- return - 1
69
+ let destructuredProps : any [ ] = [ ]
77
70
78
- if ( a . type === 'boolean' && b . type !== 'boolean' )
79
- return 1
71
+ try {
72
+ const { props } = checker . getComponentMeta ( emailPath )
73
+ let emailProps = ( props ) . filter ( prop => ! prop . global ) . sort ( ( a , b ) => {
74
+ if ( ! a . required && b . required )
75
+ return 1
80
76
81
- if ( a . type !== 'boolean' && b . type === 'boolean' )
82
- return - 1
77
+ if ( a . required && ! b . required )
78
+ return - 1
83
79
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
80
+ if ( a . type === 'boolean' && b . type !== 'boolean' )
81
+ return 1
91
82
92
- if ( type === 'string' ) {
93
- return {
94
- type : 'string' ,
95
- value : destr ( value ) ?? '' ,
83
+ if ( a . type !== 'boolean' && b . type === 'boolean' )
84
+ return - 1
85
+
86
+ return 0
87
+ } )
88
+
89
+ emailProps = emailProps . map ( stripeTypeScriptInternalTypesSchema )
90
+ destructuredProps = emailProps . map ( ( prop ) => {
91
+ const destructuredType = prop . type . split ( '|' ) . map ( ( type ) => {
92
+ type = type . trim ( )
93
+ const value = prop . default
94
+
95
+ if ( type === 'string' ) {
96
+ return {
97
+ type : 'string' ,
98
+ value : destr ( value ) ?? '' ,
99
+ }
96
100
}
97
- }
98
101
99
- if ( type === 'number' ) {
100
- return {
101
- type : 'number' ,
102
- value : destr ( value ) || 0 ,
102
+ if ( type === 'number' ) {
103
+ return {
104
+ type : 'number' ,
105
+ value : destr ( value ) || 0 ,
106
+ }
103
107
}
104
- }
105
108
106
- if ( type === 'boolean' ) {
107
- return {
108
- type : 'boolean' ,
109
- value : destr ( value ) || false ,
109
+ if ( type === 'boolean' ) {
110
+ return {
111
+ type : 'boolean' ,
112
+ value : destr ( value ) || false ,
113
+ }
110
114
}
111
- }
112
115
113
- if ( type === 'object' || type . includes ( 'Record' ) || type . includes ( 'Record<' ) ) {
114
- return {
115
- type : 'object' ,
116
- value : value ? JSON5 . parse ( value ) : { } ,
116
+ if ( type === 'object' || type . includes ( 'Record' ) || type . includes ( 'Record<' ) ) {
117
+ return {
118
+ type : 'object' ,
119
+ value : value ? JSON5 . parse ( value ) : { } ,
120
+ }
117
121
}
118
- }
119
122
120
- if ( type === 'array' || type . includes ( '[]' ) || type . includes ( 'Array' ) || type . includes ( 'Array<' ) ) {
121
- return {
122
- type : 'array' ,
123
- value : value ? JSON5 . parse ( value ) : [ ] ,
123
+ if ( type === 'array' || type . includes ( '[]' ) || type . includes ( 'Array' ) || type . includes ( 'Array<' ) ) {
124
+ return {
125
+ type : 'array' ,
126
+ value : value ? JSON5 . parse ( value ) : [ ] ,
127
+ }
128
+ }
129
+
130
+ if ( type === 'Date' ) {
131
+ return {
132
+ type : 'date' ,
133
+ value : value ? eval ( value ) : new Date ( ) . toISOString ( ) ,
134
+ }
124
135
}
125
- }
126
136
127
- if ( type === 'Date' ) {
128
137
return {
129
- type : 'date ' ,
130
- value : value ? eval ( value ) : new Date ( ) . toISOString ( ) ,
138
+ type : 'string ' ,
139
+ value : value ?? '' ,
131
140
}
132
- }
141
+ } )
133
142
134
143
return {
135
- type : 'string' ,
136
- value : value ?? '' ,
144
+ label : prop . name ,
145
+ type : destructuredType [ 0 ] . type ,
146
+ value : destructuredType [ 0 ] . value ,
137
147
}
138
148
} )
139
-
140
- return {
141
- label : prop . name ,
142
- type : destructuredType [ 0 ] . type ,
143
- value : destructuredType [ 0 ] . value ,
144
- }
145
- } )
149
+ }
150
+ catch ( error ) {
151
+ console . warn ( 'Error destructuring props' , error )
152
+ }
146
153
147
154
const content = ( await useStorage ( 'assets:emails' ) . getItem (
148
155
email ,
0 commit comments