@@ -7,18 +7,29 @@ import fs from "node:fs"
7
7
import os from "node:os"
8
8
import path from "node:path"
9
9
import { PNG } from "pngjs"
10
- import pixelmatch from "pixelmatch"
10
+ import pixelmatch from "pixelmatch"
11
11
import { asyncFolderWalker } from 'async-folder-walker'
12
12
13
- const basePath1 = os . homedir ( ) + '/Downloads/ screenshots-main/ '
14
- const basePath2 = os . homedir ( ) + '/Downloads/ screenshots-3/'
13
+ const basePath1 = '../ screenshots-main'
14
+ const basePath2 = "../ screenshots/"
15
15
16
16
iterateFiles ( )
17
17
18
- async function iterateFiles ( ) {
18
+ async function iterateFiles ( ) {
19
19
const walker = asyncFolderWalker ( [ basePath1 ] )
20
20
let tmpdir = ''
21
21
22
+ try {
23
+ tmpdir = os . tmpdir ( )
24
+ console . log ( tmpdir )
25
+ fs . mkdtempSync ( tmpdir )
26
+ tmpdir = path . join ( tmpdir , "diffs/" )
27
+ fs . mkdirSync ( tmpdir )
28
+ } catch ( e ) {
29
+ console . log ( e . message )
30
+ }
31
+
32
+
22
33
for await ( const file of walker ) {
23
34
if ( typeof file == 'string' ) {
24
35
if ( file . includes ( ".png" ) ) {
@@ -27,19 +38,21 @@ async function iterateFiles () {
27
38
const otherFile = file . replace ( basePath1 , basePath2 )
28
39
const img2 = PNG . sync . read ( fs . readFileSync ( otherFile ) ) ;
29
40
30
- const { width, height} = img1 ;
31
- const diff = new PNG ( { width, height} ) ;
32
- const result = pixelmatch ( img1 . data , img2 . data , diff . data , width , height , { threshold : 0.1 } )
33
- if ( result > 0 ) {
34
- console . log ( result , file , otherFile )
35
- if ( ! tmpdir ) {
36
- tmpdir = path . join ( os . tmpdir ( ) , 'diffs-' )
37
- fs . mkdtempSync ( tmpdir )
41
+ const { width, height } = img1 ;
42
+ const diff = new PNG ( { width, height } ) ;
43
+ try {
44
+ const result = pixelmatch ( img1 . data , img2 . data , diff . data , width , height , { threshold : 0.1 } )
45
+ if ( result > 0 ) {
46
+ console . log ( result , file , otherFile )
47
+
48
+ const dest = tmpdir + path . parse ( file ) . base
49
+ console . log ( "diff image saved to:" )
50
+ console . log ( dest )
51
+ fs . writeFileSync ( dest , PNG . sync . write ( diff ) ) ;
38
52
}
39
-
40
- const dest = tmpdir + path . parse ( file ) . base
41
- console . log ( "diff image saved to:" )
42
- console . log ( dest )
53
+ } catch ( e ) {
54
+ console . log ( e . message , file )
55
+ const dest = tmpdir + "err-" + path . parse ( file ) . base
43
56
fs . writeFileSync ( dest , PNG . sync . write ( diff ) ) ;
44
57
}
45
58
}
0 commit comments