-
Notifications
You must be signed in to change notification settings - Fork 0
/
zip.utils.ts
47 lines (40 loc) · 1.28 KB
/
zip.utils.ts
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
import type { IZipEntry } from 'adm-zip';
import AdmZip from 'adm-zip';
import { DEFAULT_BOT_CONFIGS } from '../constants';
export function getFilesFromZipFile(
zipFile: ArrayBuffer | Buffer
): IZipEntry[] {
const zip = new AdmZip(
Buffer.isBuffer(zipFile) ? zipFile : Buffer.from(zipFile)
);
return zip.getEntries().filter((entry) => !entry.isDirectory);
}
function isImage(file: IZipEntry): boolean {
return new RegExp('\\.(png|jpeg|jpg|gif|svg)$', 'gi').test(file.entryName);
}
export function findScreenshotDiffImages(
zipFile: ArrayBuffer | Buffer,
screenshotsDiffsPaths?: string[]
): IZipEntry[] {
const files = getFilesFromZipFile(zipFile);
const diffsPaths =
screenshotsDiffsPaths || DEFAULT_BOT_CONFIGS.screenshotsDiffsPaths;
return files
.filter((file) =>
diffsPaths.some((regExp) =>
new RegExp(regExp, 'gi').test(file.entryName)
)
)
.filter(isImage);
}
export function findNewScreenshotImages(
zipFile: ArrayBuffer | Buffer,
newScreenshotMark?: string
) {
return getFilesFromZipFile(zipFile).filter((file) =>
new RegExp(
newScreenshotMark || DEFAULT_BOT_CONFIGS.newScreenshotMark,
'gi'
).test(file.entryName)
);
}