-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiff.js
47 lines (42 loc) · 1.21 KB
/
diff.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
const BlinkDiff = require( 'blink-diff' );
const fs = require( 'fs' );
const DIFF_PATH = process.env.DIFF_PATH || 'photos-diffs';
const IMAGE_A_PATH = process.env.IMAGE_A_PATH || 'photos-a';
const IMAGE_B_PATH = process.env.IMAGE_B_PATH || 'photos-b';
/**
* Run using `npm run diff`. Optionally pass in custom folders using env variables.
*
* Example:
* IMAGE_A_PATH=screenshots-a IMAGE_A_PATH=screenshots-b DIFF_PATH=results npm run diff
*/
function runDiff( image ) {
if ( image[ 0 ] === '.' ) {
return;
}
const diff = new BlinkDiff( {
imageAPath: `${ IMAGE_A_PATH }/${ image }`,
imageBPath: `${ IMAGE_B_PATH }/${ image }`,
imageOutputPath: `./${ DIFF_PATH }/${ image }`,
threshold: 50, // if at least 50 pixels are different, this will "fail".
} );
return new Promise( ( resolve, reject ) => {
diff.run( ( error, result ) => {
if ( error ) {
reject( error );
} else {
resolve( diff.hasPassed( result.code ), result );
}
} );
} );
}
( async () => {
try {
const files = await fs.promises.readdir( `./${ IMAGE_A_PATH }` );
for ( const file of files ) {
const result = await runDiff( file );
console.log( file, result );
}
} catch ( error ) {
console.error( error );
}
} )();