Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9dffdfa
configuration for tests
hybernaut Dec 14, 2016
abb4d27
more test config cleanup
hybernaut Dec 14, 2016
86c4d29
basic grunt task implemented
hybernaut Dec 14, 2016
e8ae024
read template files from filesystem
hybernaut Dec 15, 2016
4057b64
remove debug
hybernaut Dec 15, 2016
d988011
TODO for validating htmlBody/htmlFile etc.
hybernaut Dec 15, 2016
aac24c8
pretty good support for creating a server
hybernaut Dec 15, 2016
0873473
make create server task idempotent
hybernaut Dec 16, 2016
bcef316
refactor postmark-servers task using more idiomatic grunt usage
hybernaut Dec 16, 2016
71ccf91
duplicate test requires only one additional iteration, not two
hybernaut Dec 16, 2016
d3331ca
collect serverToken from secrets.json
hybernaut Dec 23, 2016
a48658c
read template config from templates.json
hybernaut Dec 23, 2016
a650a90
clean up example template files
hybernaut Dec 23, 2016
d87365d
simplify configuration
hybernaut Dec 23, 2016
bb51efc
use more canonical htmlBody and textBody
hybernaut Dec 23, 2016
3967d29
read templates.json in the postmark-templates task
hybernaut Dec 23, 2016
82fb13c
checkpoint: making progress toward saving templateID output
hybernaut Jan 3, 2017
a0a6f6d
output template info (including IDs) in templates.json format
hybernaut Jan 4, 2017
8fb62e5
update existing templates if templateID is present
hybernaut Jan 4, 2017
a518367
if update fails with bad Id, then revert to create
hybernaut Jan 4, 2017
6131196
move postmark-servers task to separate PR
hybernaut Jan 4, 2017
ac17e37
remove references to postmark-servers (for another PR)
hybernaut Jan 4, 2017
f3a7f7e
one last comment
hybernaut Jan 4, 2017
4a0b60c
node.js style callback
hybernaut Jan 13, 2017
aa3194b
simplify configuration following suggestions from @derekrushforth
hybernaut Jan 13, 2017
90ec805
log "created" vs "updated"
hybernaut Jan 13, 2017
315fd19
sample Gruntfile reads templates.json or defaults to inline config
hybernaut Jan 13, 2017
314332b
older WIP
Nov 18, 2018
53e7131
improve error handling on createTemplate
Jan 2, 2019
394f7bc
improve error handling
Jan 3, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.idea
secrets.json
secrets.json
templates-output.json
31 changes: 25 additions & 6 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
module.exports = function(grunt) {

grunt.initConfig({
secrets: grunt.file.readJSON('secrets.json'),
secrets: grunt.file.exists('secrets.json') ? grunt.file.readJSON('secrets.json') : {},

/* Postmark
------------------------------------------------- */
Expand All @@ -15,23 +15,42 @@ module.exports = function(grunt) {
serverToken: '<%= secrets.serverToken %>'
},
email: {
from: 'you@youremail.com',
to: 'you@youremail.com',
from: '<%= secrets.testSender %>',
to: '<%= secrets.testRecipient %>',
subject: 'Yo',
src: ['test/email.html']
},
bulk: {
from: 'you@youremail.com',
to: 'you@youremail.com',
from: '<%= secrets.testSender %>',
to: '<%= secrets.testRecipient %>',
subject: 'Hey',
src: ['test/*.html']
}
},

// you can either specify the template configuration here, or in templates.json
'postmark-templates': grunt.file.exists('templates.json') ? grunt.file.readJSON('templates.json') : {
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',
}
}

});

grunt.loadTasks('tasks');

grunt.registerTask('default', ['postmark']);
// you can also get a JSON report of uploaded templates (default filename: templates-output.json)
grunt.registerTask('templates-logged', ['postmark-templates', 'postmark-templates-output']);

grunt.registerTask('default', ['postmark', 'postmark-templates']);

};
6 changes: 4 additions & 2 deletions secrets_example.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"server_token": "POSTMARK_API_TEST"
}
"serverToken": "POSTMARK_API_TEST",
"testSender": "you@youremail.com",
"testRecipient": "you@youremail.com"
}
138 changes: 138 additions & 0 deletions tasks/grunt-postmark-templates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* grunt-postmark-templates
* push templates to a Postmark server for use with SendTemplatedEmail
*
* https://github.com/wildbit/grunt-postmark.git
*/

