Skip to content

Commit c82dab7

Browse files
authored
Pushing layouts before templates (#81)
Fixes #41
1 parent 28b78e5 commit c82dab7

4 files changed

Lines changed: 331 additions & 32 deletions

File tree

package-lock.json

Lines changed: 158 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"@types/lodash": "^4.14.123",
3434
"@types/mocha": "^9.1.0",
3535
"@types/nconf": "^0.10.0",
36+
"@types/sinon": "^10.0.16",
3637
"@types/table": "^4.0.5",
3738
"@types/traverse": "^0.6.32",
3839
"@types/watch": "^1.0.1",
@@ -45,6 +46,7 @@
4546
"mocha": "^9.1.4",
4647
"nconf": "^0.11.4",
4748
"pre-commit": "^1.2.2",
49+
"sinon": "^15.2.0",
4850
"ts-node": "^8.0.3",
4951
"typescript": "^4.5.5"
5052
},

src/commands/templates/push.ts

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)