Skip to content

Commit 967140b

Browse files
Merge pull request #22 from wildbit/feature/template-models
Template model storage
2 parents e49b11a + da216d1 commit 967140b

6 files changed

Lines changed: 31 additions & 9 deletions

File tree

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2019 [Wildbit](https://wildbit.com)
3+
Copyright (c) 2020 [Wildbit](https://wildbit.com)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postmark-cli",
3-
"version": "1.3.6",
3+
"version": "1.4.6",
44
"description": "A CLI tool for managing templates, sending emails, and fetching servers on Postmark.",
55
"main": "./dist/index.js",
66
"dependencies": {

src/commands/templates/preview.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,11 @@ const preview = (serverToken: string, args: TemplatePreviewArguments) => {
134134
if (layout && !layout.HtmlBody)
135135
return renderTemplateInvalid(res, layoutError)
136136

137+
const { TemplateType, TestRenderModel } = template
137138
const payload = {
138139
HtmlBody: getSource('html', template, layout),
139-
TemplateType: template.TemplateType,
140+
TemplateType,
141+
TestRenderModel,
140142
}
141143

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

162+
const { TemplateType, TestRenderModel } = template
160163
const payload = {
161164
TextBody: getSource('text', template, layout),
162-
TemplateType: template.TemplateType,
165+
TemplateType,
166+
TestRenderModel,
163167
}
164168

165169
return validateTemplateRequest('text', payload, res)

src/commands/templates/pull.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ const processTemplates = (options: ProcessTemplatesOptions) => {
157157
requestCount++
158158

159159
// Save template to file system
160-
saveTemplate(outputDir, response)
160+
saveTemplate(outputDir, response, client)
161161
totalDownloaded++
162162

163163
// Show feedback when finished saving templates
@@ -185,7 +185,7 @@ const processTemplates = (options: ProcessTemplatesOptions) => {
185185
* Save template
186186
* @return An object containing the HTML and Text body
187187
*/
188-
const saveTemplate = (outputDir: string, template: Template) => {
188+
const saveTemplate = (outputDir: string, template: Template, client: any) => {
189189
outputDir =
190190
template.TemplateType === 'Layout' ? join(outputDir, '_layouts') : outputDir
191191
const path: string = untildify(join(outputDir, template.Alias))
@@ -202,7 +202,7 @@ const saveTemplate = (outputDir: string, template: Template) => {
202202
outputFileSync(join(path, 'content.txt'), template.TextBody)
203203
}
204204

205-
const meta: MetaFile = {
205+
let meta: MetaFile = {
206206
Name: template.Name,
207207
Alias: template.Alias,
208208
...(template.Subject && { Subject: template.Subject }),
@@ -212,5 +212,22 @@ const saveTemplate = (outputDir: string, template: Template) => {
212212
}),
213213
}
214214

215-
outputFileSync(join(path, 'meta.json'), JSON.stringify(meta, null, 2))
215+
// Save suggested template model
216+
client
217+
.validateTemplate({
218+
...(template.HtmlBody && { HtmlBody: template.HtmlBody }),
219+
...(template.TextBody && { TextBody: template.TextBody }),
220+
...meta,
221+
})
222+
.then((result: any) => {
223+
meta.TestRenderModel = result.SuggestedTemplateModel
224+
})
225+
.catch((error: any) => {
226+
log('Error fetching suggested template model', { error: true })
227+
log(error, { error: true })
228+
})
229+
.then(() => {
230+
// Save the file regardless of success or error when fetching suggested model
231+
outputFileSync(join(path, 'meta.json'), JSON.stringify(meta, null, 2))
232+
})
216233
}

src/types/Template.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export interface MetaFile {
8383
LayoutTemplate?: string | null
8484
HtmlBody?: string
8585
TextBody?: string
86+
TestRenderModel?: any
8687
}
8788

8889
export interface MetaFileTraverse {

0 commit comments

Comments
 (0)