Skip to content

Commit

Permalink
Pushing layouts before templates
Browse files Browse the repository at this point in the history
Fixes #41
  • Loading branch information
tomek-ac committed Aug 17, 2023
1 parent 28b78e5 commit 9008bbe
Show file tree
Hide file tree
Showing 4 changed files with 331 additions and 32 deletions.
158 changes: 158 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@types/lodash": "^4.14.123",
"@types/mocha": "^9.1.0",
"@types/nconf": "^0.10.0",
"@types/sinon": "^10.0.16",
"@types/table": "^4.0.5",
"@types/traverse": "^0.6.32",
"@types/watch": "^1.0.1",
Expand All @@ -45,6 +46,7 @@
"mocha": "^9.1.4",
"nconf": "^0.11.4",
"pre-commit": "^1.2.2",
"sinon": "^15.2.0",
"ts-node": "^8.0.3",
"typescript": "^4.5.5"
},
Expand Down
86 changes: 54 additions & 32 deletions src/commands/templates/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,29 @@ async function processTemplates({ newList, manifest, all, force, spinner, client
if (force || await confirmation()) {
spinner.text = 'Pushing templates to Postmark...'
spinner.start()
return pushTemplates(spinner, client, pushManifest)
await pushTemplates(
client,
pushManifest,
function handleBeforePush(template) {
spinner.color = "yellow";
spinner.text = `Pushing template: ${template.Alias}`;
},
function handleError(template, error) {
spinner.stop()
logError(`\n${template.Alias || template.Name}: ${error}`)
spinner.start()
},
function handleComplete(failed){
spinner.stop()
log('✅ All finished!', { color: 'green' })

if (failed > 0) {
logError(
`⚠️ Failed to push ${failed} ${pluralize(failed, 'template', 'templates')}. Please see the output above for more details.`
)
}
}
)
} else {
log('Canceling push. Have a good day!')
}
Expand Down Expand Up @@ -312,43 +334,43 @@ function printReview({ templates, layouts }: TemplatePushReview) {
/**
* Push all local templates
*/
async function pushTemplates(spinner: ora.Ora, client: ServerClient, templates: TemplateManifest[]) {
let failed = 0

for (const template of templates) {
spinner.color = 'yellow'
spinner.text = `Pushing template: ${template.Alias}`
if (template.New) {
type OnPushTemplateError = (template: TemplateManifest, error: unknown) => void
type OnPushTemplatesComplete = (failed: number) => void
type OnBeforePushTemplate = (template: TemplateManifest) => void
export async function pushTemplates(
client: ServerClient,
localTemplates: TemplateManifest[],
onBeforePush: OnBeforePushTemplate,
onError: OnPushTemplateError,
onComplete: OnPushTemplatesComplete
) {
let failed = 0;

// Push first layouts, then standard templates. We're iterating the list twice which is not super efficient,
// but it's easier to read and maintain.
// We need to push layouts first because they can be used by standard templates.
for (const templateType of [TemplateTypes.Layout, TemplateTypes.Standard]) {
for (const template of localTemplates) {
if (template.TemplateType !== templateType) continue;

onBeforePush(template);
try {
await client.createTemplate(template)
await pushTemplate(template);
} catch (error) {
handleError(error, template)
failed++
}
} else {
invariant(template.Alias, 'Template alias is required')
try {
await client.editTemplate(template.Alias, template)
} catch (error) {
handleError(error, template)
failed++
onError(template, error);
failed++;
}
}
}

spinner.stop()
onComplete(failed);

log('✅ All finished!', { color: 'green' })

if (failed > 0) {
logError(
`⚠️ Failed to push ${failed} ${pluralize(failed, 'template', 'templates')}. Please see the output above for more details.`
)
}

function handleError(error: unknown, template: TemplateManifest) {
spinner.stop()
logError(`\n${template.Alias || template.Name}: ${error}`)
spinner.start()
async function pushTemplate(template: TemplateManifest): Promise<void> {
invariant(template.Alias, "Template alias is required");
if (template.New) {
await client.createTemplate(template);
} else {
await client.editTemplate(template.Alias, template);
}
}
}
Loading

0 comments on commit 9008bbe

Please sign in to comment.