name: Publish
on:
  workflow_run:
    workflows: ["Tests"]
    types:
      - completed
jobs:
  publish:
    runs-on: ubuntu-latest
    # not on PRs
    if: github.event_name != 'pull_request'
    env:
        NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        GITHUB_CI: true
    steps:
      - uses: actions/checkout@v4
      # Setup .npmrc file to publish to npm
      - uses: actions/setup-node@v4
        with:
          node-version: 18.12.0
          registry-url: 'https://registry.npmjs.org'
      - name: Set git user name/email
        run: |
            git config --global user.email "actions@github.com"
            git config --global user.name "Github Actions Bot"
      - name: Install dependencies
        run: npm install
      - name: Build
        run: npm run build
      - name: Increment version
        run: npm run increment
      - name: Publish @next
        run: npm publish --tag next
        if: ${{ github.ref == 'refs/heads/dev' }}
      - name: Publish @latest
        run: npm publish
        if: ${{ github.ref == 'refs/heads/main' }}
      - name: Commit changes
        run: git push && git push --tags