#!/bin/bash
## 权限问题，用  chmod u+x *.sh
## eg: oper.sh 

# 用户名  
# 项目名 
# 操作 (create , restart , start , kill ,  stop , del , build , sync)
# 部署路径 (upload)
# 同步内容 (node_modules , build , cli)

cmdDir=$(
  cd $(dirname "$0")
  pwd
)

username=$1     # 用户名
projectName=$2  # 项目名
cmd=$3          # 命令
port=$4 # 端口
domain_name=$5 # 域名
# *.pre2.hieap.cn==>http://192.168.4.192:8001
# *.pre.hieap.cn==>http://192.168.10.242:31167

# ip=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"​`
# tagIp193="192.168.4.193"
# if [[ $ip == *$tagIp193* ]]; then  
#     DEV_BASE_API='http://192.168.4.106:7777/' #传给配置 .env.development 的 VITE_APP_BASE_API 做后端请求数据
# elif [[]];then
#     # 原192.168.4.192 内网  ,现在是 192.168.4.110  , docker 内ip 172.20.0.3
#     DEV_BASE_API='http://e52.hieap.cn/' #传给配置 .env.development 的 VITE_APP_BASE_API 做后端请求数据 
# else
#     # pass 环境 对外 
#     DEV_BASE_API='http://e5.hieap.cn/' #传给配置 .env.development 的 VITE_APP_BASE_API 做后端请求数据 
# fi

domain_193='192.168.4.106'
domain_open2='open2.hieap.cn'
domain_open='open.hieap.cn'

if [[ $domain_name == *$domain_193* ]];then
    DEV_BASE_API='http://192.168.4.106:7777/' #传给配置 .env.development 的 VITE_APP_BASE_API 做后端请求数据
fi
if [[ $domain_name == *$domain_open2* ]];then
    # 原192.168.4.192 内网  ,现在是 192.168.4.110  , docker 内ip 172.20.0.3
    DEV_BASE_API='http://e52.hieap.cn/' #传给配置 .env.development 的 VITE_APP_BASE_API 做后端请求数据 
fi
if [[ $domain_name == *$domain_open* ]];then
    # pass 环境 对外 
    DEV_BASE_API='http://e5.hieap.cn/' #传给配置 .env.development 的 VITE_APP_BASE_API 做后端请求数据 
fi




# tagIp192="192.168.4.192"
nginxStr='' 

function getNginx(){
    nginxStr='' 
    # if [[ $ip == *$tagIp193* ]]; then
    #     nginxStr=''
    # elif [[ $ip == *$tagIp192* ]]; then
    #     # *.pre2.hieap.cn==>http://192.168.4.192:8001   
    #     # 宿主机 4.110 ，docker 内ip 172.20.0.3 
    #     # 内网用户ng 配置
    #     nginxStr='"'${projectName}'.'${username}'.pre2.hieap.cn" "'${1}'";'
    # else
    #     # *.pre.hieap.cn==>http://192.168.10.242:32467
    #     # pass 下的 外网用户 ng 配置
    #     nginxStr='"'${projectName}'.'${username}'.pre.hieap.cn" "'${1}'";'
    # fi

    if [[ $domain_name == *$domain_193* ]];then
        nginxStr=''
    fi
    if [[ $domain_name == *$domain_open2* ]];then
        # *.pre2.hieap.cn==>http://192.168.4.192:8001   
        # 宿主机 4.110 ，docker 内ip 172.20.0.3 
        # 内网用户ng 配置
        nginxStr='"'${projectName}'.'${username}'.pre2.hieap.cn" "'${1}'";'
    fi
    if [[ $domain_name == *$domain_open* ]];then
        # *.pre.hieap.cn==>http://192.168.10.242:32467
        # pass 下的 外网用户 ng 配置
        nginxStr='"'${projectName}'.'${username}'.pre.hieap.cn" "'${1}'";'
    fi

}


