name: Publish

on:
  push:
    tags:
      - 'v*'

permissions:
  contents: read
  id-token: write

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: latest

      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          registry-url: 'https://registry.npmjs.org'

      - name: Install dependencies
        run: bun install --frozen-lockfile

      - name: Run tests
        run: bun test --recursive

      - name: Publish all packages
        run: |
          for pkg in packages/shade-core packages/shade-crypto-web packages/shade-storage-sqlite packages/shade-storage-postgres packages/shade-server packages/shade-transport packages/shade-proto; do
            cd "$pkg"
            npm publish --provenance --access public
            cd ../..
          done
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
