前端shell脚本执行打包构建功能

欢迎点击领取 -《前端面试题进阶指南》:前端登顶之巅-最全面的前端知识点梳理总结

环境Mac iterm2命令窗口;因项目云效流水线构建node太老,不能执行单个项目的版本升级;前端框架使用vue3.2 + vite4.0.1需要node16以上支持;需要前端本地build上传;

执行shell脚本 sh publish.sh(测试环境) || sh publish.sh online(线上环境)

  1. 执行lint格式化代码操作
  2. build对应环境代码
  3. 填写commit相关信息
  4. 执行git上传指令
  5. 以上操作有误,可输入n结束命令,纠正后再次执行
#!/usr/bin/env bash

TIME=`date +"%Y-%m-%d/%H:%M:%S"`

COMMIT_MSG=''

buildStr=''

log() {
  echo "build afo log||$1"
}

exitIfNeed() {
 if [ $? -ne 0 ]; then
  log "status=failed||error=${1}" && exit 1
 fi
}

case "$1" in
 online)
  buildStr='npm run build:prod'
  ;;
 *)
  buildStr='npm run build:test'
esac

if [ -d "./dist" ];then
  echo "\033[31m ====dist文件夹已存在=======继续执行====\033[0m"
  echo -e "\033[44;37mcommand: 开始执行lint格式化... ...操作\033[0m"
  npm run lint
  exitIfNeed 'npm run lint failed!'
  read -p "您是否要重新执行打包命令(y/n): " command
  if [ "$command" == "y" ];then
  rm -rf dist
  echo -e "\033[44;37mcommand =====开始build========start=====\033[0m"
  $buildStr
  exitIfNeed 'npm run build failed!'
  fi
else
  read -p "您是否要执行打包命令(y/n): " command
  if [ "$command" == "y" ];then
  echo -e "\033[44;37mcommand: 开始执行lint格式化... ...操作\033[0m"
  npm run lint
  exitIfNeed 'npm run lint failed!'
  echo -e "\033[44;37mcommand =====开始build========start=====\033[0m"
  $buildStr
  exitIfNeed 'npm run build failed!'
  fi
fi

read -p "您是否要执行上传npm包命令(y/n): " command
if [ "$command" == "y" ];then
    read -p "请输入上传的commit信息内容: " COMMIT_MSG
    commit_msg=dev_${TIME}:${COMMIT_MSG}
    echo -e "\033[44;37mcommand: 开始执行publish... ...上传\033[0m"
    git add .
    git commit -m $commit_msg
    git pull 
    exitIfNeed 'git pull failed!'
    git push origin master
    exitIfNeed 'git push failed!'
    exit
elif [ $command == 'n' ];then
  echo -e "\033[31m程序已终止... ...end...\033[0m"
  exit
fi

exitIfNeed '====Build uboot failed!===='
exit 0

你可能感兴趣的:(前端,git,github)