name: "publish"

on:
  workflow_dispatch:
    inputs:
      release_type:
        description: 'release type: minor | patch | beta'
        required: true

jobs:
  publish-new-version:
    name: "publish a new version"
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          token: ${{ secrets.PAT }}

      - name: npm install
        run: npm i -g lerna && npm run bootstrap

      - name: get version list
        run: |
          PKG_NAME=@douyinfe/semi-ui
          echo "VERSION_LIST="$(npm view $PKG_NAME versions --json)"" >> $GITHUB_ENV
        

      - name: get version
        run: echo "RELEASE_VERSION="$(node scripts/version.js)"" >> $GITHUB_ENV
        env:
            RELEASE_TYPE: ${{ github.event.inputs.release_type }}

      - name: publish
        run: |
          git config --global user.name 'semi-bot'
          git config --global user.email 'semi-bot@users.noreply.github.com'
          npm config set registry=https://registry.npmjs.org/
          npm config set //registry.npmjs.org/:_authToken=${{ secrets.NPM_TOKEN }}
          npm whoami
          DIST_TAG=latest
          if [[ ${{ github.event.inputs.release_type }} == 'beta' ]]; then
            DIST_TAG=beta
          fi
          echo "$RELEASE_VERSION"
          echo "$DIST_TAG"
          lerna version $RELEASE_VERSION --exact --force-publish --yes --no-push
          lerna publish from-package --dist-tag $DIST_TAG --yes
          git push -o ci.skip --follow-tags --no-verify --atomic