33# 配置文件路径
44CONFIG_FILE=" /usr/share/nginx/html/config.js"
55
6+ echo " ========================================="
67echo " 开始生成运行时配置文件..."
8+ echo " 目标文件: $CONFIG_FILE "
9+ echo " ========================================="
10+
11+ # 检查目标目录是否存在
12+ TARGET_DIR=$( dirname " $CONFIG_FILE " )
13+ if [ ! -d " $TARGET_DIR " ]; then
14+ echo " ERROR: 目标目录不存在: $TARGET_DIR "
15+ mkdir -p " $TARGET_DIR " || echo " ERROR: 无法创建目录"
16+ fi
717
818# 构建包含全部 VITE_* 变量的运行时配置(同时注入带前缀与不带前缀的键)
919CONFIG_BODY=" "
1020COUNT=0
1121
22+ # 显示所有VITE_*环境变量用于调试
23+ echo " 扫描VITE_*环境变量..."
24+ env | grep ' ^VITE_' || echo " 未找到任何VITE_*变量"
25+ echo " ========================================="
26+
1227# 遍历所有以 VITE_ 开头的环境变量
1328for var in $( env | grep ' ^VITE_[A-Za-z0-9_]*=' | cut -d= -f1 | sort) ; do
1429 value=$( printenv " $var " 2> /dev/null)
@@ -20,9 +35,11 @@ for var in $(env | grep '^VITE_[A-Za-z0-9_]*=' | cut -d= -f1 | sort); do
2035
2136 # 追加属性:无前缀副本 与 带前缀副本
2237 if [ -n " $CONFIG_BODY " ]; then
23- CONFIG_BODY=" $CONFIG_BODY ,\n"
38+ CONFIG_BODY=" ${CONFIG_BODY} ,
39+ "
2440 fi
25- CONFIG_BODY=" $CONFIG_BODY $no_prefix_key : \" $escaped_value \" ,\n $var : \" $escaped_value \" "
41+ CONFIG_BODY=" ${CONFIG_BODY} ${no_prefix_key} : \" ${escaped_value} \" ,
42+ ${var} : \" ${escaped_value} \" "
2643
2744 COUNT=$(( COUNT + 1 ))
2845 echo " Found VITE var: $var "
@@ -39,5 +56,20 @@ ${CONFIG_BODY}
3956console.log("运行时配置已加载,共注入 ${COUNT} 个 VITE_* 变量(双份键)");
4057EOF
4158
59+ echo " ========================================="
4260echo " 配置文件已生成: $CONFIG_FILE "
4361echo " 已注入 VITE_* 变量数量: $COUNT "
62+ echo " ========================================="
63+
64+ # 验证文件是否成功生成
65+ if [ -f " $CONFIG_FILE " ]; then
66+ echo " ✅ 配置文件生成成功"
67+ echo " 文件大小: $( wc -c < " $CONFIG_FILE " ) bytes"
68+ echo " 前10行内容:"
69+ head -n 10 " $CONFIG_FILE "
70+ else
71+ echo " ❌ ERROR: 配置文件生成失败!"
72+ exit 1
73+ fi
74+
75+ echo " ========================================="
0 commit comments