-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmake.js
230 lines (179 loc) · 7 KB
/
make.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
var fs = require('q-io/fs'),
q = require('q'),
tools = require('./toolbelt'),
colors = require('colors');
colors.setTheme({
tick: 'green',
title: 'underline',
info: 'grey'
});
var prompt = function prompt(log, tick) {
console.log((tick ? tick.tick : '› '.tick) + log);
};
console.time('› '.tick + 'work took me');
var directories = ['candidate', 'current', 'diff'],
innerDirectories = ['candidate/hq', 'current/hq', 'diff/hq', 'candidate/html', 'current/html'],
innerDirectoriesLevel2 = ['candidate/html/failed', 'current/html/failed'],
jsonFiles = ['report.json', 'paths.json'];
var qForEach = function qForEach(func, list) {
return function(res) {
var arr = [];
(list ? list : res).forEach(function(listEl) {
arr.push(func(listEl));
});
return q.all(arr);
}
};
var step = function step(func, tick) {
return function(res) {
prompt(tick ? tick : 'taking next step');
return res ? func(res) : func();
}
};
var promiseResult = function promiseResult(func) {
return function(res) {
var deffered = q.defer();
deffered.resolve(func(res));
return deffered.promise;
}
};
var qChain = function qChain(func, list) {
var promiseChain = q.fcall(function() {});
list.forEach(function(listEl) {
var promiseLink = func.bind(this, listEl);
promiseChain = promiseChain.then(promiseLink);
});
return promiseChain;
};
var removeTreeIfDirExists = function (path) {
return fs.exists(path)
.then(function (exists) {
if(exists) {
prompt(path +' directory deleted');
return fs.removeTree(path);
}
});
};
var program = function() {
q.fcall(qForEach(removeTreeIfDirExists, directories))
.then(qForEach(fs.makeDirectory.bind(fs), directories))
.then(qForEach(fs.makeDirectory.bind(fs), innerDirectories))
.then(qForEach(fs.makeDirectory.bind(fs), innerDirectoriesLevel2))
.then(
qForEach(function(file) {
fs.write(file, '')
}, jsonFiles))
.then(
step(fs.list.bind(fs, 'tasks'), 'hello sir/madame. peddif is ready to work'))
.then(promiseResult(tools.jsOnly))
.then(step(qForEach(function(task) {
var path = 'casperjs run.js --web-security=no ' + task,
result = tools.exec(path);
prompt('executing ' + task + ' task');
return result
.then(function(msg) {
prompt(task + ' task completed', '» ');
})
.fail(function(msg) {
console.error(task + ' failed');
// console.error(msg.error);
// stdout is, in fact, more interesting, than error msg, even when task failed
console.log(msg.stdout);
process.exit();
});
}), 'starting tasks execution'))
.then(function() {
prompt('all tasks executed. screenshots are taken');
var imagesLists = ['candidate', 'current'].map(function(dir) {
return fs.list(dir);
});
return q.all(imagesLists);
})
.then(function(imagesLists) {
prompt('calculating differences between screenshots');
var promiseChain = q.fcall(function() {});
imagesLists = imagesLists.map(function(list) {
return tools.imagesOnly(list);
});
imagesLists[0].forEach(function(image) {
var promiseLink = function() {
var file = image,
current = 'current/' + file,
diff = 'diff/' + file,
candidate = 'candidate/' + file,
deffered = q.defer();
tools.equateImagesHeight([current, candidate])
.then(function() {
return tools.compareImages([tools.dirify(current), tools.dirify(candidate)], tools.dirify(diff))
})
.then(function(res) {
var diffFactor, moved = [];
if(res.equality === 0) {
diffFactor = '10000';
} else if(res.equality === 1) {
diffFactor = '00001';
} else {
diffFactor = (1 - res.equality).toString().replace('.', '').substr(1,4);
}
moved.push(fs.move(current, 'current/' + diffFactor + '_' + file));
moved.push(fs.move(candidate, 'candidate/' + diffFactor + '_' + file));
moved.push(fs.move(diff, 'diff/' + diffFactor + '_' + file));
deffered.resolve(q.all(moved));
});
return deffered.promise;
};
promiseChain = promiseChain.then(promiseLink);
});
return promiseChain;
})
.then(step(qChain.bind(this, function(dir) {
var deffered = q.defer();
fs.list(dir)
.then(function(list) {
var stripped = [];
tools.imagesOnly(list).forEach(function(image) {
stripped.push(tools.stripToJpg(dir + '/' + image));
});
deffered.resolve(q.all(stripped));
});
return deffered.promise;
}, directories), 'generating previews'))
.then(step(qChain.bind(this, function(dir) {
var deffered = q.defer();
fs.list(dir).then(function(list) {
var moved = [];
tools.pngOnly(list).forEach(function(image) {
moved.push(fs.move(dir + '/' + image, dir + '/hq/' + image));
});
deffered.resolve(q.all(moved));
});
return deffered.promise;
}, directories), 'preparing images'))
.then(function() {
prompt('generating report');
return tools.exec('casperjs report.js');
})
.then(function(res) {
return tools.exec('casperjs coverage.js');
})
.then(function(res) {
return fs.list('diff');
})
.then(function(list) {
var screensTaken = tools.imagesOnly(list).length * 2;
prompt('aye sir! work is finished\n');
prompt('info:'.title);
prompt(screensTaken + ' screenshots has been taken');
console.timeEnd('› '.tick + 'work took me');
prompt('thanks for cooperation');
})
.fail(function(error) {
console.error(error);
if (error.errno === 34) {
prompt('tryin again');
program();
}
})
.done();
};
program();