@@ -4,32 +4,34 @@ const { execSync } = require("child_process");
4
4
const path = require ( "path" ) ;
5
5
const fs = require ( "fs" ) ;
6
6
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 ) ;
8
12
9
- function compress ( file ) {
10
- console . log ( `Compressing ${ file } ` ) ;
11
13
// 需要先安装 https://pngquant.org/ 命令行工具
14
+ execSync ( `cp "${ file } " "${ file } .bak"` , { encoding : "utf-8" } ) ;
15
+ // execSync(`sips --resampleWidth 1920 "${file}"`, { encoding: "utf-8" });
12
16
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
+
17
18
const statsCompress = fs . statSync ( file . replace ( ".png" , "-fs8.png" ) ) ;
18
19
if ( statsOrigin . size > statsCompress . size ) {
19
20
execSync ( `rm -rf "${ file } "` ) ;
20
21
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"` ) ;
24
24
} else {
25
25
execSync ( `rm -rf "${ file . replace ( ".png" , "-fs8.png" ) } "` ) ;
26
+ execSync ( `rm -rf "${ file } "` ) ;
27
+ execSync ( `mv "${ file } .bak" "${ file } "` ) ;
26
28
}
27
29
execSync ( "sleep 0.2" ) ;
28
30
}
29
31
30
32
if ( cluster . isMaster ) {
31
33
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'
33
35
) ;
34
36
for ( let i = 0 ; i < numCPUs ; i += 1 ) {
35
37
cluster . fork ( ) ;
@@ -43,16 +45,13 @@ if (cluster.isMaster) {
43
45
if ( ( i % numCPUs ) + 1 !== + cluster . worker . id ) {
44
46
return ;
45
47
}
46
- if (
47
- ! file ||
48
- file . endsWith ( "/demo1-main.png" ) ||
49
- file . endsWith ( "-fs8.png" )
50
- ) {
48
+ if ( ! file || file . endsWith ( "-fs8.png" ) ) {
51
49
return ;
52
50
}
53
51
try {
54
- compress ( file ) ;
52
+ compress ( file , i ) ;
55
53
} catch ( e ) {
54
+ console . error ( e ) ;
56
55
console . log ( `Failed to compress ${ file } ` ) ;
57
56
execSync ( `rm -rf "${ file . replace ( ".png" , "-fs8.png" ) } "` ) ;
58
57
}
0 commit comments