-
Notifications
You must be signed in to change notification settings - Fork 1
/
visualify-gallery.js
119 lines (108 loc) · 3.55 KB
/
visualify-gallery.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
#!/usr/bin/env node
'use strict'
const program = require('commander');
const fs = require('fs');
const chalk = require('chalk');
const Mustache = require('mustache');
const loadConfig = require('./lib/loadConfig.js');
program
.option('-c, --config-file <config-file>', 'Configuration file')
.option('-d, --defaults-file <defaults-file>', 'Default configuration')
.option('--debug', 'Show browser while doing snaps')
.option('-o, --output-directory [shots-dir]', 'Output directory for tests, directory in config file')
.parse(process.argv);
let domains = program.args;
const config = loadConfig.load(program.defaultsFile, program.configFile, domains);
const shotsDir = program.outputDirectory ? program.outputDirectory : config.directory;
config.directory = shotsDir;
/**
* Template variables:
*
* - gallery_generated
* - paths
* - .alias
* - .domain1name
* - .domain1url
* - .domain2name
* - .domain2url
* - .diffname
* - .widths
* - .width
* - .img1url
* - .thumb1url
* - .img2url
* - .thumb2url
* - .imgdiffurl
* - .thumbdiffurl
* - .diff
* - .threshold
*/
const variables = {};
// Generate path blocks with numeric keys to allow us to order correctly
try {
loadPaths(config)
.then((paths) => {
variables.paths = paths;
variables.gallery_generated = new Date();
saveGallery(config, variables)
.then(() => console.log(chalk.green('Gallery generated.')));
});
} catch (e) {
console.error(e);
process.exit(1);
}
async function loadPaths(config) {
const domains = Object.keys(config.domains);
const sortpaths = await Object.keys(config.paths).reduce((agg, path) => {
const basePath = `${config.directory}/${path}`;
const thumbPath = `thumbnails/${path}`;
const imgPath = `${path}`;
let maxDiff = 0;
const widths = config.screen_widths.map((width) => {
let threshold = '';
const logFilePath = `${basePath}/${width}_chrome_data.txt`;
const diff = fs.readFileSync(logFilePath,'utf8');
if ((diff * 1) > maxDiff) {
maxDiff = diff * 1;
}
if ((diff * 1) > config.threshold) {
threshold = 'threshold';
addLog(`WARN: ${path} failed at a resolution of ${width} (${diff}% diff)`);
}
return {
width,
diff,
threshold,
img1url: `${imgPath}/${width}_chrome_${domains[0]}.png`,
thumb1url: `${thumbPath}/${width}_chrome_${domains[0]}.png`,
img2url: `${imgPath}/${width}_chrome_${domains[1]}.png`,
thumb2url: `${thumbPath}/${width}_chrome_${domains[1]}.png`,
imgdiffurl: `${imgPath}/${width}_chrome_diff.png`,
thumbdiffurl: `${thumbPath}/${width}_chrome_diff.png`,
};
});
agg.push({
widths: widths,
maxDiff,
alias: path,
domain1url: `${config.domains[domains[0]]}${config.paths[path]}`,
domain2url: `${config.domains[domains[1]]}${config.paths[path]}`,
domain1name: domains[0],
domain2name: domains[1],
diffname: 'diff',
});
return agg;
}, []);
// At this point sortpaths is an array of objects, each object has widths
// (an array of objects) and maxwidth
sortpaths.sort((a, b) => (a.maxDiff * 100) < (b.maxDiff * 100))
return sortpaths;
}
async function saveGallery(config, variables) {
const template = fs.readFileSync(`${__dirname}/configs/${config.gallery.template}.mustache`, 'utf8');
const rendered = Mustache.render(template, variables);
fs.writeFileSync(`${config.directory}/gallery.html`, rendered);
}
function addLog(msg) {
console.log(msg);
}