forked from polubis/4markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiffs.js
More file actions
97 lines (86 loc) · 2.04 KB
/
diffs.js
File metadata and controls
97 lines (86 loc) · 2.04 KB
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
const fs = require(`fs`);
const path = require(`path`);
const { PNG } = require(`pngjs`);
const pixelmatch = require(`pixelmatch`);
const args = process.argv;
const suiteName = args[2];
const createDiffs = () => {
const baselineDirPath = path.join(
__dirname,
`cypress`,
`screenshots`,
`baseline`,
suiteName,
);
fs.readdir(baselineDirPath, (err, files) => {
if (err) {
console.error(`Error reading directory:`, err);
return;
}
// Filter out subdirectories
const filePaths = files.filter((file) =>
fs.statSync(path.join(baselineDirPath, file)).isFile(),
);
filePaths.forEach((file) => {
const img1 = PNG.sync.read(
fs.readFileSync(
path.join(
__dirname,
`cypress`,
`screenshots`,
`baseline`,
suiteName,
file,
),
),
);
const img2 = PNG.sync.read(
fs.readFileSync(
path.join(
__dirname,
`cypress`,
`screenshots`,
`current`,
suiteName,
file,
),
),
);
const { width, height } = img1;
const diff = new PNG({ width, height });
const numDiffPixels = pixelmatch(
img1.data,
img2.data,
diff.data,
width,
height,
{ threshold: 0.1 },
);
if (numDiffPixels > 0) {
try {
fs.mkdirSync(path.join(__dirname, `cypress`, `screenshots`));
} catch {}
try {
fs.mkdirSync(path.join(__dirname, `cypress`, `screenshots`, `diffs`));
} catch {}
try {
fs.mkdirSync(
path.join(__dirname, `cypress`, `screenshots`, `diffs`, suiteName),
);
} catch {}
fs.writeFileSync(
path.join(
__dirname,
`cypress`,
`screenshots`,
`diffs`,
suiteName,
file,
),
PNG.sync.write(diff),
);
}
});
});
};
createDiffs();