-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinux-pre-commit
More file actions
97 lines (84 loc) · 3.47 KB
/
linux-pre-commit
File metadata and controls
97 lines (84 loc) · 3.47 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/bin/sh
# 安装 coreutils(如果尚未安装)
if ! command -v realpath >/dev/null 2>&1 || ! command -v cygpath >/dev/null 2>&1; then
if [ "$(uname)" = "Darwin" ]; then
# macOS
brew install coreutils
elif [ "$(uname)" = "Linux" ]; then
# Linux
sudo apt-get install coreutils
else
# Windows (Cygwin)
apt-cyg install coreutils
fi
fi
# 使用 cygpath 将 Windows 风格的路径转换为 Unix 风格的路径
# 对于非 Windows 系统,cygpath 可能不可用,因此我们使用 realpath
if [ "$(uname)" = "Darwin" ] || [ "$(uname)" = "Linux" ]; then
NODE_SCRIPT_UPDATE_MDFILES_JSON=$(realpath "../vercel_ci/update-mdfiles-json.js")
JSON_OUTPUT_PATH_MDFILES_JSON=$(realpath "../vercel_ci/jsons/mdfiles.json")
NODE_SCRIPT_GEN_SITEMAP=$(realpath "../vercel_ci/gen-sitemap.js")
XML_OUTPUT_PATH_SITEMAP=$(realpath "../vercel_ci/sitemap.xml")
NODE_SCRIPT_MD2HTML=$(realpath "../vercel_ci/md2html.js") # 添加 md2html 脚本的绝对路径
else
NODE_SCRIPT_UPDATE_MDFILES_JSON=$(cygpath -u "../vercel_ci/update-mdfiles-json.js")
JSON_OUTPUT_PATH_MDFILES_JSON=$(cygpath -u "../vercel_ci/jsons/mdfiles.json")
NODE_SCRIPT_GEN_SITEMAP=$(cygpath -u "../vercel_ci/gen-sitemap.js")
XML_OUTPUT_PATH_SITEMAP=$(cygpath -u "../vercel_ci/sitemap.xml")
NODE_SCRIPT_MD2HTML=$(cygpath -u "../vercel_ci/md2html.js") # 添加 md2html 脚本的绝对路径
fi
# 输出绝对路径
echo "Absolute path of Node.js script for updating mdfiles.json: $NODE_SCRIPT_UPDATE_MDFILES_JSON"
echo "Absolute path of generated JSON file for mdfiles.json: $JSON_OUTPUT_PATH_MDFILES_JSON"
echo "Absolute path of Node.js script for generating sitemap.xml: $NODE_SCRIPT_GEN_SITEMAP"
echo "Absolute path of generated XML file for sitemap.xml: $XML_OUTPUT_PATH_SITEMAP"
echo "Absolute path of Node.js script for md2html: $NODE_SCRIPT_MD2HTML" # 输出 md2html 脚本的绝对路径
# 检查 Node.js 脚本是否存在
if [ ! -f "$NODE_SCRIPT_UPDATE_MDFILES_JSON" ]; then
echo "Node.js script for updating mdfiles.json not found: $NODE_SCRIPT_UPDATE_MDFILES_JSON"
exit 1
fi
if [ ! -f "$NODE_SCRIPT_GEN_SITEMAP" ]; then
echo "Node.js script for generating sitemap.xml not found: $NODE_SCRIPT_GEN_SITEMAP"
exit 1
fi
if [ ! -f "$NODE_SCRIPT_MD2HTML" ]; then
echo "Node.js script for md2html not found: $NODE_SCRIPT_MD2HTML"
exit 1
fi
# 检查 Node.js 是否可用
if command -v node >/dev/null 2>&1; then
# 执行 Node.js 脚本以更新 mdfiles.json
node "$NODE_SCRIPT_UPDATE_MDFILES_JSON"
# 执行 md2html 脚本
node "$NODE_SCRIPT_MD2HTML"
# 执行 Node.js 脚本以生成 sitemap.xml
node "$NODE_SCRIPT_GEN_SITEMAP"
else
echo "Node.js is not installed or not in PATH."
exit 1
fi
# 函数用于检查文件是否存在且内容已变更
check_and_add_to_staging() {
local file_path=$1
if [ -f "$file_path" ]; then
git diff --quiet "$file_path" && git diff --staged "$file_path" > /dev/null
if [ $? -ne 0 ]; then
# 文件内容已变更,添加到暂存区
git add "$file_path"
fi
else
echo "Generated file not found: $file_path"
exit 1
fi
}
# 检查生成的 mdfiles.json 和 sitemap.xml 文件是否存在且内容已变更
check_and_add_to_staging "$JSON_OUTPUT_PATH_MDFILES_JSON"
check_and_add_to_staging "$XML_OUTPUT_PATH_SITEMAP"
# 如果有文件被添加到暂存区,则自动提交更改
if git diff --staged --quiet HEAD; then
echo "No changes to commit."
else
git commit -m "Update mdfiles.json, sitemap.xml, and generate HTML files"
fi
exit 0