name: Test, Build and Publish
on:
  workflow_dispatch:
  push:
    branches:
      - 'main'
jobs:
  build-test-publish:
    env:
      NPM_TOKEN: ${{secrets.NPM_TOKEN}}
      GH_TOKEN: ${{secrets.GH_TOKEN}}
      GH_USER: ${{secrets.GH_USER}}
      GH_EMAIL: ${{secrets.GH_EMAIL}}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.GH_TOKEN }}

      - uses: pnpm/action-setup@v4
        with:
          version: 7

      - name: 'Setup node with cache'
        uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: 'pnpm'

      - run: pnpm install --frozen-lockfile
      - run: pnpm test
      - run: pnpm build
      - name: setup git coordinates
        run: |
          git config user.name $GH_USER
          git config user.email $GH_EMAIL
      - name: setup npm registry
        run: |
          echo "@veramo-community:registry=https://registry.npmjs.org/" > .npmrc
          echo "registry=https://registry.npmjs.org/" >> .npmrc
          echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> .npmrc
          npm whoami
      - name: publish @latest when on main
        if: github.ref == 'refs/heads/main'
        run: pnpm release
