Skip to content

Commit

Permalink
Handle scenario when server templates are empty
Browse files Browse the repository at this point in the history
  • Loading branch information
derekrushforth committed Jan 28, 2021
1 parent d401ecb commit a65ee59
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 27 deletions.
83 changes: 56 additions & 27 deletions src/commands/templates/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
TemplatePushReview,
TemplatePushArguments,
Templates,
ProcessTemplates,
} from '../../types'
import { pluralize, log, validateToken } from '../../utils'

Expand Down Expand Up @@ -93,34 +94,29 @@ const push = (serverToken: string, args: TemplatePushArguments) => {
client
.getTemplates({ count: 300 })
.then(response => {
getTemplateContent(client, response).then(newList => {
compareTemplates(newList, manifest, all)

spinner.stop()
if (pushManifest.length === 0)
return log('There are no changes to push.')

// Show which templates are changing
printReview(review)

// Push templates if force arg is present
if (force) {
spinner.text = 'Pushing templates to Postmark...'
spinner.start()
return pushTemplates(spinner, client, pushManifest)
}

// Ask for user confirmation
confirmation().then(answer => {
if (answer.confirm) {
spinner.text = 'Pushing templates to Postmark...'
spinner.start()
pushTemplates(spinner, client, pushManifest)
} else {
log('Canceling push. Have a good day!')
}
// Check if there are templates on the server
if (response.TotalCount === 0) {
processTemplates({
newList: [],
manifest: manifest,
all: all,
force: force,
spinner: spinner,
client: client,
})
})
} else {
// Gather template content before processing templates
getTemplateContent(client, response).then(newList => {
processTemplates({
newList: newList,
manifest: manifest,
all: all,
force: force,
spinner: spinner,
client: client,
})
})
}
})
.catch((error: any) => {
spinner.stop()
Expand All @@ -134,6 +130,39 @@ const push = (serverToken: string, args: TemplatePushArguments) => {
}
}

/**
* Compare templates and CLI flow
*/
const processTemplates = (config: ProcessTemplates) => {
const { newList, manifest, all, force, spinner, client } = config

compareTemplates(newList, manifest, all)

spinner.stop()
if (pushManifest.length === 0) return log('There are no changes to push.')

// Show which templates are changing
printReview(review)

// Push templates if force arg is present
if (force) {
spinner.text = 'Pushing templates to Postmark...'
spinner.start()
return pushTemplates(spinner, client, pushManifest)
}

// Ask for user confirmation
confirmation().then(answer => {
if (answer.confirm) {
spinner.text = 'Pushing templates to Postmark...'
spinner.start()
pushTemplates(spinner, client, pushManifest)
} else {
log('Canceling push. Have a good day!')
}
})
}

/**
* Gather template content from server to compare against local versions
*/
Expand Down
9 changes: 9 additions & 0 deletions src/types/Template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,12 @@ export interface TemplateValidationPayload {
TextBody: string
TemplateType: 'Standard' | 'Layout'
}

export interface ProcessTemplates {
newList: TemplateManifest[]
manifest: TemplateManifest[]
all: boolean
force: boolean
spinner: any
client: any
}

0 comments on commit a65ee59

Please sign in to comment.