@@ -60,113 +60,104 @@ export function previewFilename(moduleName: string, functionName: string) {
60
60
) ;
61
61
}
62
62
63
- export const handler = buildHandler (
64
- async ( argv : ExportPreviewsArgs ) => {
65
- if ( ! argv . outDir ) throw new Error ( "outDir option is not set" ) ;
66
-
67
- const outDir = argv . outDir ;
68
-
69
- if ( typeof outDir !== "string" ) {
70
- error ( "please specify an outDir like --outDir ./html" ) ;
71
- return ;
72
- }
73
-
74
- if ( undefined === argv . emailsDir ) {
75
- error ( "please specific an emailsDir like --emailsDir ./emails" ) ;
76
- return ;
77
- }
78
-
79
- const previewsPath = getPreviewsDirectory ( argv . emailsDir ) ;
80
- if ( ! previewsPath ) {
81
- error (
82
- "Could not find emails directory. Have you initialized the project with `mailing init`?"
83
- ) ;
84
- return ;
85
- }
86
-
87
- registerRequireHooks ( ) ;
88
-
89
- const previewText = argv . minify ? "minified preview html" : "preview html" ;
90
-
91
- let count = 0 ;
92
-
93
- const lint : { [ filename : string ] : HtmlLintError [ ] } = { } ;
94
- const toWrite : Array < ( ) => Promise < void > > = [ ] ;
95
- const filenames : string [ ] = [ ] ;
96
-
97
- const previewRenders = readdirSync ( previewsPath )
98
- . filter ( ( path ) => ! / ^ \. / . test ( path ) )
99
- . flatMap ( ( p ) => {
100
- const previewPath = resolve ( previewsPath , p ) ;
101
- const previewModule = require ( previewPath ) ;
102
-
103
- return Object . keys ( require ( previewPath ) ) . flatMap (
104
- async ( previewFunction ) => {
105
- const filename = previewFilename ( p , previewFunction ) ;
106
- count ++ ;
107
-
108
- const { html, errors, htmlLint } = render (
109
- await previewModule [ previewFunction ] ( )
110
- ) ;
111
- if ( errors . length ) {
112
- error ( `MJML errors rendering ${ filename } :` , errors ) ;
113
- }
114
-
115
- if ( htmlLint . length && ! argv . skipLint ) {
116
- lint [ filename ] = htmlLint ;
117
- }
118
-
119
- const minifyConfig = {
120
- collapseWhitespace : true ,
121
- minifyCSS : false ,
122
- caseSensitive : true ,
123
- removeEmptyAttributes : true ,
124
- } ;
125
-
126
- const outHtml = argv . minify
127
- ? await minify ( html , minifyConfig )
128
- : html ;
129
- filenames . push ( filename ) ;
130
- toWrite . push ( async ( ) =>
131
- outputFile ( resolve ( outDir , filename ) , outHtml )
132
- ) ;
63
+ export const handler = buildHandler ( async ( argv : ExportPreviewsArgs ) => {
64
+ if ( ! argv . outDir ) throw new Error ( "outDir option is not set" ) ;
65
+
66
+ const outDir = argv . outDir ;
67
+
68
+ if ( typeof outDir !== "string" ) {
69
+ error ( "please specify an outDir like --outDir ./html" ) ;
70
+ return ;
71
+ }
72
+
73
+ if ( undefined === argv . emailsDir ) {
74
+ error ( "please specific an emailsDir like --emailsDir ./emails" ) ;
75
+ return ;
76
+ }
77
+
78
+ const previewsPath = getPreviewsDirectory ( argv . emailsDir ) ;
79
+ if ( ! previewsPath ) {
80
+ error (
81
+ "Could not find emails directory. Have you initialized the project with `mailing init`?"
82
+ ) ;
83
+ return ;
84
+ }
85
+
86
+ registerRequireHooks ( ) ;
87
+
88
+ const previewText = argv . minify ? "minified preview html" : "preview html" ;
89
+
90
+ let count = 0 ;
91
+
92
+ const lint : { [ filename : string ] : HtmlLintError [ ] } = { } ;
93
+ const toWrite : Array < ( ) => Promise < void > > = [ ] ;
94
+ const filenames : string [ ] = [ ] ;
95
+
96
+ const previewRenders = readdirSync ( previewsPath )
97
+ . filter ( ( path ) => ! / ^ \. / . test ( path ) )
98
+ . flatMap ( ( p ) => {
99
+ const previewPath = resolve ( previewsPath , p ) ;
100
+ const previewModule = require ( previewPath ) ;
101
+
102
+ return Object . keys ( require ( previewPath ) ) . flatMap (
103
+ async ( previewFunction ) => {
104
+ const filename = previewFilename ( p , previewFunction ) ;
105
+ count ++ ;
106
+
107
+ const { html, errors, htmlLint } = render (
108
+ await previewModule [ previewFunction ] ( )
109
+ ) ;
110
+ if ( errors . length ) {
111
+ error ( `MJML errors rendering ${ filename } :` , errors ) ;
133
112
}
134
- ) ;
135
- } ) ;
136
- await Promise . all ( previewRenders ) ;
137
-
138
- const lintCount = Object . keys ( lint ) . length ;
139
- if ( lintCount ) {
140
- error (
141
- lintCount > 1
142
- ? `Aborted because ${ lintCount } files have lint errors:`
143
- : `Aborted because 1 file has lint errors:` ,
144
- "\n\n" ,
145
- Object . entries ( lint )
146
- . map (
147
- ( [ filename , errors ] ) =>
148
- `${ filename } \n${ errors
149
- . map ( ( e , i ) => ` ${ i + 1 } . ${ e . message } ` )
150
- . join ( "\n\n" ) } `
151
- )
152
- . join ( "\n\n" ) ,
153
- "\n\n"
154
- ) ;
155
113
156
- return ;
157
- }
114
+ if ( htmlLint . length && ! argv . skipLint ) {
115
+ lint [ filename ] = htmlLint ;
116
+ }
158
117
159
- log ( `Exporting ${ previewText } to` ) ;
160
- log ( `${ outDir } /` ) ;
161
- await Promise . all ( toWrite . map ( ( f ) => f ( ) ) ) ;
162
- for ( const f of filenames . sort ( ) ) {
163
- log ( ` |-- ${ f } ` ) ;
164
- }
165
- log ( `✅ Processed ${ count } previews\n` ) ;
166
- } ,
167
- {
168
- captureOptions : ( ) => {
169
- return { event : "export-previews invoked" } ;
170
- } ,
118
+ const minifyConfig = {
119
+ collapseWhitespace : true ,
120
+ minifyCSS : false ,
121
+ caseSensitive : true ,
122
+ removeEmptyAttributes : true ,
123
+ } ;
124
+
125
+ const outHtml = argv . minify ? await minify ( html , minifyConfig ) : html ;
126
+ filenames . push ( filename ) ;
127
+ toWrite . push ( async ( ) =>
128
+ outputFile ( resolve ( outDir , filename ) , outHtml )
129
+ ) ;
130
+ }
131
+ ) ;
132
+ } ) ;
133
+ await Promise . all ( previewRenders ) ;
134
+
135
+ const lintCount = Object . keys ( lint ) . length ;
136
+ if ( lintCount ) {
137
+ error (
138
+ lintCount > 1
139
+ ? `Aborted because ${ lintCount } files have lint errors:`
140
+ : `Aborted because 1 file has lint errors:` ,
141
+ "\n\n" ,
142
+ Object . entries ( lint )
143
+ . map (
144
+ ( [ filename , errors ] ) =>
145
+ `${ filename } \n${ errors
146
+ . map ( ( e , i ) => ` ${ i + 1 } . ${ e . message } ` )
147
+ . join ( "\n\n" ) } `
148
+ )
149
+ . join ( "\n\n" ) ,
150
+ "\n\n"
151
+ ) ;
152
+
153
+ return ;
154
+ }
155
+
156
+ log ( `Exporting ${ previewText } to` ) ;
157
+ log ( `${ outDir } /` ) ;
158
+ await Promise . all ( toWrite . map ( ( f ) => f ( ) ) ) ;
159
+ for ( const f of filenames . sort ( ) ) {
160
+ log ( ` |-- ${ f } ` ) ;
171
161
}
172
- ) ;
162
+ log ( `✅ Processed ${ count } previews\n` ) ;
163
+ } ) ;
0 commit comments