name: 'Publish to NPM'

on:
  workflow_dispatch:
  workflow_run:
    workflows: ['Build & Test']
    branches: ['main']
    types:
      - completed

jobs:
  publish:
    name: 'Publish to NPM'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup NodeJS
        uses: actions/setup-node@v2
        with:
          node-version: '>=22'
          registry-url: 'https://registry.npmjs.org'

      - name: Install NPM dependencies
        run: npm install

      - name: Build package
        run: npm run build

      - name: Validate package
        run: npx --yes @arethetypeswrong/cli --pack .

      - name: Publish package to NPM
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
