-
Notifications
You must be signed in to change notification settings - Fork 6
/
zeno.js
1029 lines (889 loc) · 33.7 KB
/
zeno.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';
var fs = require('fs'),
util = require('util'),
path = require('path'),
spawn = require('child_process').spawn,
im = require('imagemagick'),
request = require('request'),
utils = require('./tools/utils');
var Zeno = function (app, server, io, params) {
this.app = app;
this.io = io;
this.server = server;
// --log
this.logFile = params.log;
// --fileUrl
this.pageUrl = params.fileUrl || false;
// --file
this.pageFile = params.file || 'pages.json';
// --cookieUrl
this.cookieUrl = params.cookieUrl || false;
// --cookie
this.cookieFile = params.cookie || 'cookies.json';
// --folder
this.dir = params.folder || 'screenshots';
// --engine
this.engine = require('./tools/engine').get(params.engine);
// --startAction
this.startAction = params.startAction || false;
this.log = utils.log;
this.devices = ['desktop', 'tablet', 'mobile'];
this.versioning = 'versioning';
this.ext = '.png';
this.phantomScript = path.join(__dirname, 'phantomScript.js');
this.instance = [];
this.cookiesList = [];
this.modules = [];
this.listtoshot = [];
this.versions = [];
this.versionsByPage = {};
this.results = {};
this.pages = {};
this.emitter = new (require('events').EventEmitter)();
this.emitter.setMaxListeners(200);
this.uaDesktop = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:44.0) Gecko/20100101 Firefox/44.0';
this.uaMobile = 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_2_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13D15 Safari/601.1';
this.uaTablet = 'Mozilla/5.0 (iPad; CPU OS 9_0 like Mac OS X) AppleWebKit/601.1.17 (KHTML, like Gecko) Version/8.0 Mobile/13A175 Safari/600.1.4';
this.vpDesktop = 1280;
this.vpTablet = 1024;
this.vpMobile = 640;
};
Zeno.prototype = {
/*
* Initialize the app and fetch configuration
* @param cb end initialization callback
*/
init: function(cb) {
var self = this;
if (!fs.existsSync(this.dir)) {
fs.mkdirSync(this.dir);
}
if (!fs.existsSync(path.join(this.dir, this.versioning))) {
fs.mkdirSync(path.join(this.dir, this.versioning));
}
this.updateVersionList(true);
if (this.logFile) {
var logFile = fs.createWriteStream(this.logFile, {flags : 'a'});
var logStdout = process.stdout;
// copy stdout in the log file
console.log = function(d) {
logFile.write(util.format(d) + '\n');
logStdout.write(util.format(d) + '\n');
};
}
// Init results object
this.devices.forEach(function(device) {
self.results.engine = self.engine.name;
self.results[device] = {};
self.results[device].results = [];
});
this.addCoreRoad();
this.addListeners();
this.addSocketIoListeners();
this.log('Engine detected: ' + this.engine.name + ' ' + this.engine.version);
var loadConfigurationFile = function(err, file) {
if (err) {
self.log('No file configuration founded');
} else {
self.pages = JSON.parse(file);
self.pages.refreshing = {
desktop: [],
tablet: [],
mobile: []
};
self.instance = self.pages.envs;
}
self.loadModules(cb);
if (self.startAction) {
self.devicesComparaison(self.instance[0], self.instance[1]);
}
};
// Fetch configuration file
if (this.pageUrl) {
request(this.pageUrl, function (err, response, file) {
loadConfigurationFile(err, file);
});
} else {
fs.readFile(this.pageFile, 'utf-8', function (err, file) {
loadConfigurationFile(err, file);
});
}
// Fetch cookies file
if (this.cookieUrl) {
request(this.cookieUrl, function (err, response, file) {
if (err) {
this.log(err);
}
else {
self.cookiesList = JSON.parse(file);
}
});
} else {
fs.readFile(this.cookieFile, 'utf-8', function (err, file) {
if (err) {
this.log(err);
}
else {
self.cookiesList = JSON.parse(file);
}
});
}
},
/*
* Create core express road
*/
addCoreRoad: function () {
var self = this;
this.app.get('/', function(req, res) {
res.render('index');
});
this.app.get('/update/:env', function(req, res) {
self.instance.forEach(function (env) {
if(env.alias === req.params.env || "all" === req.params.env) {
self.envScreenshot(env, 'desktop');
self.envScreenshot(env, 'mobile');
self.envScreenshot(env, 'tablet');
}
});
res.send('Update ' + req.params.env + ' in progress\n');
});
this.app.get('/update/:env/:screenshotId', function(req, res) {
self.instance.forEach(function (env) {
if(env.alias === req.params.env) {
self.envScreenshot(env, 'desktop', req.params.screenshotId);
self.envScreenshot(env, 'mobile', req.params.screenshotId);
self.envScreenshot(env, 'tablet', req.params.screenshotId);
}
});
res.send('Update ' + req.params.env + ' with ' + req.params.screenshotId+ 'in progress\n');
});
this.app.get('/routes/:name', function(req, res) {
res.render('routes/' + req.params.name);
});
this.app.get('/pages', function(req, res) {
res.send(JSON.stringify(self.pages));
});
this.app.get('/queue', function(req, res) {
res.send(JSON.stringify(self.listtoshot));
});
this.app.get('/versions', function(req, res) {
res.send(JSON.stringify(self.versions));
});
this.app.get('/versions/page/', function(req, res) {
res.send(JSON.stringify(self.versionsByPage));
});
this.app.get('/versions/page/:page', function(req, res) {
res.send(JSON.stringify(self.versionsByPage[req.params.page]));
});
this.app.get('/results', function(req, res) {
res.send(JSON.stringify(self.results));
});
this.app.get('/results/:device', function(req, res) {
res.send(JSON.stringify(self.results[req.params.device]));
});
this.app.get('/ua', function(req, res) {
var data = {};
var userAgents = {};
userAgents.uaDesktop = self.uaDesktop;
userAgents.uaTablet = self.uaTablet;
userAgents.uaMobile = self.uaMobile;
var viewPorts = {};
viewPorts.vpDesktop = self.vpDesktop;
viewPorts.vpTablet= self.vpTablet;
viewPorts.vpMobile= self.vpMobile;
data.userAgents = userAgents;
data.viewPorts = viewPorts;
res.send(JSON.stringify(data));
});
this.app.get('/log', function(req, res) {
if (self.logFile) {
fs.readFile(self.logFile, 'utf-8', function(err, file){
if (err) {self.log(err);}
var lines = file.trim().split('\n');
var log = '';
var start = lines.length - 20 > 0 ? lines.length - 20 : 0;
for (var i = start; i < lines.length; i++) {
log += lines[i] + '<br/>';
}
res.send(log);
});
} else {
res.send('log mode not activated');
}
});
this.app.get('/compareall/:env1/:env2', function(req, res) {
var env1, env2;
for (var i = 0; i < self.instance.length; i++) {
if (self.instance[i].alias === req.params.env1) {
env1 = self.instance[i];
} else if (self.instance[i].alias === req.params.env2) {
env2 = self.instance[i];
}
}
self.devicesComparaison(env1, env2);
res.send('{status: "Comparaison started"}');
});
},
/*
* Attach core listeners
*/
addListeners: function () {
var self = this;
this.on('takeScreenshot', function (data) {
//check for fallbacks
if(typeof data.options.device === 'undefined') {
data.options.device = 'desktop';
}
if(typeof data.options.env === 'undefined') {
data.options.env = '';
}
self.takeScreenshot(data.url, data.path, data.options);
});
this.on('onEnvUpdate', function (data){
self.io.sockets.emit('queueChangeEvent', self.listtoshot);
});
},
/*
* Attach core socket IO listeners
*/
addSocketIoListeners: function () {
var self = this;
/*
* SocketIO listeners
*/
this.io.sockets.on('connection', function (socket) {
/*
* Fired when an image is refreshed by a client
*/
socket.on('refreshOneScreen', function (data) {
self.unitScreenshot(
self.instance[data.env],
data.name,
data.type,
socket
);
});
/*
* Fired when an environment is refreshed by a client
*/
socket.on('refreshEnv', function (data) {
self.envScreenshot(data.env, data.type, 'manual', socket);
});
/*
* Get if pages are loaded by url or filesystem
*/
socket.on('getConfType', function (cb) {
var conf = {
file: self.pageUrl || self.pageFile,
url: self.pageUrl ? true : false
};
cb(conf)
});
/*
* Update conf file
*/
socket.on('updateConfFile', function (cb) {
if (self.pageUrl) {
request(self.pageUrl, function (err, response, file) {
try {
self.pages = JSON.parse(file);
self.pages.refreshing = {
desktop: [],
tablet : [],
mobile : []
};
self.instance = self.pages.envs;
self.log('Configuration reloaded with success');
cb(self.pages);
} catch (e) {
console.log("Error reloading conf: " + e);
}
});
} else {
fs.readFile(self.pageFile, 'utf-8', function (err, file) {
try {
self.pages = JSON.parse(file);
self.pages.refreshing = {
desktop: [],
tablet : [],
mobile : []
};
self.instance = self.pages.envs;
self.log('Configuration reloaded with success');
cb(self.pages);
} catch (e) {
console.log("Error reloading conf: " + e);
}
});
}
});
/*
* Fired when user update the configuration from /pages
*/
socket.on('updateList', function (data) {
self.devices.forEach(function (device){
data.list[device].forEach(function (url) {
delete url.percentage;
});
});
self.pages.desktop = data.list.desktop;
self.pages.mobile = data.list.mobile;
self.pages.tablet = data.list.tablet;
self.instance = self.pages.envs;
});
/*
* Fired after each client side comparaison to update the server object
*/
socket.on('updateResults', function (data) {
self.updateResultsByName(data.name, data.device, data.percentage);
});
socket.on('updateEngine', function (data) {
self.engine = require('./tools/engine').get(data.engine);
self.pages.engine = self.engine.name;
self.log('Engine updated : ' + self.engine.name + ' ' + self.engine.version);
});
socket.on('saveList', function () {
// todo
});
socket.on('addDevicePage', function (page, device, cb) {
delete page.percentage;
delete page.$$hashKey;
delete page.confRefreshing;
delete page.classRefreshing;
if (page) {
self.pages[device].push(page);
cb('ok');
} else {
cb('ko');
}
});
var hasSameAlternativeAndCookies = function(page1, page2) {
return (page1.alternative == page2.alternative
&& JSON.stringify(page1.cookies) == JSON.stringify(page2.cookies));
};
var isSamePage = function (page1, page2) {
return (page1.url == page2.url && hasSameAlternativeAndCookies(page1, page2));
};
socket.on('removeDevicePage', function (page, device, cb) {
var i = self.pages[device].length;
while (i--) {
var k = self.pages[device][i];
if (isSamePage(k, page)) {
self.pages[device].splice(i, 1);
cb('ok');
return;
}
}
cb('ko');
});
socket.on('updateUrlPage', function (page, devices, cb) {
var urlBase;
var present = false;
var firstDevice = devices[0];
devices.shift();
self.pages[firstDevice].some(function (k) {
if (k.name == page.name
&& hasSameAlternativeAndCookies(k, page)) {
urlBase = k.url;
k.url = page.url;
return present = true;
}
});
if (!present) {
cb('ko');
return;
}
devices.some(function (device) {
self.pages[device].some(function (k) {
if (k.url == urlBase
&& hasSameAlternativeAndCookies(k, page)) {
k.url = page.url;
return;
}
});
});
cb('ok');
});
socket.on('getCookiesList', function (cb) {
cb(self.cookiesList)
});
socket.on('modifyCookieValue', function(cookie, cb) {
try {
var cookieName = cookie.name;
self.cookiesList[cookieName].value = cookie.value;
cb("ok")
} catch(exception) {
self.log(exception);
cb("ko")
}
});
});
},
/*
* Load modules
* @param cb end initialization callback
*/
loadModules: function (cb) {
var self = this,
name = 'modules';
fs.readdir(name, function(err, dirs){
if (err) {return self.log(err);}
self.modules = dirs;
self.modules.forEach(function(dir) {
if (dir[0] == '.') {
return
}
var module = require('./' + name + '/' + dir + '/' + dir);
if (module.module) {
module.module(self.getWrapper());
} else {
self.log('Module "' + dir + '" desactivated: no module Method');
}
});
self.endInit();
if(cb) {cb();}
});
},
/*
* finalize express configuration adn error handling
*/
endInit: function () {
var self = this;
this.app.get('*', function(req, res, next) {
self.log('Error 404 : ' + req.url);
var err = new Error();
err.status = 404;
next(err);
});
/* Error handling */
this.app.use(function(err, req, res, next){
if(err.status !== 404) {
return next();
}
res.status(404).render('404');
});
this.server.listen(this.app.get('port'), function(){
self.log('Express server listening on port ' + self.app.get('port'));
});
},
/*
* Return a list of cookies value for a given page
* @param page one page object of one device
*/
getCookies: function (page) {
var cookies = [];
if (page.cookies) {
for (var i = 0; i < page.cookies.length; i++) {
if(this.cookiesList[page.cookies[i]]) {
cookies.push(this.cookiesList[page.cookies[i]]);
}
}
}
return cookies;
},
/*
* Create a phantmJs process to take screenshot and create a thumbnail using imagemagick
* @param url page url to capture
* @param name image name according to configuration
* @param options engine options
*/
takeScreenshot: function (url, name, options) {
var d = new Date(),
p = require('path'),
self = this,
path = p.join(this.dir, options.env + name + this.ext);
if (options.hasOwnProperty('date')) {
d = options.date;
}
// directory name pattern : mm-dd-yyyy-hh-mm-ss(?-screenshotId)
var versionPath = (d.getMonth() + 1) + '-' + d.getDate() + '-' + d.getFullYear() + '-' + d.getHours() + '-' + d.getMinutes() + '-' + d.getSeconds();
if (options.hasOwnProperty('screenshotId')) {
versionPath += '-' + options.screenshotId;
}
var screenshotPackDir = p.join(this.dir, this.versioning, versionPath);
if (!fs.existsSync(screenshotPackDir)) {
fs.mkdir(screenshotPackDir, function (err){
var screenShotName = options.env + name + self.ext;
self.updateVersionList(false, screenShotName, versionPath);
});
}
var args = [this.engine.ssl, this.phantomScript, JSON.stringify({
ua : options.userAgent,
viewportSize: options.viewportSize,
cookies : options.cookies,
blacklist : self.pages.blacklist,
path : path, // path on disk
url : url, // use to render a page from url
body : options.body // use to render a page from html
})],
process = spawn(this.engine.path, args);
process.stdout.on('data', function(data) {
var chunk = '' + data, // cast as a string
metrics;
try {
metrics = JSON.parse(chunk);
if (metrics.errors.length) {
self.log('Request error: ' + metrics.errors);
}
} catch(err) {
self.log('Phantom Error:' + chunk);
}
self.emit('onScreenshotDone', {
name : name,
options: options,
metrics: metrics
});
});
// exit callback
process.on('exit', function(code) {
if(code) {
self.log('Error during ' + self.engine.name + ' exec: ' + code);
return self.next();
}
self.log('(' + this.pid + ') update done: ' + path);
// Copy for versioning
var stream = fs.createReadStream(path);
stream.pipe(fs.createWriteStream(screenshotPackDir + '/' + options.env + name + self.ext));
stream.on('end', function(){
self.emit('onCopyDone', {
name: name
});
});
// Create a thumbnail to reduce Ram blueprint on client
im.resize({
srcPath: path,
dstPath: path.replace(self.ext, '_thumb.png'),
height : 200
}, function(err){
if (err) {return console.warn(err);}
// Copy for versioning
var stream = fs.createReadStream(path);
stream.pipe(fs.createWriteStream(screenshotPackDir + '/' + options.env + name + '_thumb' + self.ext));
stream.on('end', function(){
self.emit('onCopyDone', {
name: name
});
});
// Push update url to each client
self.io.sockets.emit('updateOneScreen', {
name: name,
env : options.env
});
self.next();
});
});
},
/*
* Remove current element from the queue
* and start the next one
*/
next: function () {
this.listtoshot.splice(0, 1);
this.io.sockets.emit('queueChangeEvent', this.listtoshot);
if (this.listtoshot.length){
this.takeScreenshot(
this.listtoshot[0].url,
this.listtoshot[0].name,
this.listtoshot[0].options
);
} else {
// Start comparaison if at least one environment has been updated
if (this.pages.refreshing.desktop.length || this.pages.refreshing.tablet.length || this.pages.refreshing.mobile.length){
this.devicesComparaison(this.instance[0], this.instance[1]);
}
// wait that everthing is finished before allowing the next refresh
this.pages.refreshing = {
desktop: [],
tablet: [],
mobile: []
};
}
},
/*
* Refresh a whole environment for one device
* @param env environment object
* @param device device name
* @param packScreenshotId id of screenshot pack
*/
envScreenshot: function (env, device, paramScreenshotId) {
var server,
cookies = [],
self = this,
pages = this.pages[device],
details = {};
if (device === 'mobile') {
details.viewport = {width: this.vpMobile, height: 1100};
details.ua = self.uaMobile;
} else if (device === 'desktop') {
details.viewport = {width: this.vpDesktop, height: 1100};
details.ua = self.uaDesktop;
} else if (device === 'tablet') {
details.viewport = {width: this.vpTablet, height: 1100};
details.ua = self.uaTablet;
} else {
return;
}
if (!utils.contains(this.pages.refreshing[device], env.server)) {
this.log('Update ' + device + ' screenshots (' + env.server + ')');
this.pages.refreshing[device].push(env.server);
pages.forEach(function (page) {
if (page.url) {
var alternative = undefined;
self.getCookies(cookies, page);
if (page.hasOwnProperty('alternative') && env.hasOwnProperty('alternative')) {
alternative = env.alternative[page.alternative];
}
var options = {
env : env.server,
cookies : cookies,
device : device,
userAgent : details.ua,
viewportSize : details.viewport,
date : new Date(),
};
if (typeof paramScreenshotId !== "undefined") {
options.screenshotId = paramScreenshotId;
}
if (typeof alternative !== 'undefined'){
server = alternative;
} else {
server = env.server;
}
self.listtoshot.push({
url : self.parseUrl(server, page.url, env.port),
name : page.name,
options: options
});
}
});
self.emit('onEnvUpdate', {
device : device,
env : env.server
});
} else {
this.log('Refresh desktop already in progress: ' + env.server);
}
// do not trigger update if it's already running
if(this.listtoshot.length === pages.length) {
this.takeScreenshot(
this.listtoshot[0].url,
this.listtoshot[0].name,
this.listtoshot[0].options
);
}
},
/*
* Refresh one version of one image
* @param env environment object
* @param name id of the page object
* @param device device name
*/
unitScreenshot: function (env, name, device) {
var ua,
width,
server,
url,
alternative,
cookies = [],
height = 1100;
if (device === 'tablet') {
ua = this.uaTablet;
width = this.vpTablet;
} else if (device === 'mobile') {
ua = this.uaMobile;
width = this.vpMobile;
} else if (device === 'desktop') {
ua = this.uaDesktop;
width = this.vpDesktop;
} else {
return;
}
for (var i = 0; i < this.pages[device].length; i++) {
if (this.pages[device][i].name === name) {
cookies = this.getCookies(this.pages[device][i]);
if (this.pages[device][i].hasOwnProperty('alternative') && env.hasOwnProperty('alternative')) {
alternative = env.alternative[this.pages[device][i].alternative];
}
url = this.pages[device][i].url;
break;
}
}
this.log('Updating "' + name + '" for ' + device + '/' + env.alias);
var options = {
env : env.server,
userAgent : ua,
cookies : cookies,
device : device,
date : new Date(),
viewportSize :
{width: width, height: height}
};
if (typeof alternative !== 'undefined'){
server = alternative;
} else {
server = env.server;
}
var realUrl = this.parseUrl(server, url, env.port);
this.listtoshot.push({
url : realUrl,
name : name,
options : options
});
this.io.sockets.emit('queueChangeEvent', this.listtoshot);
// do not trigger update if it's already running, just queue it
if(this.listtoshot.length === 1) {
this.takeScreenshot(realUrl, name, options);
}
},
/*
* Read versionning folder to update and sort the list
*/
updateVersionList: function (withFullRead, file, version) {
this.log('Fetch versions list');
var self = this;
var pathVersioning = path.join(this.dir, this.versioning);
var functionCompareDate = function (a, b) {
// read mm-dd-yyyy-hh-mm-ss(?-screenshotId)
var as = a.split('-');
var bs = b.split('-');
if (as.length == 3) {
var date1 = new Date(as[2], parseInt(as[0], 10) - 1, as[1]);
} else {
var date1 = new Date(as[2], parseInt(as[0], 10) - 1, as[1], as[3], as[4], as[5]);
}
if (bs.length == 3) {
var date2 = new Date(bs[2], parseInt(bs[0], 10) - 1, bs[1]);
} else {
var date2 = new Date(bs[2], parseInt(bs[0], 10) - 1, bs[1], bs[3], bs[4], bs[5]);
}
var diff = date1 - date2;
return diff;
};
var makeVersionsByPage = function (file, version) {
if (self.versionsByPage.hasOwnProperty(file)) {
if (self.versionsByPage[file].indexOf(version) == -1) {
self.versionsByPage[file].push(version);
self.versionsByPage[file].sort(functionCompareDate);
} else {
}
} else {
self.versionsByPage[file] = [version];
}
};
if (withFullRead) {
fs.readdir(pathVersioning, function (err, dirs) {
if (err) {
return self.log(err);
}
dirs.sort(functionCompareDate);
self.versions = dirs;
self.io.sockets.emit('updateVersionEvent', {versions: self.versions});
dirs.forEach(function (version) {
fs.readdir(path.join(pathVersioning, version), function (err, files) {
if (files) {
files.forEach(function (file) {
makeVersionsByPage(file, version);
});
self.io.sockets.emit('updateVersionByPageEvent', {versionsByPage: self.versionsByPage});
}
});
});
});
} else {
if (self.versions.indexOf(version) == -1) {
self.versions.push(version);
self.versions.sort(functionCompareDate)
}
makeVersionsByPage(file, version);
self.io.sockets.emit('updateVersionEvent', {versions: self.versions});
self.io.sockets.emit('updateVersionByPageEvent', {versionsByPage: self.versionsByPage});
}
},
/*
* Return a valid http url to render
* @param server property of environment object
* @param url url to decode
* @param port port number (optional)
*/
parseUrl: function (server, url, port) {
var decodeUrl;
var host = this.pages.host.replace('{$alias}', server);
if (port) {
host += ':' + port;
}
decodeUrl = url.replace('$host', host);
return decodeUrl;
},
/*
* Update results list
* @param name: image name described in configuration
* @param device: device where the comparaison has been computed
* @param percentage: result of the comparaison
*/
updateResultsByName: function(name, device, percentage) {
var found = false;
for (var i = 0; i < this.results[device].results.length; i++) {
var error = this.results[device].results[i];
if (name === error.name) {
found = true;
if (percentage === '0.00') {
this.results[device].results.splice(i, 1);
} else {
error.percentage = percentage;
this.results[device].date = new Date();
}
break;
}
}
// new failure to add
if (!found && percentage !== '0.00') {
this.results[device].results.push({name: name, percentage: percentage});
this.results[device].date = new Date();
}
},
/*
* Compare the whole set of images
* @param env1: value of instance object
* @param env2: value of instance object
*/
devicesComparaison: function (env1, env2) {
this.log('Comparaison started between: ' + env1.alias + '/' + env2.alias);
var self = this;
this.devices.forEach(function (device) {
self.results[device].results = [];
self.pages[device].forEach(function (page) {
self.results[device].date = new Date();
var name = page.name;
utils.compareImages(
path.join(self.dir, env1.server + name + self.ext),
path.join(self.dir, env2.server + name + self.ext),
function (percentage) {
self.updateResultsByName(name, device, percentage);
}
);
});
});
},