-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
1524 lines (1368 loc) · 41.8 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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
/*
D U S T M A N
1.11.55
A Gulp 4 automation boilerplate
by https://github.com/vitto
*/
var gulp = require('gulp');
var message = (function(){
var colour = require('colour');
// var sleep = require('sleep').sleep;
colour.setTheme({
error: 'red bold',
event: 'magenta',
intro: 'rainbow',
notice: 'yellow',
speak: 'white',
success: 'green',
task: 'white',
verbose: 'blue',
warning: 'yellow bold'
});
var verbose = 3;
var phrases = {
add: [
'What the hell is %file%?? I, DUSTMAN will do something to solve this situation...',
'I\'ve found a sensational discovery, %file% is alive!',
'Hey %file%, welcome to da build',
'File %file% detected. Updating the build.'
],
change: [
'Hey, something\'s happened to %file%, this is a work for DUSTMAN...',
'Dear %file%, do you really though I wouldn\'t noticed you? Hahaha!',
'Aha! %file%! You are under build!',
'We change every day, just as you, %file%'
],
unlink: [
'We have lost %file%, this is a work for DUSTMAN...',
'Oh my god... %file%... Nooooo!',
'Another good %file% gone... I will avange you...',
'Good bye %file%. I will clean your past without pain.'
],
wait: [
'Waiting silently if something changes, is unlinked or added',
'Dustman is watching them',
'The dust is never clear totally, waiting for changes',
'I will seek and clean. Again, and again'
]
};
var isVerboseEnough = function(verbosity) {
return verbose >= verbosity;
};
var log = function(level, message, delay) {
if (isVerboseEnough(level)) {
console.log(message);
if (typeof delay !== 'undefined') {
var waitTill = new Date(new Date().getTime() + delay * 1000);
while(waitTill > new Date()) { }
// sleep(delay);
}
}
};
var event = function(eventType, file) {
var min, max, phrase, splitPhrase, finalPhrase, index;
min = 1;
max = phrases[eventType].length;
index = (Math.floor(Math.random() * (max - min + 1)) + min) - 1;
phrase = phrases[eventType][index];
if (typeof file !== 'undefined') {
splitPhrase = phrase.split('%file%');
finalPhrase = colour.event(splitPhrase[0]) + file + colour.event(splitPhrase[1]);
} else {
finalPhrase = colour.event(phrase + '...');
}
log(1, finalPhrase);
};
return {
intro: function() {
console.log('');
console.log(colour.intro(' D U S T M A N '));
console.log('');
},
error: function(message) {
log(0, colour.error('Error: ') + message.toString().trim());
},
event: function(eventType, file) {
event(eventType, file);
},
wait: function() {
log(3, '');
event('wait');
},
notice: function(message, delay) {
log(2, colour.notice('Notice: ') + message.toString().trim(), delay);
},
setVerbosity: function(verbosity) {
verbose = verbosity;
},
speak: function(message, delay) {
log(2, colour.speak(message), delay);
},
success: function(message, delay) {
log(1, colour.success(message.toString().trim()), delay);
},
task: function(message, delay) {
log(3, '');
log(2, colour.task(message), delay);
},
verbose: function(title, message, delay) {
if (typeof message !== 'undefined') {
log(3, colour.verbose(title.toString().trim() + ': ') + message.toString().trim(), delay);
} else {
log(3, colour.verbose(title.toString().trim()), delay);
}
},
warning: function(message, delay){
log(2, colour.warning('Warning: ') + message.toString().trim(), delay);
},
};
})();
var config = (function(){
var colour = require('colour');
var fs = require('fs');
var yaml = require('js-yaml');
var merge = require('merge');
var path = require('path');
var configFile = 'dustman.yml';
var nodeMinVersion = false;
var data = {
config: {
autoprefixer: {
browsers: [
'last 3 versions'
]
},
faker: {
locale: 'en'
},
prettify: {
indent_char: ' ',
indent_size: 2
},
twig: {
cache: false
},
osNotifications: true,
emptyFolders: true,
polling: false,
verbose: 3,
verify: false
},
css: {
file: 'dustman.min.css',
watch: './**/*.css',
vendors: {
files: false
}
},
js: {
file: 'dustman.min.js',
watch: './**/*.js',
vendors: {
files: false
}
},
html: {
engine: 'html',
files: false,
fixtures: false
},
paths: {
css: 'dustman/css/',
fonts: 'dustman/fonts/',
images: 'dustman/img/',
js: 'dustman/js/',
server: 'dustman/'
},
tasks: [
'css',
'js',
'html'
]
};
var configFileExists = function(configFile) {
try {
fs.accessSync(configFile, fs.F_OK);
return true;
} catch (e) {
message.error('config file ' + configFile + ' NOT found');
notify.broken('Config not found');
}
};
var checkDefaultConfig = function(loadedConfig, configFile){
if (!loadedConfig) {
configFileExists(configFile);
return yaml.safeLoad(fs.readFileSync(configFile, 'utf-8'));
}
return loadedConfig;
};
var pathClean = function(configPath) {
return path.normalize(configPath).replace(/\/$/, '') + '/';
};
var checkSubConfig = function(config, prop) {
if (typeof config === 'string') {
var dir = path.parse(configFile).dir;
if (dir === '') {
dir = '.';
}
var filePath = dir + '/' + config;
if (configFileExists(filePath)) {
return yaml.safeLoad(fs.readFileSync(filePath, 'utf-8'))[prop];
}
} else {
return config;
}
};
var checkArguments = function(){
var loadedConfig = false;
for (var i = 0; i < process.argv.length; i += 1) {
if (process.argv[i] === '--silent' || process.argv[i] === '--S') {
message.warning('You\'ve set --silent or --S flag to che gulp process, this could hide some errors not handled by Dustman');
}
if (process.argv[i] === '--config' && process.argv[i + 1] !== undefined) {
configFile = process.argv[i + 1];
configFileExists(configFile);
loadedConfig = yaml.safeLoad(fs.readFileSync(configFile, 'utf-8'));
}
}
loadedConfig = checkDefaultConfig(loadedConfig, configFile);
loadedConfig.config = checkSubConfig(loadedConfig.config, 'config');
loadedConfig.css = checkSubConfig(loadedConfig.css, 'css');
loadedConfig.html = checkSubConfig(loadedConfig.html, 'html');
loadedConfig.js = checkSubConfig(loadedConfig.js, 'js');
loadedConfig.paths = checkSubConfig(loadedConfig.paths, 'paths');
loadedConfig.shell = checkSubConfig(loadedConfig.shell, 'shell');
loadedConfig.vendors = checkSubConfig(loadedConfig.vendors, 'vendors');
data = merge.recursive(true, data, loadedConfig);
data.paths.css = pathClean(data.paths.css);
data.paths.fonts = pathClean(data.paths.fonts);
data.paths.images = pathClean(data.paths.images);
data.paths.js = pathClean(data.paths.js);
data.paths.server = pathClean(data.paths.server);
message.setVerbosity(data.config.verbose);
};
var warn = function(systemVersion, requiredVersion) {
message.verbose('Node system version', systemVersion);
message.verbose('Node required version', requiredVersion);
message.warning('The system node version is older then the required minimum version', 2);
message.warning('Please update node version to to avoid malfunctions', 3);
};
var checkVersion = function(version) {
if (typeof version === 'undefined') {
message.error('Minimum node version not specified');
}
var nodeSystemVersion = process.version.match(/(\d+\.\d+)/)[0].split('.');
nodeMinVersion = version.match(/(\d+\.\d+)/)[0].split('.');
if (parseInt(nodeSystemVersion[0]) < parseInt(nodeMinVersion[0])) {
warn(process.version.toString(), version.toString());
} else if (parseInt(nodeSystemVersion[0]) === parseInt(nodeMinVersion[0]) && parseInt(nodeSystemVersion[1]) < parseInt(nodeMinVersion[1])) {
warn(process.version.toString(), version.toString());
}
};
var ifProp = function(propName) {
return typeof data[propName] !== 'undefined' ? true : false;
};
return {
file: function() {
return configFile;
},
get: function(propName){
if (!ifProp(propName)) {
message.error('Required property ' + colour.yellow(propName) + ' NOT found in ' + colour.yellow(configFile));
}
return data[propName];
},
hasTask: function(taskName) {
if (!ifProp('tasks')) {
message.error('Required property ' + colour.yellow('tasks') + ' NOT found in ' + colour.yellow(configFile));
}
for (var i = 0; i < data.tasks.length; i += 1) {
if (data.tasks[i] === taskName) {
return true;
}
}
return false;
},
if: function(propName){
return ifProp(propName);
},
load: function(version){
checkVersion(version);
checkArguments();
},
pathClean : function(configPath) {
return path.normalize(configPath).replace(/\/$/, '') + '/';
}
};
})();
var task = task || {};
task.cache = (function(){
var fs = require('fs');
var mkdirp = require('mkdirp');
var folder = {
temp: __dirname + '/node_modules/dustman/temp/'
};
return {
init: function() {
if (!fs.existsSync(folder.temp)){
mkdirp.sync(folder.temp);
}
},
folder: function(name) {
return folder[name];
}
};
})();
var task = task || {};
task.core = (function(){
var fs = require('fs');
return {
action: function(name, actionName) {
return name + ':' + actionName;
},
fileCheck: function(path){
try {
path = path.replace(new RegExp(/\*.*$/), '');
fs.accessSync(path, fs.F_OK);
return true;
} catch (e) {
notify.broken('File not found, have you installed vendors after npm install?');
message.error(path.toString() + ' NOT found');
if (path.toString().toLowerCase().indexOf('vendor') > -1) {
message.warning('Have you installed vendors after npm install?');
}
process.exit();
}
},
fileExists: function(path) {
try {
path = path.replace(new RegExp(/\*.*$/), '');
fs.accessSync(path, fs.F_OK);
return true;
} catch (e) {
return false;
}
},
has: function(task, property) {
return property in task ? true : false;
}
};
})();
var tasks = (function(){
var browserSync = require('browser-sync');
var del = require('del');
var firstBuildDone = false;
var buildFine = true;
var buildAlreadyRecovered = false;
var buildIndex = 0;
var paths;
var pipeline = {
before:[],
middle:[],
after:[]
};
var cssConfig = {};
var tasksConfig = {};
var watchFolders = [];
var getWatchFolder = function(property) {
if (config.if(property)) {
var configProperty = config.get(property);
if (task.core.has(configProperty, 'watch')) {
return [configProperty.watch];
}
}
return [];
};
var init = function() {
paths = config.if('paths') ? config.get('paths') : false;
tasksConfig = config.get('config');
cssConfig = config.if('css') ? config.get('css') : false;
notify.init();
task.cache.init();
watchFolders = watchFolders.concat(getWatchFolder('css'));
watchFolders = watchFolders.concat(getWatchFolder('js'));
watchFolders = watchFolders.concat(getWatchFolder('html'));
};
var addToPipeline = function(subTaskPipeline) {
pipeline.before = pipeline.before.concat(subTaskPipeline.before);
pipeline.middle = pipeline.middle.concat(subTaskPipeline.middle);
pipeline.after = pipeline.after.concat(subTaskPipeline.after.reverse());
};
var pollingOptions = function() {
if (tasksConfig.polling) {
return {
usePolling: true,
interval: parseInt(tasksConfig.polling)
};
}
return {};
};
var http = function(tasks) {
gulp.task('http', gulp.series(tasks, function() {
browserSync.stream();
browserSync.init({
server: {
baseDir: paths.server
},
logLevel: 'info',
notify: true
});
message.wait();
return gulp.watch(watchFolders, pollingOptions(), gulp.series(tasks, function(done){
browserSync.reload();
message.wait();
done();
}))
.on('change', function(path) {
message.event('change', path);
})
.on('unlink', function(path) {
message.event('unlink', path);
})
.on('add', function(path) {
message.event('add', path);
});
}));
gulp.task('h', gulp.parallel('http'));
};
var watch = function(tasks) {
gulp.task('watch', gulp.series(tasks, function() {
message.wait();
return gulp.watch(watchFolders, pollingOptions(), gulp.series(tasks, function(done){
message.wait();
done();
}))
.on('change', function(path) {
message.event('change', path);
})
.on('unlink', function(path) {
message.event('unlink', path);
})
.on('add', function(path) {
message.event('add', path);
});
}));
gulp.task('w', gulp.parallel('watch'));
};
var verify = function() {
var pipeline = {
before: [],
middle: [],
after: []
};
if (tasksConfig.verify) {
var taskName = 'verify';
gulp.task(taskName, function(done){
var files = [];
files = files.concat(task.css.verify());
files = files.concat(task.js.verify());
files = files.concat(task.html.verify());
message.task('Verifying if all files were successfully created');
for (var i = 0; i < files.length; i += 1) {
message.verbose('File to check', files[i]);
task.core.fileCheck(files[i]);
}
done();
});
pipeline.middle.push(taskName);
}
return pipeline;
};
var buildStatus = function(isBuildFine) {
if (typeof isBuildFine !== 'undefined') {
buildFine = isBuildFine;
buildAlreadyRecovered = isBuildFine;
}
return buildFine;
};
var resetStatus = function() {
var pipeline = {
before: [],
middle: [],
after: []
};
var taskName = 'resetStatus';
gulp.task(taskName, function(done){
buildFine = true;
done();
});
pipeline.before.push(taskName);
return pipeline;
};
var id = function(increment) {
if (typeof increment !== 'undefined') {
buildIndex = buildIndex + 1;
}
return buildIndex;
};
var checkStatus = function() {
var pipeline = {
before: [],
middle: [],
after: []
};
if (tasksConfig.osNotifications) {
var taskName = 'checkStatus';
gulp.task(taskName, function(done){
if (id() > 0 && buildFine && !buildAlreadyRecovered) {
buildAlreadyRecovered = true;
notify.recovered();
}
done();
});
pipeline.after.push(taskName);
}
return pipeline;
};
var empty = function() {
var pipeline = {
before: [],
middle: [],
after: []
};
if (tasksConfig.emptyFolders) {
var taskName = 'empty';
gulp.task(taskName, function(done){
if (task.core.fileExists(paths.server) && !firstBuildDone) {
firstBuildDone = true;
message.task('Deleting previous build folder and it\'s assets to prepare the build process');
var path = paths.server;
if (paths.server.match(/.*\/$/)) {
path = paths.server + '**';
} else {
path = paths.server + '/**';
}
message.verbose('Folder to empy', path);
del.sync([path]);
done();
} else {
done();
}
});
pipeline.before.push(taskName);
}
return pipeline;
};
var build = function(tasks){
gulp.task('default', gulp.series(tasks, function(done){
done();
}));
};
return {
init: function(){
init();
addToPipeline(resetStatus());
addToPipeline(task.timer.get());
addToPipeline(task.shell.get());
addToPipeline(empty());
addToPipeline(task.css.get());
addToPipeline(task.js.get());
addToPipeline(task.vendors.get());
addToPipeline(task.html.get());
addToPipeline(verify());
addToPipeline(checkStatus());
pipeline.after.reverse();
var pipelineList = pipeline.before.concat(pipeline.middle.concat(pipeline.after));
build(pipelineList);
watch(pipelineList);
http(pipelineList);
},
buildStatus: function(isBuildFine) {
return buildStatus(isBuildFine);
},
id: function(increment) {
return id(increment);
}
};
})();
var notify = (function(){
var notifier = require('node-notifier');
var path = require('path');
var tasksConfig;
var init = function() {
tasksConfig = config.get('config');
};
return {
broken: function(message) {
if (tasksConfig.osNotifications) {
notifier.notify({
title: 'Dustman',
icon: path.join(__dirname, 'images/error.png'),
message: 'Build broken!\n' + message.toString().trim(),
sound: true
});
}
},
error: function(message) {
if (tasksConfig.osNotifications) {
notifier.notify({
title: 'Dustman',
icon: path.join(__dirname, 'images/warning.png'),
message: 'Build error!\n' + message.toString().trim()
});
}
tasks.buildStatus(false);
},
recovered: function() {
if (tasksConfig.osNotifications) {
notifier.notify({
title: 'Dustman',
icon: path.join(__dirname, 'images/success.png'),
message: 'Build recovered!\nNow you can continue working'
});
}
},
init: function() {
init();
}
};
})();
var task = task || {};
task.timer = (function(){
var moment = require('moment');
var name = 'timer';
var startBuildDate;
var pipeline = {
before:[],
middle:[],
after:[]
};
var start = function(){
var taskName = task.core.action(name, 'start');
gulp.task(taskName, function(done){
startBuildDate = Date.now();
done();
});
pipeline.before.push(taskName);
};
var stop = function(){
var taskName = task.core.action(name, 'stop');
gulp.task(taskName, function(done){
var stopBuildDate = Date.now();
var timeSpent = (stopBuildDate - startBuildDate)/1000 + ' secs';
message.success('The dust was cleaned successfully in ' + timeSpent);
message.success('Build [ ' + tasks.id() + ' ] done at ' + moment().format('HH:mm') + ' and ' + moment().format('ss') + ' seconds.');
console.log('');
tasks.id(true);
done();
});
pipeline.after.push(taskName);
};
return {
get: function(){
start();
stop();
return pipeline;
}
};
})();
var task = task || {};
task.vendors = (function(){
var name = 'vendors';
var paths = {};
var vendorsConfig = {};
var vendorsFontsBuilt = false;
var vendorsImagesBuilt = false;
var pipeline = {
before:[],
middle:[],
after:[]
};
var init = function() {
paths = config.get('paths');
vendorsConfig = config.if('vendors') ? config.get('vendors') : {};
};
var images = function() {
if (task.core.has(vendorsConfig, 'images')) {
var taskName = task.core.action(name, 'images');
gulp.task(taskName, function (done) {
if (vendorsImagesBuilt) {
message.notice('Skipping vendors images build to improve speed, if you need to update them just re-run the task');
done();
} else {
vendorsImagesBuilt = true;
message.task('Copying images from vendors');
for (var i = 0; i < vendorsConfig.images.length; i += 1) {
message.verbose('Image vendor', vendorsConfig.images[i]);
task.core.fileCheck(vendorsConfig.images[i]);
}
message.verbose('Vendor images copied to', paths.images);
return gulp.src(vendorsConfig.images)
.pipe(gulp.dest(paths.images));
}
});
return [taskName];
} else {
message.warning('Vendor\'s Images not found, skipping task');
}
return [];
};
var fonts = function(){
if (task.core.has(vendorsConfig, 'fonts')) {
var taskName = task.core.action(name, 'fonts');
gulp.task(taskName, function (done) {
if (vendorsFontsBuilt) {
message.notice('Skipping vendors fonts build to improve speed, if you need to update them just re-run the task');
done();
} else {
vendorsFontsBuilt = true;
message.task('Copying fonts from vendors');
var i = 0;
for (i = 0; i < vendorsConfig.fonts.length; i += 1) {
message.verbose('Font vendor', vendorsConfig.fonts[i]);
task.core.fileCheck(vendorsConfig.fonts[i]);
}
message.verbose('Vendor fonts copied to', paths.fonts);
return gulp.src(vendorsConfig.fonts)
.pipe(gulp.dest(paths.fonts));
}
});
return [taskName];
} else {
message.warning('Vendor\'s Fonts not found, skipping task');
}
return [];
};
return {
get: function(){
if (!config.if('vendors')) {
return pipeline;
}
init();
pipeline.middle = pipeline.middle.concat(fonts());
pipeline.middle = pipeline.middle.concat(images());
return pipeline;
}
};
})();
var task = task || {};
task.shell = (function(){
var exec = require('child_process').exec;
var name = 'shell';
var taskConfig = [];
var pipeline = {
before: [],
middle:[],
after: []
};
var init = function() {
taskConfig = config.if('shell') ? config.get('shell') : [];
};
var afterMessage = function(){
if (task.core.has(taskConfig, 'after')) {
var taskName = task.core.action(name, 'after-message');
gulp.task(taskName, function(done){
message.task('Executing shell tasks after build');
done();
});
pipeline.after.push(taskName);
}
};
var afterTask = function(index) {
var taskName = task.core.action(name, 'after-' + index);
pipeline.after.push(taskName);
gulp.task(taskName, function(done){
message.verbose('Shell', taskConfig.after[index]);
exec(taskConfig.after[index], function (err) {
done(err);
});
});
};
var after = function(){
if (task.core.has(taskConfig, 'after')) {
afterMessage();
for (var i = 0; i < taskConfig.after.length; i += 1) {
afterTask(i);
}
}
};
var beforeMessage = function(){
if (task.core.has(taskConfig, 'before')) {
var taskName = task.core.action(name, 'before-message');
gulp.task(taskName, function(done){
message.task('Executing shell tasks before build');
done();
});
pipeline.before.push(taskName);
}
};
var beforeTask = function(index) {
var taskName = task.core.action(name, 'before-' + index);
pipeline.before.push(taskName);
gulp.task(taskName, function(done){
message.verbose('Shell', taskConfig.before[index]);
exec(taskConfig.before[index], function (err) {
done(err);
});
});
};
var before = function(){
if (task.core.has(taskConfig, 'before')) {
beforeMessage();
for (var i = 0; i < taskConfig.before.length; i += 1) {
beforeTask(i);
}
}
};
return {
get: function(){
if (!config.if('shell')) {
return pipeline;
}
init();
before();
after();
return pipeline;
}
};
})();
var task = task || {};
task.html = (function(){
var faker = require('faker');
var moment = require('moment');
var path = require('path');
var prettify = require('gulp-html-prettify');
var twig = require('gulp-twig');
var fs = require('fs');
var name = 'html';
var paths = {};
var templateConfig;
var twigConfig = {};
var pipeline = {
before:[],
middle:[],
after:[]
};
var init = function() {
paths = config.get('paths');
templateConfig = config.if('html') ? config.get('html') : {};
twigConfig = config.if('config') ? config.get('config') : {};
faker.locale = 'en';
};
var loadFixtures = function() {
var file, fixtures;
if (templateConfig.fixtures) {
fixtures = {};
for (var fixture in templateConfig.fixtures) {
if (templateConfig.fixtures.hasOwnProperty(fixture)) {
message.verbose('Loading fixtures', templateConfig.fixtures[fixture]);
file = templateConfig.fixtures[fixture];
task.core.fileCheck(file);
fixtures[fixture] = JSON.parse(fs.readFileSync(file, 'utf8'));
}
}
return fixtures;
}
return {};
};
var build = function() {
if (config.if('html') && task.core.has(templateConfig, 'files')) {
gulp.task(name, function () {
message.task('Build HTML');
if (!task.core.has(twigConfig, 'twig')) {
twigConfig.twig = {};
}
twigConfig.twig.data = {
faker: faker,
moment: moment,
fixtures: loadFixtures()
};
for (var i = 0; i < templateConfig.files.length; i += 1) {
message.verbose('Template view', templateConfig.files[i]);
task.core.fileCheck(templateConfig.files[i]);
}
message.verbose('All Template files converted in', paths.server);
if (templateConfig.engine === 'html') {
return gulp.src(templateConfig.files)
.pipe(prettify(twigConfig.prettify || {}))
.pipe(gulp.dest(paths.server));
}
return gulp.src(templateConfig.files)
.pipe(twig(twigConfig.twig))
.pipe(prettify(twigConfig.prettify || {}))
.pipe(gulp.dest(paths.server));
});
return [name];
} else {
message.warning('Twig files not set, skipping task');
}
return [];
};
var getFilesToVerifyHTML = function() {
var htmlConfig, files;
files = [];
if (config.if('html')) {
htmlConfig = config.get('html');
if (htmlConfig.files) {