#!/bin/bash

echo "选择一个发布的选项（输入一个数字）"
echo

select VERSION in patch minor major "指定版本"
  do
    echo
    if [[ $REPLY =~ ^[1-4]$ ]]; then
      if [[ $REPLY == 4 ]]; then
        read -p "请输入具体版本号: " -r VERSION
        echo
        if [[ -z $REPLY ]]; then
          VERSION=$REPLY
        fi
      fi

      read -p "发布版为: $VERSION - 确定? (y/n) " -n 1 -r
      echo

      if [[ $REPLY =~ ^[Yy]$ || -z $REPLY ]]; then

        # bump version
        npm version $VERSION
        NEW_VERSION=$(node -p "require('./package.json').version")
        echo 发布中 ${NEW_VERSION} ...

        # changelog
        npm run changelog

        # git release
        git add .
        read -p "请输入 commit 类型（feat/fix）:" LOGNAME
        read -p "请输入 commit message:" MESSAGE
        git commit -m "${LOGNAME}: ${MESSAGE}"
        echo "push ......"
        read -p "请输入 push 的分支（d:dev/m:master）:" BRANCH
        git push origin $BRANCH
        #git push origin refs/tags/v${NEW_VERSION}
        echo "已发布"
      else
        echo Cancelled
      fi
      break
    else
      echo Invalid \"${REPLY}\"
      echo "想要继续，请输入(1-4)之间的一个数字."
      echo
    fi
  done
