-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
63 lines (57 loc) · 1.7 KB
/
index.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const core = require("@actions/core");
const util = require("util");
const execFileSync = require("node:child_process").execFileSync;
const glob = util.promisify(require("glob"));
async function main() {
try {
const { default: zopflipng } = await import("zopflipng-bin");
const paths = core.getMultilineInput("image-paths");
const globPaths = paths.filter((path) =>
path.toLowerCase().endsWith(".png"),
);
const limit = core.getInput("limit");
const globPromises = globPaths.map((globPath) =>
glob(globPath, {
nodir: true,
follow: false,
}),
);
const results = await Promise.all(globPromises);
const imagePaths = results.flat();
if (imagePaths.length === 0) {
console.log("Files are not found.");
return;
}
// find saving rate
const regexp = /Percentage of original: ([\d.]+)%/m;
const largeImages = imagePaths.reduce((prev, imagePath) => {
console.log(`Check ${imagePath}`);
const stdoutBuffer = execFileSync(zopflipng, [
"-d",
imagePath,
"output.png",
]);
const stdout = stdoutBuffer.toString();
const matcher = stdout.match(regexp);
if (matcher) {
const savingRate = Number(matcher[1]);
if (savingRate < Number(limit)) {
prev = [...prev, imagePath];
}
}
return prev;
}, []);
if (largeImages.length > 0) {
throw new Error(
`Found files to be able to be reduced size\n${largeImages.join("\n")}`,
);
}
} catch (error) {
const zopflipngError = error?.stdout?.toString();
if (zopflipngError) {
console.error(zopflipngError);
}
core.setFailed(error.message);
}
}
main();