module.exports = function(grunt) {

'use strict';

grunt.registerMultiTask('postmark-templates', 'Create or update Postmark templates', function() {

var done = this.async();
var options = this.options();
var template = this.data;

var defaultLocale = 'en';

var serverToken = options.serverToken || grunt.config('secrets.serverToken') || grunt.config('secret.postmark.server_token');

if (!serverToken) {
grunt.fail.warn('Missing required option "serverToken" \n');
}

template.name = template.name || this.target;

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 && !template.htmlSrc) {
grunt.log.error('Missing template property "htmlBody" or "htmlSrc"\n');
}

if (!template.textBody && !template.textSrc) {
grunt.log.error('Missing template property "textBody" or "textSrc"\n');
}

var postmark = require('postmark');
var client = new postmark.Client(serverToken);

var locale = template.locale || defaultLocale;

// read the referenced files, but hold on to the original filenames
var expanded = {
Name: template.name,
Subject: template.subject,
HtmlBody: template.htmlBody || grunt.file.read(template.htmlSrc),
TextBody: template.textBody || grunt.file.read(template.textSrc),
TemplateId: template.templateId
};

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) {
if (err == null) {
grunt.log.writeln('Template ' + JSON.stringify(template.name) + ' created: ' + JSON.stringify(response.TemplateId));
} else {
grunt.log.writeln('Error creating template ' + template.name + ': ' + JSON.stringify(err));
}
});
} else if (!err && response.TemplateId) {
grunt.log.writeln('Template ' + template.name + ' updated: ' + JSON.stringify(response.TemplateId));
} else {
grunt.log.writeln('Error on createTemplate(' + template.name + '): ' + JSON.stringify(err) + JSON.stringify(response));
}

handleResponse(err, response, done, template);

});
} else {
client.createTemplate(expanded, function(err, response) {
if (!err && response.TemplateId) {
grunt.log.writeln('Template ' + expanded.Name + ' created: ' + JSON.stringify(response.TemplateId));
} else {
grunt.log.writeln('Error on createTemplate(' + expanded.name + '): ' + JSON.stringify(err) + JSON.stringify(response));

}
handleResponse(err, response, done, template);

});
}

});

function handleResponse(err, response, done, template) {
if (err){
errorMessage(err);
done();
} else {

template.templateId = response.TemplateId;
// append this record to the result array, used by postmark-templates-output task
var upd = grunt.config.get('updatedTemplates') || {};
var tname = template.name;
delete template.name;
upd[tname] = template;
grunt.config.set('updatedTemplates', upd);

done(template);
}
}

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', 'writes out the resulting template IDs', function() {

var options = this.options({
filename: "templates-output.json"
});

var results = grunt.config('updatedTemplates');

grunt.file.write(options.filename, JSON.stringify(results, null, 2));

grunt.log.writeln("Updated template information written to " + options.filename);

});

};
14 changes: 7 additions & 7 deletions tasks/grunt-postmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
* https://github.com/wildbit/grunt-postmark.git
*/

'use strict';

module.exports = function(grunt) {

'use strict';

grunt.registerMultiTask('postmark', 'Send emails through Postmark', function() {

var done = this.async();
var options = this.options();
var _data = this.data;

// Check for server token
if (!options.serverToken && !_data.serverToken) {
if (!options.serverToken && !_data.serverToken) {
grunt.fail.warn('Missing Postmark server token \n');
}

Expand All @@ -25,8 +25,8 @@ module.exports = function(grunt) {
if (this.files.length > 0) {

var message = {
'From': _data.from || options.from,
'To': _data.to || options.to,
'From': _data.from || options.from,
'To': _data.to || options.to,
'Subject': _data.subject || options.subject
};

Expand Down Expand Up @@ -56,7 +56,7 @@ module.exports = function(grunt) {
handleResponse(err, response, done);
});
}

} else {
// Warn about no files being passed to task
grunt.log.warn('No src file found \n');
Expand All @@ -66,7 +66,7 @@ module.exports = function(grunt) {


function handleResponse(err, response, done) {
err ? errorMessage(err) : successMessage(response);
var _ = err ? errorMessage(err) : successMessage(response);
done();
}

Expand Down
1 change: 1 addition & 0 deletions test/email.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello from grunt-postmark-templates