-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
executable file
·166 lines (146 loc) · 3.67 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
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
// Include gulp
var gulp = require('gulp');
var gulpSass = require('gulp-sass')(require('node-sass'));
var rename = require('gulp-rename');
var autoprefixer = require('gulp-autoprefixer');
var inliner = require('gulp-inline-css');
var inlinesource = require('gulp-inline-source');
var imagemin = require('gulp-imagemin');
var cache = require('gulp-cached');
var plumber = require('gulp-plumber');
var browserSync = require('browser-sync').create();
var argv = require('minimist')(process.argv.slice(2), {
string: [
'template'
, 'recipient'
]
, boolean: [
'litmus'
]
, alias: {
t: 'template'
, r: 'recipient'
, l: 'litmus'
}
});
var nodemailer = require('nodemailer');
var fs = require('fs');
var config = require('./config.json');
// Compile Our Sass
function sass() {
return gulp.src('src/scss/*.scss')
.pipe(plumber({
errorHandler: function(err) {
console.log(err);
this.emit('end');
}
}))
.pipe(gulpSass({
outputStyle: 'compressed'
, errLogToConsole: true
}))
.pipe(autoprefixer('last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'))
.pipe(gulp.dest('src/css'));
};
// Build our templates
function build() {
return gulp.src('src/html/*.html')
.pipe(plumber())
.pipe(cache('templates'))
.pipe(inlinesource({
compress: false
}))
.pipe(inliner({
removeStyleTags: false
}))
.pipe(gulp.dest('./output'))
.pipe(browserSync.stream());
};
// Image task
function images() {
return gulp.src('src/images/**/*')
.pipe(imagemin({
optimizationLevel: 3
, progressive: true
, interlaced: true
}))
.pipe(rename(function(path) {
path.basename = path.basename.replace('@2x', '');
return path;
}))
.pipe(gulp.dest('./output/images'));
};
// Watch Files For Changes
function watch() {
gulp.watch('src/scss/*.scss', gulp.series(sass, build)).on('change', function() {
// Clear cache so all html templates are rebuilt.
delete cache.caches['templates'];
});
gulp.watch('src/html/*.html', build);
gulp.watch('src/images/**/*', images);
};
// Default Task
function first_run() {
// Serve files from the root of this project
browserSync.init({
server: {
baseDir: "./output/"
}
});
return gulp.series(
sass
, build
, images
, watch
)();
};
// Email testing tasks
function send() {
//console.log(argv);
return sendEmail();
};
function sendEmail() {
try {
if (argv.template !== undefined) {
var template_path = "./output/" + argv.template;
var transporter = nodemailer.createTransport({
service: 'Mailgun'
, auth: {
user: config.auth.mailgun.user
, pass: config.auth.mailgun.pass
}
});
var template_content = fs.readFileSync(template_path, encoding = "utf8");
var mail_options = {
from: config.email.from
, to: argv.recipient || config.email.to
, subject: config.email.subject + ' - ' + argv.template
, html: template_content
};
if (argv.litmus) {
mail_options.to = config.email.litmus;
}
transporter.sendMail(mail_options, function(error, info) {
if (error) {
throw console.error(error);
} else {
console.log('Message sent: ' + info.response);
}
});
} else {
throw "Please define a template name e.g. gulp send -t email.html, gulp send --template email.html";
}
} catch (e) {
if (e.code == 'ENOENT') {
console.error("There was an error. Check your template name to make sure it exists in './output'.");
} else if(e instanceof TypeError) {
console.error('There was an error. Please check your config.json to make sure everything is spelt correctly.');
} else {
console.error(e);
}
}
}
exports.build = build;
exports.images = images;
exports.default = first_run;
exports.watch = watch;