-
Notifications
You must be signed in to change notification settings - Fork 4
Add the ability for mailmason to generate and upload/update templates.
#3
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
Changes from 28 commits
9dffdfa
abb4d27
86c4d29
e8ae024
4057b64
d988011
aac24c8
0873473
bcef316
71ccf91
d3331ca
a48658c
a650a90
d87365d
bb51efc
3967d29
82fb13c
a0a6f6d
8fb62e5
a518367
6131196
ac17e37
f3a7f7e
4a0b60c
aa3194b
90ec805
315fd19
19fc6c4
253a587
ee632f2
b7e7a42
2cfb716
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| node_modules | ||
| .idea | ||
| secrets.json | ||
| config.json | ||
| secrets.json |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,35 +3,62 @@ | |
| * https://github.com/wildbit/grunt-postmark.git | ||
| */ | ||
|
|
||
| module.exports = function(grunt) { | ||
| module.exports = function (grunt) { | ||
| var secret = grunt.file.readJSON('secrets.json'); | ||
| var config = grunt.file.readJSON('config.json'); | ||
|
|
||
| grunt.initConfig({ | ||
| secrets: grunt.file.readJSON('secrets.json'), | ||
| secret: secret, | ||
| config: config, | ||
|
|
||
| /* Postmark | ||
| ------------------------------------------------- */ | ||
| ------------------------------------------------- */ | ||
| postmark: { | ||
| options: { | ||
| serverToken: '<%= secrets.serverToken %>' | ||
| serverToken: "<%= secret.postmark.server_token %>", | ||
| }, | ||
| email: { | ||
| from: '[email protected]', | ||
| to: '[email protected]', | ||
| subject: 'Yo', | ||
| from: "<%= config.postmark.from %>", | ||
| to: "<%= config.postmark.to %>", | ||
| subject: "<%= config.postmark.subject %>", | ||
| src: ['test/email.html'] | ||
| }, | ||
| bulk: { | ||
| from: '[email protected]', | ||
| to: '[email protected]', | ||
| subject: 'Hey', | ||
| from: "<%= config.postmark.from %>", | ||
| to: "<%= config.postmark.to %>", | ||
| subject: "<%= config.postmark.subject %>", | ||
| src: ['test/*.html'] | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| // you can either specify the template configuration here, or in templates.json | ||
| 'postmark-templates-upload': config.templates && config.templates | ||
| ? config.templates.output_file || config.templates.file | ||
| : { | ||
| test_email: { | ||
| name: 'testing-postmark-templates-js1-' + new Date().valueOf(), | ||
| subject: 'Testing grunt-postmark-templates', | ||
| htmlBody: 'test/email.html', | ||
|
||
| textBody: 'test/email.txt', | ||
| }, | ||
| test_email_again: { | ||
| name: 'testing-postmark-templates-js2-' + new Date().valueOf(), | ||
| subject: 'Testing grunt-postmark-templates', | ||
| htmlBody: 'test/email.html', | ||
| textBody: 'test/email.txt', | ||
| } | ||
| }, | ||
|
|
||
| 'postmark-templates-output': { | ||
| options: { | ||
| outputFile: '<%= config.templates.output_file || config.templates.file %>', | ||
| cleanOutput: '<%= config.templates.clean_output %>' | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| grunt.loadTasks('tasks'); | ||
|
|
||
| grunt.registerTask('default', ['postmark']); | ||
| grunt.registerTask('default', ['postmark', 'postmark-templates']); | ||
|
|
||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,8 @@ After the plugin is installed, it can be enabled in your Gruntfile: | |
| grunt.loadNpmTasks('grunt-postmark'); | ||
| ``` | ||
|
|
||
| You'll need to add a [`config.json`](https://github.com/wildbit/mailmason/wiki/Getting-Started#create-configjson-required) and a [`secrets.json`](https://github.com/wildbit/mailmason/wiki/Getting-Started#create-secretsjson-optional) per the `mailmason` configuration. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are several details in this wiki entry that aren't relevant to grunt-postmark so this might confuse people who aren't using MailMason. We should replace this with setup instructions specific to grunt-postmark. I can create the wiki page and link it once we've merged this PR. |
||
|
|
||
| ## Postmark task | ||
| _Run this task with the `grunt postmark` command._ | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // 1. Copy this file to "config.json" for your own version that won't be | ||
| // tracked in source control. | ||
| // | ||
| // 2. Delete these comments from the configuration so Grunt doesn't get confused | ||
| // | ||
| // 3. Visit the following page for details: | ||
|
||
| // https://github.com/wildbit/mailmason/wiki/Getting-Started#configjson | ||
| { | ||
| "postmark": { | ||
| "from": "[email protected]", | ||
| "to": "[email protected]", | ||
| "subject": "Test Email" | ||
| }, | ||
| "templates": { | ||
| "file": "templates.json" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // 1. Copy this file to "secrets.json" for your own version that won't be | ||
| // tracked in source control. | ||
| // | ||
| // 2. Delete these comments from the configuration so Grunt doesn't get confused | ||
| // | ||
| // 3. Visit the following page for details: | ||
|
||
| // https://github.com/wildbit/mailmason/wiki/Getting-Started#secretsjson | ||
| { | ||
| "postmark": { | ||
| "server_token": "YOUR_POSTMARK_SERVER_TOKEN_HERE" | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* | ||
| * grunt-postmark-templates | ||
| * push templates to a Postmark server for use with SendTemplatedEmail | ||
| * | ||
| * https://github.com/wildbit/grunt-postmark.git | ||
| */ | ||
|
|
||
| module.exports = function (grunt) { | ||
| grunt.registerMultiTask('postmark-templates-upload', 'Create or update Postmark templates', function () { | ||
| var done = this.async(); | ||
| var options = this.options(); | ||
| var template = this.data; | ||
|
|
||
| var serverToken = options.serverToken || grunt.config('secret.postmark.server_token'); | ||
|
|
||
| if (!serverToken) { | ||
| grunt.fail.warn('Missing Postmark server token \n'); | ||
| } | ||
|
|
||
| if (!template.Name) { | ||
|
||
| grunt.fail.warn('Missing required template property "Name" \n'); | ||
| } | ||
|
|
||
| if (!template.Subject) { | ||
| grunt.fail.warn('Missing required template property "Subject" \n'); | ||
| } | ||
|
|
||
| if (!template.HtmlBody) { | ||
| grunt.log.error('Missing template property "HtmlBody" \n'); | ||
| } | ||
|
|
||
| if (!template.TextBody) { | ||
| grunt.log.error('Missing template property "TextBody" \n'); | ||
| } | ||
|
|
||
| var postmark = require('postmark'); | ||
| var client = new postmark.Client(serverToken); | ||
|
|
||
| // read the referenced files, but hold on to the original filenames | ||
| var expanded = Object.assign({}, template); | ||
|
|
||
| if (template.TemplateId) { | ||
| client.editTemplate(template.TemplateId, expanded, function (err, response) { | ||
| if (err && err.code === 1101) { | ||
| grunt.log.warn('Template ' + template.TemplateId + ' not found, so attempting create'); | ||
| delete template.TemplateId; | ||
| delete expanded.TemplateId; | ||
| client.createTemplate(expanded, function (err, response) { | ||
| grunt.log.writeln('Template ' + template.Name + ' created: ' + JSON.stringify(response.TemplateId)); | ||
| handleResponse(err, response, done, template); | ||
| }); | ||
| } else { | ||
| grunt.log.writeln('Template ' + template.Name + ' updated: ' + JSON.stringify(response.TemplateId)); | ||
| handleResponse(err, response, done, template); | ||
| } | ||
| }); | ||
| } else { | ||
| client.createTemplate(expanded, function (err, response) { | ||
| grunt.log.writeln('Template ' + template.Name + ' created: ' + JSON.stringify(response.TemplateId)); | ||
| handleResponse(err, response, done, template); | ||
| }); | ||
| } | ||
|
|
||
| }); | ||
|
|
||
| function handleResponse(err, response, done, template) { | ||
| if (err){ | ||
| errorMessage(err); | ||
| done(); | ||
| } else { | ||
| template.TemplateId = response.TemplateId; | ||
| // compile the templates for use by the `postmark-templates-output` task | ||
| var updatedTemplates = grunt.config.get('updatedTemplates') || {}; | ||
| updatedTemplates[template.Name] = template; | ||
| grunt.config.set('updatedTemplates', updatedTemplates); | ||
|
|
||
| done(); | ||
| } | ||
| } | ||
|
|
||
| function errorMessage(err) { | ||
| if (err.message) { | ||
| grunt.log.warn('Error: ' + err.message); | ||
| } else { | ||
| grunt.log.warn('Error: ' + JSON.stringify(err)); | ||
| } | ||
| } | ||
|
|
||
| // invoke this task after postmark-templates to get an output file containing the resulting template IDs | ||
| // this is in the same format as the postmark-templates config. | ||
|
|
||
| grunt.registerTask('postmark-templates-output', 'Write out the resulting template IDs', function () { | ||
| var options = this.options({ | ||
| cleanOutput: false | ||
| }); | ||
| var updatedTemplates = grunt.config('updatedTemplates'); | ||
| var oldTemplates = grunt.file.read(options.outputFile); | ||
|
|
||
| Object.keys(updatedTemplates).forEach(function (updatedTemplateKey) { | ||
| updatedTemplates[updatedTemplateKey] = Object.assign( | ||
| oldTemplates[updatedTemplateKey] || {}, | ||
| updatedTemplates[updatedTemplateKey] | ||
| ); | ||
|
|
||
| if (options.cleanOutput) { | ||
| delete updatedTemplates[updatedTemplateKey].HtmlBody; | ||
| delete updatedTemplates[updatedTemplateKey].TextBody; | ||
| } | ||
| }); | ||
|
|
||
| grunt.file.write(options.outputFile, JSON.stringify(updatedTemplates, null, 2)); | ||
| grunt.log.writeln("Updated template information written to " + options.outputFile); | ||
|
|
||
| }); | ||
|
|
||
| // you can also get a JSON report of uploaded templates | ||
| grunt.registerTask('postmark-templates', ['postmark-templates-upload', 'postmark-templates-output']); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Hello from grunt-postmark-templates |

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optionally storing the template data in a separate JSON file is a good idea, but this might be a roadblock for people who are configuring this for the first time.
example_config.jsonreferencestemplates.jsonso I ran into issues during setup because it wasn't clear whether the plugin was using the data from the template config or from the target config.Instead of reading from
templates.jsonwe should remove this and document it as an optional step. This would reduce the friction during the initial setup process.