#BASH_SOURCE[0]用户所在的目录到脚本的路径
## 获取eap5.0 路径
# eap5Dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
eap5Dir="${cmdDir%/hiruntime*}"
projectPath=${eap5Dir}/desktop/preview
tmpPath=${eap5Dir}/hiruntime/project/desktop/project-template

source=''
source=$source'\t<% if(htmlWebpackPlugin.vite){%>\n\t<!--预览时 公共库-->'
source=$source'\n\t<script src="\/'${projectName}'\/setting\/desktop\/development.js" type="text\/javascript"><\/script>'
source=$source'\n\t<%}%>'
source=$source'\n\t<% if(!htmlWebpackPlugin.vite){%>\n\t<!--发布时 公共库-->'
source=$source'\n\t<script src="\/${serverName}\/render\/'${username}'\/'${projectName}'\/setting\/desktop\/appsetting.js" type="text\/javascript"><\/script>'
source=$source'\n\t<%}%>\n'

viteStr=''
viteStr=$viteStr'let socketHost = `${__HMR_HOSTNAME__ || location.hostname}:${__HMR_PORT__}`;'
viteStr=$viteStr'let socketHostSplit = socketHost.split(".");'
viteStr=$viteStr'let reg=/^[0-9]*$/gi;'
viteStr=$viteStr'if(reg.test(socketHostSplit[0])==false){socketHost = socketHost.substring(0,socketHost.indexOf(":"));}'


nrm del zhc
nrm add zhc  http://npm.hieap.cn/
nrm use zhc

