-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
83 lines (66 loc) · 1.66 KB
/
gulpfile.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
var gulp = require('gulp');
var fs = require('fs');
var loader = require('gulp-load-plugins');
var plugins = loader({
scope: 'devDependencies',
replaceString: 'gulp-',
camelize: true
});
var through = require('through2');
/**
* Functions
*/
function matchRegEx(content) {
var regex = /{{(.+?)}}/gi;
var match = content.match(regex);
return match;
}
function injectMJML(content, callback) {
var match = matchRegEx(content);
var filename = '';
if (match === null) {
callback(content);
return;
}
filename = match[0].replace('{{', '').replace('}}', '');
fs.readFile('src/templates/' + filename, 'utf8', function(err, data) {
if(err) {
throw err;
}
var regex = RegExp('{{' + filename + '}}', 'g');
var result = content.replace(regex, data);
injectMJML(result, callback);
});
}
function mjmlTemplates() {
var stream = null;
stream = through.obj(function(file, enc, callback) {
var that = this;
var content = file.contents.toString('utf8');
if (file.isBuffer() === false) {
callback();
return;
}
injectMJML(content, function(finalContent) {
file.contents = new Buffer(finalContent);
that.push(file);
callback();
});
});
return stream;
}
gulp.task('mjml', function() {
return gulp.src('src/*.mjml')
.pipe(mjmlTemplates())
.pipe(plugins.mjml())
.pipe(gulp.dest('dist/'));
});
function changeEvent(evt) {
plugins.util.log(plugins.util.colors.cyan(evt.path.substring(evt.path.lastIndexOf('\\') + 1), 'was', plugins.util.colors.magenta(evt.type)));
}
gulp.task('watch', ['mjml'], function() {
gulp.watch('src/**/*.mjml', ['mjml']).on('change', function(evt) {
changeEvent(evt);
});
});
gulp.task('default', ['mjml']);