天天看點

linux shell 自動化部署 npm vue 項目

此 shell 是提供給前端登入伺服器自動化部署 vue 項目的

#!/bin/sh
# url:https://github.com/jefferyjob/tool

#====================================================================
#=== 項目路徑定義 ===================================================
#=== 請在此定義您的項目路徑 =========================================
#====================================================================
dir_array=(
"/data/Web/make_client/web_make_client"
"/data/Web/make_client/web_make_client_back"
"/data/Web/WebUPinVue"
)

#===================================================================
#=== 是否開啟确認 ==================================================
#=== true: 開啟、false: 關閉  ======================================
#===================================================================
verify=true


#====================================================================
#=== 指令執行參數 ===================================================
#====================================================================
clear
#echo "+------------------------------------------------------+:
#echo "|          Npm project automation deployment           |"
#echo "+------------------------------------------------------+"

echo -e "\033[33m請選擇要執行的vue項目\033[0m"
for i in ${!dir_array[@]}
do
	echo -e "\033[33m$i: ${dir_array[$i]} \033[0m"
done

# 提示輸入
read -p "請輸入項目序号: " num

if [ ! -n "$num" ];then
	echo -e "\033[31mError: 未選擇項目序号\033[0m"
  	exit
elif echo $num | grep -q '[^0-9]';then
	echo -e "\033[31mError: 請輸入數字格式\033[0m"
	exit
elif [[ $num -lt 0 ]]||[[ $num -gt ${#dir_array[@]} ]];then
	echo -e "\033[31mError: 錯誤的項目序号\033[0m"
	exit
elif [ ! -x "${dir_array[$num]}" ]; then
        echo -e "\033[31mError: 該項目目錄不存在或沒有可執行權限\033[0m"
        exit
fi

# 項目路徑輸出
echo -e "\033[36mvue path: ${dir_array[$num]}\033[0m"

echo -e '\n'

# npm 指令定義
command_array=("cnpm install" "cnpm run build" "cnpm install && cnpm run build" "npm install" "npm run build" "npm install && npm run build")

echo -e "\033[33m請選擇要執行的npm指令\033[0m"
for i in ${!command_array[@]}
do
        echo -e "\033[33m$i: ${command_array[$i]} \033[0m"
done


# 提示輸入
read -p "請輸入指令序号: " number

if [ ! -n "$number" ];then
        echo -e "\033[31mError: 未選擇指令序号\033[0m"
        exit
elif echo $number | grep -q '[^0-9]';then
        echo -e "\033[31mError: 請輸入數字格式\033[0m"
        exit
elif [[ $number -lt 0 ]]||[[ $number -gt ${#command_array[@]} ]];then
        echo -e "\033[31mError: 錯誤的指令序号\033[0m"
        exit
fi

# 項目路徑輸出
echo -e "\033[36mvue command: ${command_array[$number]}\033[0m"

echo -e '\n'

# 輸出執行指令
echo -e "\033[44;37mcommand: ${dir_array[$num]} && ${command_array[$number]}\033[0m"

# 提示輸入
if  [[ $verify == true ]] ; then
	
	read -p "您是否要執行此指令(y/n): " command
	if [ "$command" != "y" ]&&[ "$command" != "n" ];then
	        echo -e "\033[31mError: 輸入錯誤\033[0m"
	        exit
	elif [[ $command == 'n' ]];then
	        echo -e "\033[31m程式已終止... ...end\033[0m"
	        exit
	fi

fi

# 指令執行
# 指令執行
command=${command_array[$number]}
OLD_IFS="$IFS"
IFS="&&"
command_arr=($command)
IFS="$OLD_IFS"
for i in ${!command_arr[@]}
do
        cd ${dir_array[$num]} &&  ${command_arr[$i]}
done
           

繼續閱讀