case $cmd in 
"create") #创建
    echo '正在创建桌面端应用...'$(date +%F%n%T)
    #参数个数判断，至少4个
    if [ $# -lt 4 ];then
        echo "创建项目命令参数不对：$*"
        echo "创建项目命令正确参数：用户名 项目名 create 桌面预览端口"
        exit 1
    fi
    # source ${cmdDir}/var_env.sh
    # echo ${DEV_BASE_API} 
    echo eap5Dir: ${eap5Dir}
    echo projectPath: ${projectPath}
    
    if [ ! -d "${projectPath}/${username}" ];then
        mkdir "${projectPath}/${username}" #创用户工作空间
    fi
    if [ -d "${projectPath}/${username}/${projectName}" ];then
        echo "项目名已经存在！"
        exit 1
    fi
    echo "<1>git正在拉取web端项目基础文件"
    git clone http://192.168.4.105:8080/gitbucket/git/08335/hivui-platform-template.git "${projectPath}/${username}/${projectName}/deploy"

    echo "<2>初始化node环境..."
    # rsync -auz ${tmpPath}/node_modules ${projectPath}/${username}/${projectName}/deploy/
    # cp这个命令在复制零碎文件的时候是很慢的 
    rsync -auz ${tmpPath}/node_modules ${projectPath}/${username}/${projectName}/deploy/    
    cd ${projectPath}/${username}/${projectName}/deploy/
    rm -rf ./build/bin
    rm -rf ./doc

    echo "<3>初始化相关配置..."
    ## 修改模板，重命项目名
    mv project ${projectName}
    sed -i 's#\(VITE_APP_USERPATH \?= \?\).*#\1'"/e5/render/${username}"'#g' .env.production
    sed -i 's#\(VITE_APP_PN \?= \?\).*#\1'"${projectName}/${username}"'#g' ${projectPath}/${username}/${projectName}/deploy/.env.development
    sed -i 's#\(VITE_APP_BASE_API \?= \?\).*#\1'"${DEV_BASE_API}"'#g' .env.development

    ## 替换 package.json 的 name 值 author值 ，打包时候输出静态地址是这个
    sed -i 's#\("name": \?"\).*#\1'"${projectName}"'",#g' package.json
    sed -i 's#\("author": \?"\).*#\1'"${username}"'",#g' package.json
    ## 替换 processes.json 的 args 值
    sed -i '4s#\("name": \?"\).*#\1'"vite-${username}-${projectName}"'",#g' processes.json
    sed -i '7s#\("args": \?"\).*#\1'"--host --port ${port}"'",#g' processes.json

    cd ${projectPath}/${username}/${projectName}/deploy/${projectName}/hivuiLogin
    sed -i 's/<\/head>/'"${source}"'<\/head>/g' index.html

    cd ${projectPath}/${username}/${projectName}/deploy/${projectName}/hivuiMain
    sed -i 's/<\/head>/'"${source}"'<\/head>/g' index.html

    cd ${projectPath}/${username}/${projectName}/deploy/${projectName}/hivuiBirt
    sed -i 's/<\/head>/'"${source}"'<\/head>/g' index.html

    cd ${projectPath}/${username}/${projectName}/deploy/${projectName}/setting/desktop/appsetting/js/1.0.0
    sed -i 's#\(projectName \?= \?\).*#\1"'"${projectName}"'";#g' appsetting.js

    echo "<4>配置Nginx环境"

    # /usr/local/nginx/conf/nginx.conf
    cd /usr/local/nginx/conf

    getNginx $port
    sed -i "/${projectName}.${username}.pre/d" nginx.conf
    sed -i "/map \$host \$proxy_port{/a ${nginxStr}" nginx.conf

    cd /usr/local/nginx/sbin
    # 清缓存
    rm -rf  /usr/local/nginx/proxy_temp 
    ./nginx -s reload

    echo "<5>配置node_modules/vite环境"
    # node_modules/vite 环境
    cd ${projectPath}/${username}/${projectName}/deploy/node_modules/vite/dist/client 
    sed -i "s#const.*socketHost.*=.*#${viteStr}#g" client.js
    sed -i "s#case 'update':.*#case 'update':return;#g" client.js
    sed -i "s#case.*'full-reload':.*#case 'full-reload':return;#g" client.js

    echo "<6>正在启动服务..."
    cd ${projectPath}/${username}/${projectName}/deploy/
    npm run pm2:start
    pm2 save
    echo "桌面端创建完毕！"$(date +%F%n%T)
    exit 1
;;
"delete") #删除
    #参数个数判断，至少3个
    if [ $# -lt 3 ];then
        echo "删除项目命令参数不对：$*"
        echo "删除项目命令正确参数：用户名 项目名 delete"
        exit 1
    fi

    if [ $username = "admin" -a $projectName = "systpl" ] ; then
        echo "系统模板，不可删除！"
        exit 1
    fi

    curPid=`pm2 pid vite-${username}-${projectName}`
    if [ ! -z "$curPid" ];then
        echo "vite-${username}-${projectName} 正在关闭服务..."
        pm2 delete "vite-${username}-${projectName}" 
    fi
    
    # 删除配nginx
    cd /usr/local/nginx/conf
    #删除
    sed -i "/${projectName}.${username}.pre/d" nginx.conf
    # 清缓存
    rm -rf  /usr/local/nginx/proxy_temp 
    ./nginx -s reload


    ### 删除用户项目
    rm -rf ${projectPath}/${username}/${projectName}
    echo "用户：${username}  项目：${projectName}  删除成功！"
    pm2 save
    

    exit 1
;;
"restart") #重启
    echo "》》》正在重启服务..."
    curPid=`pm2 pid vite-${username}-${projectName}`
    if [ ! -z "$curPid" ];then
        echo "vite-${username}-${projectName} 正在关闭服务..."
        pm2 delete "vite-${username}-${projectName}" 
    fi
    cd ${projectPath}/${username}/${projectName}/deploy/
    npm run rm:vite
    npm run pm2:start
    pm2 save
    echo "已经重启服务！！"
    exit 1
