name: Publish to NPM

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  publish-to-npm:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout release branch code
        uses: actions/checkout@v3
        with:
          fetch-depth: 1

      - name: Get yarn cache
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - name: Cache dependencies
        uses: actions/cache@v2
        id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: Use Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 16
          registry-url: https://registry.npmjs.org

      - name:  'Automated Version Bump'
        uses:  'phips28/gh-action-bump-version@master'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          default: prerelease
          rc-wording:     'RELEASE,alpha,PicX'

      - name: Publish to NPM
        run: npm publish || true
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
