forked from ActiveCampaign/mailmason
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
460 lines (416 loc) · 14.1 KB
/
Gruntfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
module.exports = function(grunt) {
const sass = require('node-sass')
require('load-grunt-tasks')(grunt, { pattern: ['grunt-*', 'assemble'] })
require('time-grunt')(grunt)
grunt.loadNpmTasks('grunt-ftp-deploy')
const path = {
css_src: 'src/stylesheets/',
dist: 'dist/',
dist_html_glob: 'dist/**/*.html',
dist_text_glob: 'dist/**/*.txt',
templates: 'src/templates/',
layouts: 'src/layouts/',
partials: 'src/partials/',
images_src: 'src/images',
}
/* Configuration
================================================= */
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
/* secrets.json and config.json simplify variable management to keep the
common changes centralized to key files instead of being littered
throughout the Gruntfile. This also makes it easy to .gitignore secrets
================================================= */
secret: grunt.file.exists('secrets.json')
? grunt.file.readJSON('secrets.json')
: {},
config: grunt.file.readJSON('config.json'),
/* SASS
------------------------------------------------- */
sass: {
options: {
implementation: sass,
},
styles: {
expand: true,
cwd: path.css_src,
ext: '.css',
src: ['*.scss'],
dest: `${path.dist}stylesheets/`,
},
},
/* Autoprefixer
------------------------------------------------- */
autoprefixer: {
options: {
browsers: ['last 6 versions', 'ie >= 9'],
},
styles: {
expand: true,
cwd: `${path.dist}styles/`,
ext: '.css',
src: ['*.css'],
dest: `${path.dist}styles/`,
},
},
/* Assemble
Assembles our handlebars templates. Documentation might be incompatible since the latest version uses an entirely different build system.
See: https://github.com/assemble/assemble/
------------------------------------------------- */
assemble: {
options: {
layoutdir: path.layouts,
partials: `${path.partials}*.hbs`,
flatten: true,
sender_name: '<%= config.strings.sender_name %>',
product_name: '<%= config.strings.product_name %>',
product_url: '<%= config.strings.product_url %>',
credit_card_statement_name:
'<%= config.strings.credit_card_statement_name %>',
formal_company_name: '<%= config.strings.formal_company_name %>',
address_line_1: '<%= config.strings.address_line_1 %>',
address_line_2: '<%= config.strings.address_line_2 %>',
city: '<%= config.strings.city %>',
state: '<%= config.strings.state %>',
country: '<%= config.strings.country %>',
phone: '<%= config.strings.phone %>',
images_url: '<%= config.images.images_url %>',
use_images: '<%= config.images.use_images %>',
logo_file: '<%= config.images.logo_file %>',
logo_width: '<%= config.images.logo_width %>',
use_social_circles: '<%= config.images.use_social_circles %>',
twitter_url: '<%= config.images.twitter_url %>',
facebook_url: '<%= config.images.facebook_url %>',
pinterest_url: '<%= config.images.pinterest_url %>',
instagram_url: '<%= config.images.instagram_url %>',
dribbble_url: '<%= config.images.dribbble_url %>',
google_plus_url: '<%= config.images.google_plus_url %>',
youtube_url: '<%= config.images.youtube_url %>',
linkedin_url: '<%= config.images.linkedin_url %>',
},
generic: {
expand: true,
cwd: path.templates,
src: ['**/*.hbs'],
dest: `${path.dist}compiled`,
},
postmark: {
expand: true,
cwd: path.dist,
src: ['postmark-templates/**/*.hbs', 'postmark-layouts/**/*.hbs'],
dest: path.dist,
},
},
/* Inline
Brings external resources in the HTML files. Basically just takes our CSS file and inserts it as a style block.
See: https://github.com/chyingp/grunt-inline
------------------------------------------------- */
inline: {
postmark: {
expand: true,
cwd: path.dist,
ext: '.html',
src: ['postmark-layouts/**/*.html'],
dest: path.dist,
},
generic: {
expand: true,
cwd: path.dist,
ext: '.html',
src: ['compiled/**/*.html'],
dest: path.dist,
},
},
/* Prettify
Formats our template HTML files.
See: https://github.com/jonschlinkert/grunt-prettify
------------------------------------------------- */
prettify: {
postmark: {
expand: true,
cwd: path.dist,
ext: '.html',
src: ['postmark-layouts/**/*.html'],
dest: path.dist,
},
generic: {
expand: true,
cwd: path.dist,
ext: '.html',
src: ['compiled/**/*.html'],
dest: path.dist,
},
},
/* Premailer
Adds all sorts of email magic. Adds inline styles to each element based off class. Also generates plain text versions and allows us to control commonly used table attributes via CSS.
See: https://github.com/dwightjack/grunt-premailer
------------------------------------------------- */
premailer: {
options: {
warnLevel: 'none',
preserveStyles: true,
preserveStyleAttribute: true,
},
// genericHtml: {
// files: [
// {
// expand: true,
// src: [`${path.dist}compiled/**/*.html`],
// dest: '',
// },
// ],
// },
txt: {
options: {
mode: 'txt',
lineLength: 16384,
},
files: [
{
expand: true,
src: [path.dist_html_glob],
dest: '',
ext: '.txt',
},
],
},
},
/* FTP Deploy
Deploy assets to FTP
------------------------------------------------- */
'ftp-deploy': {
build: {
auth: {
host: '<%= config.ftp.host %>',
port: '<%= config.ftp.port %>',
username: '<%= secret.ftp.username %>',
password: '<%= secret.ftp.password %>',
},
src: '<%= config.ftp.src %>',
dest: '<%= config.ftp.dest %>',
exclusions: [
'path/to/source/folder/**/.DS_Store',
'path/to/source/folder/**/Thumbs.db',
],
},
},
/* S3
Pushes image assets to an Amazon S3 bucket.
See: https://github.com/jpillora/grunt-aws
------------------------------------------------- */
s3: {
options: {
accessKeyId: '<%= secret.s3.access_key_id %>',
secretAccessKey: '<%= secret.s3.secret_access_key %>',
bucket: '<%= config.s3.bucket %>',
region: '<%= config.s3.region %>',
overwrite: '<%= config.s3.overwrite %>',
},
build: {
cwd: path.images_src,
src: '**',
},
},
/* Watch
Watches all files in the src directory for changes.
------------------------------------------------- */
watch: {
options: {
livereload: 3000,
},
files: ['src/**/*', './preview.html'],
tasks: ['default'],
},
/* Replace
------------------------------------------------- */
replace: {
// Replace handlebars body placeholder with Postmark's content placeholder
postmarkPlaceholder: {
src: [`${path.dist}postmark-layouts/**/content.hbs`],
overwrite: true,
replacements: [
{ from: /({{)(.?body.?)(}})/g, to: '\\{{{ @content }}}' },
],
},
// Insert mustachio syntax on Postmark layout preheaders
postmarkPreheader: {
src: [`${path.dist}postmark-layouts/**/content.hbs`],
overwrite: true,
replacements: [
{ from: /({{)(.?pm:preheader.?)(}})/g, to: '\\{{#preheader}}<span class="preheader">\\{{.}}</span>\\{{/preheader}}' },
],
},
// Remove handlebars config
postmarkConfig: {
src: [`${path.dist}postmark-templates/**/content.hbs`],
overwrite: true,
replacements: [{ from: /(---)(.*)(---)/s, to: '' }],
},
// Premailer escapes URLs, so our mustachio variables for URLs get fully escaped, and the
// URL variables need to be converted from %7B%7Bsomething%7D%7D to {{something}}
variableSyntax: {
src: [path.dist_text_glob],
overwrite: true,
replacements: [
{ from: '%7B%7B%7B', to: '{{{ ' },
{ from: '%7D%7D%7D', to: ' }}}' },
{ from: '%7D%7D%7D%22', to: ' }}}' },
{ from: '%7B%7B', to: '{{ ' },
{ from: '%7D%7D', to: ' }}' },
{ from: '%7D%7D%22', to: ' }}' },
{ from: '%20', to: '' },
],
},
// Add some additional attributes that grunt inline removed
styleBlock: {
src: [path.dist_html_glob],
overwrite: true,
replacements: [
{
from: '<style>',
to: '<style type="text/css" rel="stylesheet" media="all">',
},
],
},
},
/* Copy
This is mainly used to copy compiled templates to a new directory for the CSS to be inlined before being tested.
See: https://github.com/gruntjs/grunt-contrib-copy
------------------------------------------------- */
copy: {
postmarkTemplates: {
files: [
{
expand: true,
cwd: 'src/templates/',
src: ['**/*'],
dest: `${path.dist}postmark-templates`,
},
],
},
postmarkLayouts: {
files: [
{
expand: true,
cwd: 'src/layouts/',
src: ['**/*'],
dest: `${path.dist}postmark-layouts`,
},
],
},
},
/* Spamcheck
Sends all of our HTML files through Postmark’s spamcheck API.
See: https://github.com/wildbit/grunt-spamcheck
------------------------------------------------- */
spamcheck: {
emails: {
src: [path.dist_html_glob],
},
},
/* Postmark
Sends test emails through Postmark. Add and remove template targets as needed.
See: https://github.com/wildbit/grunt-postmark
------------------------------------------------- */
postmark: {
options: {
serverToken: '<%= secret.postmark.server_token %>',
from: '<%= config.postmark.from %>',
to: '<%= config.postmark.to %>',
subject: '<%= config.postmark.subject %>',
},
// run "grunt postmark:welcome" - Sends just the welcome email
welcome: {
src: 'dist/compiled/welcome/content.html',
},
// run "npm run flood" - Sends all of the emails. Be careful not to spam PM if you have a bunch of emails.
flood: {
src: ['dist/compiled/**/content.html'],
},
// run "npm run litmus" - Add a litmus test address here.
litmus: {
to: '<%= config.strings.litmus_email %>',
src: 'dist/compiled/user-invitation/content.html',
},
},
/* Shell
Used for Pushing templates to Postmark via the postmark-cli tool.
The reason we use Grunt to execute this is so that we can easily pass the server tokens in from the secrets.json.
See: https://github.com/sindresorhus/grunt-shell
------------------------------------------------- */
shell: {
options: {
stderr: true,
execOptions: {
env: {
'POSTMARK_SERVER_TOKEN': '<%= secret.postmark.server_token %>'
}
}
},
postmarkPush: {
command:
'postmark templates push ./dist',
},
},
/**
* Used for cleaning up unneeded files and directories
* https://github.com/gruntjs/grunt-contrib-clean
------------------------------------------------- */
clean: {
dist: {
src: [path.dist],
},
hbs: {
src: ['dist/**/*.hbs'],
},
},
})
/* Tasks
================================================= */
grunt.registerTask('default', [
'clean:dist',
'css',
'postmark-templates',
'generic-templates',
])
/**
* Compiles Postmark compatible layouts and templates into separate files.
* Ideal if Postmark is your email service provider.
* CSS is not inlined since Postmark handles this for you.
*/
grunt.registerTask('postmark-templates', [
'copy:postmarkTemplates', // Copy Handlebars templates to dist folder
'copy:postmarkLayouts', // Copy Handlebars layouts to dist folder
'replace:postmarkPlaceholder', // Replace handlebars {{body}} with Postmark content placeholder
'replace:postmarkConfig', // Remove handlebars config from layouts
'replace:postmarkPreheader', // Insert mustachio syntax on Postmark layout preheaders
'assemble:postmark', // Compile handlebars templates and layouts. Mainly used for loading partials into the templates.
'inline:postmark', // Load external CSS into layouts
'replace:styleBlock', // Add properties to style block
'prettify:postmark', // Format layout HTML
'premailer:txt', // Generate text versions
'replace:variableSyntax', // Clean up variable syntax in text versions
'clean:hbs', // Remove handlebars files
])
/**
* Compiles templates and layouts to dist/compiled.
* Each template file includes the combined layout with inlined CSS.
* Ideal if you don't use Postmark as an email service provider.
*/
grunt.registerTask('generic-templates', [
'assemble:generic', // Compile handlebars templates to HTML
'inline:generic', // Load external CSS into templates
'premailer', // Inline CSS and generate text version
'replace:variableSyntax', // Clean up variable syntax in text versions
'replace:styleBlock', // Add properties to style block
'prettify:generic', // Format HTML
])
grunt.registerTask('css', ['sass', 'autoprefixer'])
grunt.registerTask('images-ftp', ['ftp-deploy'])
grunt.registerTask('images-s3', ['s3'])
grunt.registerTask('deploy', ['default', 'shell:postmarkPush'])
// Testing
grunt.registerTask('spam', ['spamcheck'])
grunt.registerTask('litmus', ['default', 'postmark:litmus'])
grunt.registerTask('flood', ['default', 'postmark:flood'])
}