name: Build dist

on:
  push:
    branches: [main]

concurrency:
  group: build-dist
  cancel-in-progress: false

permissions:
  contents: write

jobs:
  build:
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, '[auto]')"

    steps:
      - uses: actions/checkout@v6
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

      - uses: actions/setup-node@v6
        with:
          node-version: '20'
          cache: 'npm'

      - run: npm ci
      - run: npm test
      - run: npm run build

      - name: Verify build output
        run: test -f dist/index.js || exit 1

      - name: Commit dist/
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add dist/ --force
          git diff --staged --quiet || git commit -m "build: compile dist/ [auto]"
          git push
