Skip to content

Commit

Permalink
Merge pull request #22 from wildbit/feature/template-models
Browse files Browse the repository at this point in the history
Template model storage
  • Loading branch information
derekrushforth authored Jan 28, 2020
2 parents e49b11a + da216d1 commit 967140b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2019 [Wildbit](https://wildbit.com)
Copyright (c) 2020 [Wildbit](https://wildbit.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postmark-cli",
"version": "1.3.6",
"version": "1.4.6",
"description": "A CLI tool for managing templates, sending emails, and fetching servers on Postmark.",
"main": "./dist/index.js",
"dependencies": {
Expand Down
8 changes: 6 additions & 2 deletions src/commands/templates/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ const preview = (serverToken: string, args: TemplatePreviewArguments) => {
if (layout && !layout.HtmlBody)
return renderTemplateInvalid(res, layoutError)

const { TemplateType, TestRenderModel } = template
const payload = {
HtmlBody: getSource('html', template, layout),
TemplateType: template.TemplateType,
TemplateType,
TestRenderModel,
}

return validateTemplateRequest('html', payload, res)
Expand All @@ -157,9 +159,11 @@ const preview = (serverToken: string, args: TemplatePreviewArguments) => {
if (layout && !layout.TextBody)
return renderTemplateInvalid(res, layoutError)

const { TemplateType, TestRenderModel } = template
const payload = {
TextBody: getSource('text', template, layout),
TemplateType: template.TemplateType,
TemplateType,
TestRenderModel,
}

return validateTemplateRequest('text', payload, res)
Expand Down
25 changes: 21 additions & 4 deletions src/commands/templates/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ const processTemplates = (options: ProcessTemplatesOptions) => {
requestCount++

// Save template to file system
saveTemplate(outputDir, response)
saveTemplate(outputDir, response, client)
totalDownloaded++

// Show feedback when finished saving templates
Expand Down Expand Up @@ -185,7 +185,7 @@ const processTemplates = (options: ProcessTemplatesOptions) => {
* Save template
* @return An object containing the HTML and Text body
*/
const saveTemplate = (outputDir: string, template: Template) => {
const saveTemplate = (outputDir: string, template: Template, client: any) => {
outputDir =
template.TemplateType === 'Layout' ? join(outputDir, '_layouts') : outputDir
const path: string = untildify(join(outputDir, template.Alias))
Expand All @@ -202,7 +202,7 @@ const saveTemplate = (outputDir: string, template: Template) => {
outputFileSync(join(path, 'content.txt'), template.TextBody)
}

const meta: MetaFile = {
let meta: MetaFile = {
Name: template.Name,
Alias: template.Alias,
...(template.Subject && { Subject: template.Subject }),
Expand All @@ -212,5 +212,22 @@ const saveTemplate = (outputDir: string, template: Template) => {
}),
}

outputFileSync(join(path, 'meta.json'), JSON.stringify(meta, null, 2))
// Save suggested template model
client
.validateTemplate({
...(template.HtmlBody && { HtmlBody: template.HtmlBody }),
...(template.TextBody && { TextBody: template.TextBody }),
...meta,
})
.then((result: any) => {
meta.TestRenderModel = result.SuggestedTemplateModel
})
.catch((error: any) => {
log('Error fetching suggested template model', { error: true })
log(error, { error: true })
})
.then(() => {
// Save the file regardless of success or error when fetching suggested model
outputFileSync(join(path, 'meta.json'), JSON.stringify(meta, null, 2))
})
}
1 change: 1 addition & 0 deletions src/types/Template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export interface MetaFile {
LayoutTemplate?: string | null
HtmlBody?: string
TextBody?: string
TestRenderModel?: any
}

export interface MetaFileTraverse {
Expand Down

0 comments on commit 967140b

Please # to comment.