This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgulpfile.js
239 lines (210 loc) · 7.28 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
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
// Gulp packages
var gulp = require('gulp');
var sass = require('gulp-sass');
var fileinclude = require('gulp-file-include');
var inlineCss = require('gulp-inline-css');
var minifyHTML = require('gulp-minify-html');
var imagemin = require('gulp-imagemin');
var zip = require('gulp-zip');
var gulpIf = require('gulp-if');
var changed = require('gulp-changed');
var replace = require('gulp-replace');
var gutil = require('gulp-util');
var plumber = require('gulp-plumber');
var clipboard = require('gulp-clipboard');
var rename = require('gulp-rename');
var argv = require('yargs').argv;
var del = require('del');
var browserSync = require('browser-sync').create();
var nodemailer = require('nodemailer');
var fs = require('fs');
// Config files
var config = require('./gulp.config')();
// Local web server (Default localhost:8080)
// Pass argument --port=XXXX to change
gulp.task('connect', ['html'], function() {
browserSync.init({
// Serve files from the local directory
server: {
baseDir: config.localDir,
directory: true
},
port: argv.port ? argv.port : config.browsersync.port,
open: config.browsersync.open || (argv.open || (argv.o || false)),
notify: config.browsersync.notify
});
gulp.watch([config.sourcePath.sass, config.sourcePath.html], ['html']);
gulp.watch(config.sourcePath.images, ['images:local']);
gulp.watch(config.localFiles)
.on('change', browserSync.reload);
});
// Build CSS files
gulp.task('sass', function() {
log('Compiling SASS to CSS');
return gulp.src('source/stylesheets/*.scss')
.pipe(plumber({ errorHandler: handleError }))
.pipe(sass())
.pipe(gulp.dest(config.localDir + '/css'));
});
// Compile Layouts into HTML files
gulp.task('html', ['sass'], function() {
log('Compiling HTML Templates');
return gulp.src(config.sourcePath.layouts)
.pipe(fileinclude({
prefix: '{{ ',
suffix: ' }}',
basepath: '@file'
}))
.pipe(gulp.dest(config.localDir));
});
// Inline all CSS styles
// Minify HTML (Optional argument: --minify)
gulp.task('inline-css', ['html'], function() {
log('Moving CSS inline');
return gulp.src(config.localDir + '/*.html')
.pipe(inlineCss({
applyStyleTags: true,
applyLinkTags: true,
removeStyleTags: false,
removeLinkTags: true
}))
.pipe(gulpIf(argv.minify, minifyHTML({ conditionals: true, spare: true, quotes: true})))
.pipe(gulp.dest(config.productionDir));
});
// Copy Images folder
gulp.task('images:local', function () {
log('Copying images');
gulp.src(config.sourcePath.images)
.pipe(gulp.dest(config.localDir + '/images'));
});
// Copy Images folder and Minify for production
gulp.task('images:production', function () {
log('Minifying and Copying images');
return gulp.src(config.sourcePath.images)
.pipe(changed(config.productionDir + '/images'))
.pipe(imagemin({ progressive: true }))
.pipe(gulp.dest(config.productionDir + '/images'));
});
// Zip all files or images only
gulp.task('zip', ['images:production'], function () {
if (! argv.zip)
return;
log('Compressing images into zip file');
if(argv.zip == 'all') {
return gulp.src(config.productionDir + '/**/**')
.pipe(zip('all_files.zip'))
.pipe(gulp.dest(config.productionDir));
} else {
return gulp.src(config.productionDir + '/images/**/*')
.pipe(zip('images.zip'))
.pipe(gulp.dest(config.productionDir));
}
});
// Empty distribution folders
gulp.task('clean', function () {
log('Cleaning up generated files');
del([
config.productionDir + '/**/**',
config.localDir + '/**/**'
]);
});
// Copy a template to the clipboard
// Pass a template name as an argument --template=NAME or -t NAME
gulp.task('copy', function() {
var template = argv.template ? argv.template : (argv.t ? argv.t : null);
if (! template) {
return log('***ERROR***: Name of template is missing.\n', 'red');
}
// Copy to Clipboard
gulp.src(config.productionDir + '/' + template + '.html')
.pipe(clipboard());
return log('Copied ' + gutil.colors.magenta(template + '.html') + ' to clipboard.\n');
});
// Clone a Template
gulp.task('clone', function() {
if (! argv.from) {
return log('***ERROR***: You need to specify a source template.\n', 'red');
}
if (! argv.to) {
return log('***ERROR***: You need to specify a name for the new template.\n', 'red');
}
// Clone layout
gulp.src([config.sourceDir + '/layouts/' + argv.from + '.html'])
.pipe(rename(argv.to + '.html'))
.pipe(replace(argv.from, argv.to))
.pipe(gulp.dest(config.sourceDir + '/layouts/'));
// Clone partials
gulp.src([config.sourceDir + '/partials/' + argv.from + '/*'])
.pipe(gulp.dest(config.sourceDir + '/partials/' + argv.to));
return gutil.log('Cloned to ' + gutil.colors.magenta(argv.to) + ' successfully.\n');
});
// Remove a Template
gulp.task('remove', function() {
var template = argv.template ? argv.template : (argv.t ? argv.t : null);
if (! template) {
return log('***ERROR***: Name of template is missing.\n', 'red');
}
// Delete from source directory and build directories
del([
config.sourceDir + '/layouts/' + template + '.html',
config.sourceDir + '/partials/' + template,
config.productionDir + '/' + template + '.html',
config.localDir + '/' + template + '.html'
]);
return log('Removed template ' + gutil.colors.magenta(template) + ' successfully.\n');
});
// Send test emails
gulp.task('mail', function() {
var template = argv.template ? argv.template : (argv.t ? argv.t : null);
if (! template) {
return log('***ERROR***: Name of template is missing\n', 'red');
}
// Nodemailer
var transporter = nodemailer.createTransport(config.nodemailer.transportOptions);
var mailOptions = config.nodemailer.mailOptions;
// Update config values
mailOptions.to = argv.to ? argv.to : config.nodemailer.mailOptions.to;
mailOptions.subject = argv.subject ? argv.subject : config.nodemailer.mailOptions.subject;
// get template contents and send email
fs.readFile(config.productionDir + '/' + template + '.html', 'utf8', function(err, data) {
if(err) {
handleError(err);
}
var regExp = /(\.\.)?\/?images\//g;
mailOptions.html = data.replace(regExp, config.nodemailer.imageHost);
// Send the email
transporter.sendMail(mailOptions, function(err, info){
if(err) {
handleError(err);
}
log('Test email for template ' + gutil.colors.magenta(template) + ' sent successfully \n');
});
});
});
/* Tasks */
// Build for local and start browsersync server
gulp.task('serve', ['sass', 'html', 'images:local', 'connect']);
// Build for Production
gulp.task('build', ['sass', 'html', 'inline-css', 'images:production', 'zip']);
// Default
gulp.task('default', ['serve']);
/* Global functions */
// Injects custom messages into stream
function log(msg, color) {
var msgColor = color ? gutil.colors[color] : gutil.colors.blue;
if (typeof(msg) === 'object') {
for (var item in msg) {
if(msg.hasOwnProperty(item)) {
gutil.log(msgColor(msg[item]));
}
}
} else {
gutil.log(msgColor(msg));
}
}
// Handles error without breaking stream
function handleError(err) {
gutil.beep();
log(err.toString(), 'red');
this.emit('end');
}