@@ -156,7 +156,29 @@ async function processTemplates({ newList, manifest, all, force, spinner, client
156156 if ( force || await confirmation ( ) ) {
157157 spinner . text = 'Pushing templates to Postmark...'
158158 spinner . start ( )
159- return pushTemplates ( spinner , client , pushManifest )
159+ await pushTemplates (
160+ client ,
161+ pushManifest ,
162+ function handleBeforePush ( template ) {
163+ spinner . color = "yellow" ;
164+ spinner . text = `Pushing template: ${ template . Alias } ` ;
165+ } ,
166+ function handleError ( template , error ) {
167+ spinner . stop ( )
168+ logError ( `\n${ template . Alias || template . Name } : ${ error } ` )
169+ spinner . start ( )
170+ } ,
171+ function handleComplete ( failed ) {
172+ spinner . stop ( )
173+ log ( '✅ All finished!' , { color : 'green' } )
174+
175+ if ( failed > 0 ) {
176+ logError (
177+ `⚠️ Failed to push ${ failed } ${ pluralize ( failed , 'template' , 'templates' ) } . Please see the output above for more details.`
178+ )
179+ }
180+ }
181+ )
160182 } else {
161183 log ( 'Canceling push. Have a good day!' )
162184 }
@@ -312,43 +334,43 @@ function printReview({ templates, layouts }: TemplatePushReview) {
312334/**
313335 * Push all local templates
314336 */
315- async function pushTemplates ( spinner : ora . Ora , client : ServerClient , templates : TemplateManifest [ ] ) {
316- let failed = 0
317-
318- for ( const template of templates ) {
319- spinner . color = 'yellow'
320- spinner . text = `Pushing template: ${ template . Alias } `
321- if ( template . New ) {
337+ type OnPushTemplateError = ( template : TemplateManifest , error : unknown ) => void
338+ type OnPushTemplatesComplete = ( failed : number ) => void
339+ type OnBeforePushTemplate = ( template : TemplateManifest ) => void
340+ export async function pushTemplates (
341+ client : ServerClient ,
342+ localTemplates : TemplateManifest [ ] ,
343+ onBeforePush : OnBeforePushTemplate ,
344+ onError : OnPushTemplateError ,
345+ onComplete : OnPushTemplatesComplete
346+ ) {
347+ let failed = 0 ;
348+
349+ // Push first layouts, then standard templates. We're iterating the list twice which is not super efficient,
350+ // but it's easier to read and maintain.
351+ // We need to push layouts first because they can be used by standard templates.
352+ for ( const templateType of [ TemplateTypes . Layout , TemplateTypes . Standard ] ) {
353+ for ( const template of localTemplates ) {
354+ if ( template . TemplateType !== templateType ) continue ;
355+
356+ onBeforePush ( template ) ;
322357 try {
323- await client . createTemplate ( template )
358+ await pushTemplate ( template ) ;
324359 } catch ( error ) {
325- handleError ( error , template )
326- failed ++
327- }
328- } else {
329- invariant ( template . Alias , 'Template alias is required' )
330- try {
331- await client . editTemplate ( template . Alias , template )
332- } catch ( error ) {
333- handleError ( error , template )
334- failed ++
360+ onError ( template , error ) ;
361+ failed ++ ;
335362 }
336363 }
337364 }
338365
339- spinner . stop ( )
366+ onComplete ( failed ) ;
340367
341- log ( '✅ All finished!' , { color : 'green' } )
342-
343- if ( failed > 0 ) {
344- logError (
345- `⚠️ Failed to push ${ failed } ${ pluralize ( failed , 'template' , 'templates' ) } . Please see the output above for more details.`
346- )
347- }
348-
349- function handleError ( error : unknown , template : TemplateManifest ) {
350- spinner . stop ( )
351- logError ( `\n${ template . Alias || template . Name } : ${ error } ` )
352- spinner . start ( )
368+ async function pushTemplate ( template : TemplateManifest ) : Promise < void > {
369+ invariant ( template . Alias , "Template alias is required" ) ;
370+ if ( template . New ) {
371+ await client . createTemplate ( template ) ;
372+ } else {
373+ await client . editTemplate ( template . Alias , template ) ;
374+ }
353375 }
354376}
0 commit comments