blob: 2d392781099e6637bc7345cb2fb46a18a135f51b (
plain) (
blame)
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
|
#!/bin/sh
SRC="./public"
# 安装必要的工具(如果未安装)
if ! command -v uglifyjs &> /dev/null; then
echo "安装 uglify-js..."
npm install -g uglify-js
fi
if ! command -v cleancss &> /dev/null; then
echo "安装 clean-css-cli..."
npm install -g clean-css-cli
fi
if ! command -v html-minifier &> /dev/null; then
echo "安装 html-minifier-terser..."
npm install -g html-minifier-terser
fi
echo "开始原地压缩文件..."
# 创建临时目录用于备份
BACKUP_DIR=$(mktemp -d)
# 先备份原文件
echo "备份原文件..."
find "$SRC" -type f \( -name "*.js" -o -name "*.css" -o -name "*.html" \) | while read file; do
rel_path="${file#$SRC/}"
backup_path="$BACKUP_DIR/$rel_path"
mkdir -p "$(dirname "$backup_path")"
cp "$file" "$backup_path"
done
# 压缩文件(原地覆盖)
find "$SRC" -type f \( -name "*.js" -o -name "*.css" -o -name "*.html" \) | while read file; do
case "$file" in
*.js)
# 跳过已经是 .min.js 的文件
if [[ "$file" != *.min.js ]]; then
echo "压缩 JS: ${file#$SRC/}"
uglifyjs "$file" -c -m -o "${file}.tmp" && mv "${file}.tmp" "$file"
fi
;;
*.css)
# 跳过已经是 .min.css 的文件
if [[ "$file" != *.min.css ]]; then
echo "压缩 CSS: ${file#$SRC/}"
cleancss "$file" -o "${file}.tmp" && mv "${file}.tmp" "$file"
fi
;;
*.html)
echo "压缩 HTML: ${file#$SRC/}"
html-minifier "$file" \
--collapse-whitespace \
--remove-comments \
--minify-css \
--minify-js \
-o "${file}.tmp" && mv "${file}.tmp" "$file"
;;
esac
done
echo "压缩完成!"
# 执行 Hugo 构建和部署
commit_date=$(date +"%Y-%m-%d %H:%M:%S")
cd ~/blog/
hugo --cleanDestinationDir --gc
cd ~/blog/public || exit 1
git add .
git commit -m "auto update: $commit_date"
git push origin main
echo "部署完成!"
|