diff --git a/LICENSE.md b/LICENSE.md index 9652b97..723ab10 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -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 diff --git a/package-lock.json b/package-lock.json index 563a2e5..66b9f17 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "postmark-cli", - "version": "1.3.6", + "version": "1.4.6", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 319694d..9735948 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/commands/templates/preview.ts b/src/commands/templates/preview.ts index a2c6fb1..3b8bf49 100644 --- a/src/commands/templates/preview.ts +++ b/src/commands/templates/preview.ts @@ -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) @@ -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) diff --git a/src/commands/templates/pull.ts b/src/commands/templates/pull.ts index c88bd4e..4182dc3 100644 --- a/src/commands/templates/pull.ts +++ b/src/commands/templates/pull.ts @@ -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 @@ -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)) @@ -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 }), @@ -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)) + }) } diff --git a/src/types/Template.ts b/src/types/Template.ts index 5c740c6..09905c2 100644 --- a/src/types/Template.ts +++ b/src/types/Template.ts @@ -83,6 +83,7 @@ export interface MetaFile { LayoutTemplate?: string | null HtmlBody?: string TextBody?: string + TestRenderModel?: any } export interface MetaFileTraverse {