-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
486 lines (397 loc) · 13.3 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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//Gulp Packages =============================================
//var fs = require('fs');
var fs = require('fs-extra');
var app = JSON.parse(fs.readFileSync('./package.json'));
var ftp = JSON.parse(fs.readFileSync('./ftp.config'));
var gulp = require('gulp'),
webpack = require('gulp-webpack'),
sass = require('gulp-sass'),
sourcemaps = require('gulp-sourcemaps'),
browserSync = require('browser-sync'),
uglifycss = require('gulp-uglifycss'),
minifyInline = require('gulp-minify-inline'),
imagemin = require('gulp-imagemin'),
injectPartials = require('gulp-inject-partials'),
htmlclean = require('gulp-htmlclean'),
cleanCSS = require('gulp-clean-css'),
concat = require('gulp-concat'),
uglify = require('gulp-uglify'),
gm = require('gulp-gm'),
del = require('del'),
zip = require('gulp-zip'),
gutil = require('gulp-util'),
vinylFtp = require('vinyl-ftp');
var tf = {
zipSrc: 'tf/',
zipDist: 'tf-upload/',
root: 'tf/' + app.slug + '/',
app: 'tf/' + app.slug + '/app/'
};
var paths = {
src: 'app/src/**/*',
srcHTML: 'app/src/*.html',
srcCSS: 'app/src/assets/scss/app.scss',
srcJS: 'app/src/assets/js/app.js',
srcPhp: 'app/src/assets/php/**/*',
srcImages: 'app/src/assets/img/**/*',
srcVideos: 'app/src/assets/videos/**/*',
srcFonts: 'app/src/assets/fonts/**/*',
tmp: 'app/tmp',
tmpIndex: 'app/tmp/index.html',
tmpHTML: 'app/tmp/**/*.html',
tmpCSS: 'app/tmp/assets/css',
tmpJS: 'app/tmp/assets/js',
tmpPhp: 'app/tmp/assets/php',
tmpImages: 'app/tmp/assets/img',
tmpVideos: 'app/tmp/assets/videos',
tmpFonts: 'app/tmp/assets/fonts/',
dist: 'app/dist',
distIndex: 'app/dist/index.html',
distCSS: 'app/dist/assets/css/',
distJS: 'app/dist/assets/js/',
distPhp: 'app/dist/assets/php',
distImages: 'app/dist/assets/img',
distVideos: 'app/dist/assets/videos',
distFonts: 'app/dist/assets/fonts'
};
var bases = {
app: 'app/src/',
tmp: 'app/tmp/',
dist: 'app/dist/',
demoFolder: app.slug + '-' + app.version + '/'
};
var sassOptions = {
errLogToConsole: true,
outputStyle: 'expanded'
};
// html task =============================================
gulp.task('test', function () {
console.log(ftp.server.host);
});
// html task =============================================
gulp.task('views', function () {
return gulp.src(paths.srcHTML)
.pipe(injectPartials({
removeTags: true
}))
.pipe(gulp.dest(paths.tmp));
});
gulp.task('views-rebuild', gulp.series('views', function (done) {
browserSync.reload();
done();
}));
gulp.task('scripts', function () {
return gulp.src(paths.srcJS)
.pipe(webpack({
output: {
filename: 'app.js'
}
}
))
.pipe(browserSync.reload({stream: true}))
.pipe(gulp.dest(paths.tmpJS));
});
gulp.task('styles', function () {
return gulp.src(paths.srcCSS)
.pipe(sourcemaps.init())
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(sourcemaps.write())
.pipe(gulp.dest((paths.tmpCSS)))
.pipe(browserSync.reload({stream: true}));
});
gulp.task('images', function () {
return gulp.src(paths.srcImages)
.pipe(gulp.dest(paths.tmpImages));
});
gulp.task('fonts', function () {
return gulp.src(paths.srcFonts)
.pipe(gulp.dest(paths.tmpFonts));
});
gulp.task('php', function () {
return gulp.src(paths.srcPhp)
.pipe(gulp.dest(paths.tmpPhp));
});
gulp.task('videos', function () {
return gulp.src(paths.srcVideos)
.pipe(gulp.dest(paths.tmpVideos));
});
gulp.task('clean:tmp', function () {
return del(bases.tmp + '**', {force: true});
});
/*---------------------------------------
Command: gulp
Description: build dev version...
---------------------------------------- */
gulp.task('default', gulp.series(
'clean:tmp',
'styles',
'scripts',
'views',
'images',
'fonts',
'php',
'videos'
));
// Rerun the task when a file changes
gulp.task('watch', gulp.series('default', function () {
browserSync.init({
server: {
baseDir: bases.tmp
}
});
gulp.watch('app/src/assets/img/**/*.*', gulp.series('images'));
// gulp.watch('app/src/assets/fonts/**/*',gulp.series('copy'));
gulp.watch('app/src/assets/php/**/*', gulp.series('php'));
gulp.watch('app/src/assets/scss/**/*', gulp.series('styles'));
gulp.watch('app/src/assets/js/**/*', gulp.series('scripts'));
gulp.watch('app/src/*/*', gulp.series('views-rebuild'));
gulp.watch('app/src/*.html').on('change', function (file) {
return gulp.src(file)
.pipe(injectPartials({
removeTags: true,
ignoreError: true
}))
.pipe(gulp.dest(paths.tmp))
.pipe(browserSync.reload({stream: true}));
});
}));
/*-----------TASKS TO CREATE PRODUCTION VERSION---------- */
/*---------------------------------------
Command: gulp html:dist
Description: Inject partials then copy to dist
---------------------------------------- */
gulp.task('html:dist', gulp.series('views', function () {
return gulp.src(paths.tmpHTML)
.pipe(gulp.dest(paths.dist));
}));
gulp.task('html:dist-min', gulp.series('views', function () {
return gulp.src(paths.tmpHTML)
.pipe(minifyInline())
.pipe(htmlclean())
.pipe(gulp.dest(paths.dist));
}));
/*---------------------------------------
Command: gulp css:dist
Description: Compiles sass which will create app.css in tmp then copy from temp to dist
---------------------------------------- */
gulp.task('css:dist', gulp.series('styles', function () {
return gulp.src(paths.srcCSS)
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(gulp.dest(paths.distCSS));
}));
gulp.task('css:dist-min', gulp.series('styles', function () {
return gulp.src(paths.srcCSS)
.pipe(sass(sassOptions).on('error', sass.logError))
.pipe(cleanCSS())
.pipe(gulp.dest(paths.distCSS));
}));
/*---------------------------------------
Command: gulp js:dist
Description: copy js from tmp to dist and compress it
---------------------------------------- */
gulp.task('js:dist', gulp.series('scripts', function () {
return gulp.src(paths.tmpJS + '/**/*')
.pipe(gulp.dest(paths.distJS));
}));
gulp.task('js:dist-min', gulp.series('scripts', function () {
return gulp.src(paths.tmpJS + '/**/*')
.pipe(uglify())
.pipe(gulp.dest(paths.distJS));
}));
/*---------------------------------------
Command: gulp php:dist
Description: copy php to dist
---------------------------------------- */
gulp.task('php:dist', gulp.series('php', function () {
return gulp.src(paths.tmpPhp + '/**/*')
.pipe(gulp.dest(paths.distPhp));
}));
gulp.task('videos:dist', gulp.series('videos', function () {
return gulp.src(paths.tmpVideos + '/**/*')
.pipe(gulp.dest(paths.distVideos));
}));
/*---------------------------------------
Command: gulp fonts:dist
Description: copy fonts to dist
---------------------------------------- */
gulp.task('fonts:dist', gulp.series('fonts', function () {
return gulp.src(paths.tmpFonts + '/**/*')
.pipe(gulp.dest(paths.distFonts));
}));
/*---------------------------------------
Command: gulp img-compress
Description: compress images : will only compress images in demo folder
do not add any other file except png,jpg,jpeg
---------------------------------------- */
gulp.task('img-compress', gulp.series('images', function () {
return gulp.src(paths.distImages + '/**/*')
.pipe(imagemin())
.pipe(gulp.dest(paths.distImages));
}));
gulp.task('previews-resize', function () {
return gulp.src('app/src/assets/img/previews/**/*')
.pipe(gm(function (gmfile) {
return gmfile.resize(470);
}, {
imageMagick: true
}))
.pipe(gulp.dest(paths.distImages + '/previews'));
});
/*---------------------------------------
Command: gulp images:dist
Description: copy images to destination
---------------------------------------- */
gulp.task('images:dist', gulp.series('images', function () {
return gulp.src(paths.srcImages)
.pipe(gulp.dest(paths.distImages))
}));
/*---------------------------------------
Command: gulp placeholder
Description: Will change images to blur placeholders in dist/assets/img/demo
---------------------------------------- */
gulp.task('placeholder', gulp.series(['images'], function () {
return gulp.src(paths.distImages + '/demo/**/*')
.pipe(gm(function (gmfile) {
return gmfile.blur(100, 100);
}, {
imageMagick: true
}))
.pipe(gulp.dest(paths.distImages + '/demo/'));
}));
/*---------------------------------------
Command: gulp copy:dist
Description: Copy to destination folder
---------------------------------------- */
gulp.task('clean:dist', function () {
return del(bases.dist + '**/*', {force: true});
});
/*---------------------------------------
Command: gulp production
Description: Create production version
---------------------------------------- */
/*---------------------------------------
Command: gulp deploy
Description: Create final minified version & upload to server
-
---------------------------------------- */
gulp.task('clean:demo-folder', function () {
return del(bases.demoFolder + '**', {force: true});
});
gulp.task('make-demo', gulp.series(['clean:demo-folder'], function () {
return gulp.src(bases.dist + '**')
.pipe(gulp.dest(bases.demoFolder));
}));
gulp.task('production', gulp.series(
'clean:dist',
'html:dist',
'css:dist',
'js:dist',
'fonts:dist',
'php:dist',
'images:dist',
'videos:dist'
));
gulp.task('production-min', gulp.series(
'clean:dist',
'html:dist-min',
'css:dist-min',
'js:dist-min',
'fonts:dist',
'php:dist',
'images:dist',
'videos:dist'
));
gulp.task('demo', gulp.series(
'production-min',
'previews-resize',
'img-compress',
'make-demo'
));
/*---------------------------------------
Command: gulp tf
Description: Create final tf version & upload to server on for envato authors.
-
---------------------------------------- */
gulp.task('clean:tf', function () {
return del(tf.zipSrc + '**', {force: true});
});
gulp.task('clean:tf-zip', function () {
return del(tf.zipDist + '**', {force: true});
});
gulp.task('tf-zip', gulp.series(['clean:tf-zip'], function () {
return gulp.src(tf.zipSrc + '**')
.pipe(zip(app.slug + '-' + app.version + '-all-files.zip'))
.pipe(gulp.dest(tf.zipDist))
}));
var $conent = '{\r\n \"server\": {\r\n \"host\": \"ENTER YOUR HOST NAME\",\r\n \"user\": \"ENTER YOUR USER NAME\",\r\n \"pass\": \"ENTER YOUR PASSWORD\",\r\n \"port\": \"21\",\r\n \"path\": \"\/public_html\/\"\r\n }\r\n}\r\n';
var $documentation = 'Kindly check documentations.html in app\/dist folder for help.\r\n\r\n\r\nFor online docs visit\r\n\r\nhttp:\/\/xvelopers.com\/html\/paper\/documentations.html';
gulp.task('tf-upload', function () {
var conn = vinylFtp.create({
host: ftp.tf.host,
user: ftp.tf.user,
password: ftp.tf.pass,
parallel: 3,
log: gutil.log
});
var globs = [
'tf-upload/' + '**'
];
// using base = '.' will transfer everything to /public_html correctly
// turn off buffering in gulp.src for best performance
return gulp.src(globs, {buffer: false})
.pipe(conn.newer(ftp.tf.path)) // only upload newer files
.pipe(conn.dest(ftp.tf.path));
});
function cb (err,data) {
if (err) {
return console.log(err);
}
console.log(data);
}
gulp.task('tf-version', gulp.series(['clean:tf', 'production', 'placeholder', 'previews-resize', 'img-compress'], function () {
gulp.src('gulpfile.js')
.pipe(gulp.dest(tf.root));
gulp.src('package.json')
.pipe(gulp.dest(tf.root));
fs.outputFile(tf.root + 'ftp.config', $conent, cb);
fs.outputFile(tf.zipSrc + 'Documentation/help.txt', $documentation, cb);
gulp.src(bases.app + '**')
.pipe(gulp.dest(tf.app + 'src'));
gulp.src(bases.dist + '**')
.pipe(gulp.dest(tf.app + 'dist'));
//Replace compress and placeholder images from dist to src folder
del(tf.app + 'src/assets/img/**/*', {force: true});
return gulp.src(tf.app + 'dist/assets/img/**/*')
.pipe(gulp.dest(tf.app + 'src/assets/img/**/*'));
}));
gulp.task('tf', gulp.series(
'tf-version',
'tf-zip',
'tf-upload',
'clean:tf',
'clean:tf-zip'
));
// CREATE DEMO VERSION
gulp.task('upload', function () {
var conn = vinylFtp.create({
host: ftp.server.host,
user: ftp.server.user,
password: ftp.server.pass,
parallel: 3,
log: gutil.log
});
var globs = [
bases.demoFolder + '**'
];
// using base = '.' will transfer everything to /public_html correctly
// turn off buffering in gulp.src for best performance
return gulp.src(globs, {base: '.', buffer: false})
.pipe(conn.newer(ftp.server.path)) // only upload newer files
.pipe(conn.dest(ftp.server.path));
});
gulp.task('deploy', gulp.series(
'default',
'demo',
'upload',
'clean:demo-folder'
));