;;
"build") #编译
    uploadPath=$4  
    modules=$5
    #参数个数判断，至少3个
    # if [ $# -lt 4 ];then
    #     echo "编译项目命令参数不对：$*"
    #     echo "编译项目命令正确参数：用户名 项目名 build 上传服务器路径 [指定编译模块]"
    #     exit 1
    # fi

    ### 用pm2管理，用pm2 重启指定的
    rm -rf "${projectPath}/${username}/${projectName}/deploy/dist" 
    cd "${projectPath}/${username}/${projectName}/deploy"

    buildFlag="building.txt"
    # touch filename
    if [ ! -f "$buildFlag" ];then
        touch "$buildFlag"
    else
        echo "已经处于编译状态，请等待..."
        exit 1;
    fi

    if [ ! $modules ]; then
        echo "正在执行编译所有模块..."$(date +%F%n%T)
        npm run build
        ### 编译所有的时候要考相关配置
        cd "${projectPath}/${username}/${projectName}/deploy/"
        if [[ ! -d "${projectPath}/${username}/${projectName}/deploy/dist/${projectName}/funclib/desklib" ]]; then
            mkdir -p "${projectPath}/${username}/${projectName}/deploy/dist/${projectName}/funclib/desklib"
        fi
        rsync -auz "${projectPath}/${username}/${projectName}/deploy/${projectName}/funclib/desklib" "${projectPath}/${username}/${projectName}/deploy/dist/${projectName}/funclib/"     
    else
        echo "正在执行编译【指定模块】..."$(date +%F%n%T)
        npm run build:modules $modules
    fi
    rm -rf "$buildFlag"

    ## 删除之前打包文件 
    rm -rf "${username}_${projectName}.zip"
    cd "${projectPath}/${username}/${projectName}/deploy/dist" 
    mkdir -p "${projectPath}/${username}/${projectName}/deploy/dist/${username}/${projectName}"
    rsync -auz "${projectPath}/${username}/${projectName}/deploy/dist/${projectName}" "${projectPath}/${username}/${projectName}/deploy/dist/${username}/"

    rm -rf "${projectPath}/${username}/${projectName}/deploy/dist/${projectName}"

    zip -q -r "${username}_${projectName}.zip"  ./*
    rsync -auz "${projectPath}/${username}/${projectName}/deploy/dist/${username}_${projectName}.zip" "${projectPath}/${username}/${projectName}/deploy/"
    rm -rf "${projectPath}/${username}/${projectName}/deploy/dist/${username}_${projectName}.zip"
    cd ${projectPath}/${username}/${projectName}/deploy/

    if [ -z "$uploadPath" ]; then
        echo '未传服务器路径，请自行拷贝！'
        exit 1;
    fi
    if [[ ! -d "$uploadPath" ]]; then
        echo "编译命令： 上传文件夹不存在, 正在帮您创建目录..."
        mkdir -p "${uploadPath}"
    fi
    echo "正在拷到服务器目录：${uploadPath}" 
    cp -fv "${username}_${projectName}.zip"  "${uploadPath}"

    echo "(‐^▽^‐) 编译打包成功！"$(date +%F%n%T)
;;
"sync") #同步环境
    #参数个数判断，至少4个
    if [ $# -lt 4 ];then
        echo "同步项目命令参数不对：$*"
        echo "同步项目命令正确参数：用户名 项目名 sync 同步方式"
        exit 1
    fi
    way=$4    

    if [ $way = 'sysfunc' ];then  #同步系统功能      
        sysfuncFlagPath="${projectPath}/${username}/${projectName}/hc-birt-platf-更新中"
        if [[ ! -d "${sysfuncFlagPath}" ]]; then
            mkdir -p "${sysfuncFlagPath}"
        else
            echo "已经相关人员同步中，请稍候...";
            exit 1;
        fi
        echo "》》》同步系统功能..."$(date +%F%n%T)                                                   
        git clone http://192.168.4.105:8080/gitbucket/git/js-team2/zhc-birt-platf.git ${eap5Dir}/hiruntime/project/desktop/__zhc-birt-platf
        rm -rf ${eap5Dir}/hiruntime/project/desktop/zhc-birt-platf
        rsync -auz ${eap5Dir}/hiruntime/project/desktop/__zhc-birt-platf/project/* ${eap5Dir}/hiruntime/workspace/${username}/${projectName}/
        rm -rf ${eap5Dir}/hiruntime/project/desktop/__zhc-birt-platf
        echo "》》》系统功能已同步！"$(date +%F%n%T) 
        # 删除临时文件标识，以准备下次更新判断
        rm -rf "${sysfuncFlagPath}"  
        exit
    fi


    if [ $way = 'nginx' ];then #nginx环境
        #获取processes.json 的端口
        portStr=$(cat ${projectPath}/${username}/${projectName}/deploy/processes.json | sed 's/,/\n/g' | grep "args" | sed 's/:/\n/g' | sed '1d' | sed 's/"//g')
        portNum=`echo $portStr | tr -cd "[0-9]"`
        # echo $portNum
        if [ $portNum = '' ];then
            echo '取不到端口信息！'
            exit  1;
        fi

        cd /usr/local/nginx/conf

        #设置相关配置 返回到nginxStr
        getNginx $portNum
        #删除
        sed -i "/${nginxStr}/d" nginx.conf
        echo '删除nginx相关配置！'
        #添加
        sed -i "/map \$host \$proxy_port{/a ${nginxStr}" nginx.conf
        echo ${nginxStr}
        echo '添加nginx相关配置！'
       
        cd /usr/local/nginx/sbin
        # 清缓存
        rm -rf  /usr/local/nginx/proxy_temp 
        ./nginx -s reload
        echo "》》》nginx环境已同步！"
        exit 1;
    fi

    
    

    # 以下注释不能用，多人点击同步文件会缺失
    # rm -rf ${eap5Dir}/hiruntime/project/desktop/project-template
    # rsync -auz ${eap5Dir}/hiruntime/project/desktop/__gittmp ${eap5Dir}/hiruntime/project/desktop/project-template
    # rm -rf ${eap5Dir}/hiruntime/project/desktop/__gittmp

    case $way in 
    "build") #同步打包环境
        echo "》》》正在同步打包环境..."$(date +%F%n%T)
        rsync -auz ${tmpPath}/build ${projectPath}/${username}/${projectName}/deploy/
        rsync -auz ${tmpPath}/babel.config.js ${projectPath}/${username}/${projectName}/deploy/
        rsync -auz ${tmpPath}/.postcssrc.js ${projectPath}/${username}/${projectName}/deploy/
        rsync -auz ${tmpPath}/.browserslistrc ${projectPath}/${username}/${projectName}/deploy/
        rsync -auz ${tmpPath}/vite-plugin ${projectPath}/${username}/${projectName}/deploy/
        echo "》》》打包环境已同步！"$(date +%F%n%T)
        exit 1
    ;;
    "hiui") #同步hiui资源库
        echo "》》》正在同步hi-ui库..."$(date +%F%n%T)
        cd "${tmpPath}/"
        hiuiFlagPath="${projectPath}/${username}/${projectName}/hi-ui-更新中"
        if [[ ! -d "${hiuiFlagPath}" ]]; then
            mkdir -p "${hiuiFlagPath}"
        else
            echo "已经相关人员同步中，请稍候...";
            exit 1;
        fi
        npm i hi-ui -S
        rm -rf ${projectPath}/${username}/${projectName}/deploy/node_modules/hi-ui
        rsync -auz ${tmpPath}/node_modules/hi-ui ${projectPath}/${username}/${projectName}/deploy/node_modules/
        ## 取hi-ui版本 并更新root package.json
        ## cd ${projectPath}/${username}/${projectName}/deploy/node_modules/hi-ui/
        ## sed -i 's#\("name": \?"\).*#\1'"${projectName}"'",#g' package.json
        echo "hiui已同步！"$(date +%F%n%T)
        # 删除临时文件标识，以准备下次更新判断
        rm -rf "${hiuiFlagPath}"
    ;;
    "taskcenter") #同步环境
        echo "》》》正在同步任务中心..."$(date +%F%n%T)
        cd "${tmpPath}/"
        taskcenterFlagPath="${projectPath}/${username}/${projectName}/hi-taskcenter-更新中"
        if [[ ! -d "${taskcenterFlagPath}" ]]; then
            mkdir -p "${taskcenterFlagPath}"
        else
            echo "已经相关人员同步中，请稍候...";
            exit 1;
        fi
        npm i hi-taskcenter -S
        rm -rf ${projectPath}/${username}/${projectName}/deploy/node_modules/hi-taskcenter
        rsync -auz ${tmpPath}/node_modules/hi-taskcenter ${projectPath}/${username}/${projectName}/deploy/node_modules/
        ## 取hi-taskcenter版本 并更新root package.json
        ## cd ${projectPath}/${username}/${projectName}/deploy/node_modules/hi-taskcenter/
        ## sed -i 's#\("name": \?"\).*#\1'"${projectName}"'",#g' package.json

        echo "任务中心已同步！"$(date +%F%n%T)
        # 删除临时文件标识，以准备下次更新判断
        rm -rf "${taskcenterFlagPath}"
    ;;
    "cli") #程序环境
        echo "开始同步..."$(date +%F%n%T)
        if [[ ! -d "${eap5Dir}/hiruntime/project/desktop/__gittmp" ]]; then
            #可以更新
            git clone http://192.168.4.105:8080/gitbucket/git/08335/hivui-platform-template.git ${eap5Dir}/hiruntime/project/desktop/__gittmp
            rsync -auz ${eap5Dir}/hiruntime/project/desktop/__gittmp/ ${tmpPath}
        else
            echo "已经相关人员同步中，请稍候...";
            exit 1;
        fi
        
        rsync -auz ${tmpPath}/assets_platform ${projectPath}/${username}/${projectName}/deploy/
        rsync -auz ${tmpPath}/index.html ${projectPath}/${username}/${projectName}/deploy
        rsync -auz ${tmpPath}/template.html ${projectPath}/${username}/${projectName}/deploy/
        echo "》》》静态共用库已同步！"

        rsync -auz ${tmpPath}/project/hivuiLogin ${projectPath}/${username}/${projectName}/deploy/${projectName}/
        cd ${projectPath}/${username}/${projectName}/deploy/${projectName}/hivuiLogin
        sed -i 's/<\/head>/'"${source}"'<\/head>/g' index.html
        echo "》》》登录模板已同步！"

        rsync -auz ${tmpPath}/project/hivuiMain ${projectPath}/${username}/${projectName}/deploy/${projectName}/
        cd ${projectPath}/${username}/${projectName}/deploy/${projectName}/hivuiMain
        sed -i 's/<\/head>/'"${source}"'<\/head>/g' index.html
        echo "》》》主页模板已同步！"


        rsync -auz ${tmpPath}/project/hivuiBirt ${projectPath}/${username}/${projectName}/deploy/${projectName}/
        cd ${projectPath}/${username}/${projectName}/deploy/${projectName}/hivuiBirt
        sed -i 's/<\/head>/'"${source}"'<\/head>/g' index.html
        echo "》》》报表模板同步！"

        cd "${tmpPath}/"        
        npm i 
        # npm rebuild
        rsync -auz ${tmpPath}/node_modules ${projectPath}/${username}/${projectName}/deploy/
        echo "》》》node环境已同步！"

        cd "${tmpPath}/"
        npm i hi-ui -S
        rm -rf ${projectPath}/${username}/${projectName}/deploy/node_modules/hi-ui
        rsync -auz ${tmpPath}/node_modules/hi-ui ${projectPath}/${username}/${projectName}/deploy/node_modules/
        echo "》》》hi-ui库已同步！"

        rsync -auz ${tmpPath}/build ${projectPath}/${username}/${projectName}/deploy/
        rsync -auz ${tmpPath}/babel.config.js ${projectPath}/${username}/${projectName}/deploy/
        rsync -auz ${tmpPath}/.postcssrc.js ${projectPath}/${username}/${projectName}/deploy/
        rsync -auz ${tmpPath}/.browserslistrc ${projectPath}/${username}/${projectName}/deploy/
        rsync -auz ${tmpPath}/vite-plugin ${projectPath}/${username}/${projectName}/deploy/

        rsync -auz ${tmpPath}/.env.development ${projectPath}/${username}/${projectName}/deploy/.env.development
        rsync -auz ${tmpPath}/.env.production ${projectPath}/${username}/${projectName}/deploy/.env.production
        rsync -auz ${tmpPath}/.env.staging ${projectPath}/${username}/${projectName}/deploy/.env.staging

        rsync -auz ${tmpPath}/vite.config.js ${projectPath}/${username}/${projectName}/deploy/vite.config.js
        echo "》》》已同步程序环境(build、babel.config.js、.postcssrc.js、.browserslistrc、vite-plugin、.env.development、.env.production、.env.staging、vite.config.js)..."

        # source ${cmdDir}/var_env.sh
        ### 设置环境变量
        cd ${projectPath}/${username}/${projectName}/deploy/
        sed -i 's#\(VITE_APP_USERPATH \?= \?\).*#\1'"/e5/render/${username}"'#g' .env.production    
        sed -i 's#\(VITE_APP_BASE_API \?= \?\).*#\1'"${DEV_BASE_API}"'#g' ${projectPath}/${username}/${projectName}/deploy/.env.development
        sed -i 's#\(VITE_APP_PN \?= \?\).*#\1'"${projectName}/${username}"'#g' ${projectPath}/${username}/${projectName}/deploy/.env.development
        sed -i 's#\(VITE_APP_PN \?= \?\).*#\1'"${projectName}/${username}"'#g' ${projectPath}/${username}/${projectName}/deploy/.env.staging

        ### node_modules/vite 环境
        cd ${projectPath}/${username}/${projectName}/deploy/node_modules/vite/dist/client 
        # sed 's/要替换的内容/新的内容/'
        # 将所有的数字替换成内容
        # sed 's/[0-9]/好的/g' testFile2.txt  
        sed -i "s#const.*socketHost.*=.*#${viteStr}#g" client.js
        sed -i "s#case 'update':.*#case 'update':return;#g" client.js
        sed -i "s#case.*'full-reload':.*#case 'full-reload':return;#g" client.js

        echo "》》》程序环境已同步！"$(date +%F%n%T)

        # 删除临时文件标识，以准备下次更新判断
        rm -rf ${eap5Dir}/hiruntime/project/desktop/__gittmp
    ;;
    esac

    rsync -auz ${tmpPath}/package.json ${projectPath}/${username}/${projectName}/deploy/package.json
    cd ${projectPath}/${username}/${projectName}/deploy/
    ## 替换 package.json 的 name 值
    sed -i 's#\("name": \?"\).*#\1'"${projectName}"'",#g' package.json
    sed -i 's#\("author": \?"\).*#\1'"${username}"'",#g' package.json
    
    #获取processes.json 的端口
    portStr=$(cat ${projectPath}/${username}/${projectName}/deploy/processes.json | sed 's/,/\n/g' | grep "args" | sed 's/:/\n/g' | sed '1d' | sed 's/"//g')
    portNum=`echo $portStr | tr -cd "[0-9]"`
    # echo $portNum
    sed -i 's#\("dev": \?"\).*#\1'"vite --host --port ${portNum}"'",#g' package.json

    echo "》》》正在重启服务..."$(date +%F%n%T)
    pm2 stop "vite-${username}-${projectName}"
    pm2 delete "vite-${username}-${projectName}"
    npm run rm:vite
    npm run pm2:start
    pm2 save
    echo "》》》已经重启服务！！"$(date +%F%n%T)
;;
esac

