-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathcreate-looks-same-differ.js
44 lines (40 loc) · 1.22 KB
/
create-looks-same-differ.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
const fs = require('fs-extra');
const looksSame = require('looks-same');
function createLooksSameDiffer(config) {
return function getImageDiff(path1, path2, diffPath, tolerance) {
const instanceConfig = { tolerance, ...config };
return new Promise(async (resolve, reject) => {
const [reference, current] = (
await Promise.all([fs.readFile(path1), fs.readFile(path2)])
).map(Buffer.from);
if (current.equals(reference)) {
return resolve(true);
}
if (reference.length === 0) {
return reject(new Error('Reference image is empty'));
}
if (current.length === 0) {
return reject(new Error('Current image is empty'));
}
try {
const { equal } = await looksSame(reference, current, instanceConfig);
if (equal) {
resolve(equal);
} else {
fs.ensureFileSync(diffPath);
const diff = await looksSame.createDiff({
...instanceConfig,
reference,
current,
diff: diffPath,
highlightColor: '#ff00ff',
});
resolve(diff);
}
} catch (err) {
reject(err);
}
});
};
}
module.exports = createLooksSameDiffer;