Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pushing layouts before templates #81

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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