Skip to content

Commit 0395db3

Browse files
committed
chore: compress images
1 parent 97203bf commit 0395db3

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

compress.js

+16-17
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,34 @@ const { execSync } = require("child_process");
44
const path = require("path");
55
const fs = require("fs");
66

7-
const numCPUs = cpus().length;
7+
const numCPUs = 1; //cpus().length;
8+
9+
function compress(file, index) {
10+
console.log(`${index}: Compressing ${file}`);
11+
const statsOrigin = fs.statSync(file);
812

9-
function compress(file) {
10-
console.log(`Compressing ${file}`);
1113
// 需要先安装 https://pngquant.org/ 命令行工具
14+
execSync(`cp "${file}" "${file}.bak"`, { encoding: "utf-8" });
15+
// execSync(`sips --resampleWidth 1920 "${file}"`, { encoding: "utf-8" });
1216
execSync(`pngquant "${file}"`, { encoding: "utf-8" });
13-
const checkStats = execSync(`ls -l "${file}" | grep 'rwxr-xr-x'`, {
14-
encoding: "utf-8"
15-
});
16-
const statsOrigin = fs.statSync(file);
17+
1718
const statsCompress = fs.statSync(file.replace(".png", "-fs8.png"));
1819
if (statsOrigin.size > statsCompress.size) {
1920
execSync(`rm -rf "${file}"`);
2021
execSync(`mv "${file.replace(".png", "-fs8.png")}" "${file}"`);
21-
if (checkStats.includes("rwxr-xr-x")) {
22-
execSync(`chmod 755 "${file}"`);
23-
}
22+
execSync(`chmod 755 "${file}"`);
23+
execSync(`rm -rf "${file}.bak"`);
2424
} else {
2525
execSync(`rm -rf "${file.replace(".png", "-fs8.png")}"`);
26+
execSync(`rm -rf "${file}"`);
27+
execSync(`mv "${file}.bak" "${file}"`);
2628
}
2729
execSync("sleep 0.2");
2830
}
2931

3032
if (cluster.isMaster) {
3133
execSync(
32-
'find . -name "*.png" -type f -size +500kb -not -path "./node_modules/*" -not -path "./docs/.vuepress/dist/*" -not -path "./.git/*" > temp.log'
34+
'find ./docs -name "*.png" -type f -size +100kb -not -path "./node_modules/*" -not -path "./docs/.vuepress/dist/*" -not -path "./.git/*" > temp.log'
3335
);
3436
for (let i = 0; i < numCPUs; i += 1) {
3537
cluster.fork();
@@ -43,16 +45,13 @@ if (cluster.isMaster) {
4345
if ((i % numCPUs) + 1 !== +cluster.worker.id) {
4446
return;
4547
}
46-
if (
47-
!file ||
48-
file.endsWith("/demo1-main.png") ||
49-
file.endsWith("-fs8.png")
50-
) {
48+
if (!file || file.endsWith("-fs8.png")) {
5149
return;
5250
}
5351
try {
54-
compress(file);
52+
compress(file, i);
5553
} catch (e) {
54+
console.error(e);
5655
console.log(`Failed to compress ${file}`);
5756
execSync(`rm -rf "${file.replace(".png", "-fs8.png")}"`);
5857
}

0 commit comments

Comments
 (0)