name: Generate documentation

on:
  workflow_dispatch:

jobs:
  generate:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version: '18.x'

      - name: Generate documentation
        run: |
          npm ci
          npm run typedoc

      - name: Commit files
        if: "github.repository == 'poolifier/poolifier' && github.ref == 'refs/heads/${{ github.event.repository.default_branch }}'"
        run: |
          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git commit -m "Generate documentation" -a

      - name: Push changes
        if: "github.repository == 'poolifier/poolifier' && github.ref == 'refs/heads/${{ github.event.repository.default_branch }}'"
        uses: CasperWA/push-protected@v2
        with:
          token: ${{ secrets.GH_TOKEN_PROTECTED }}
          branch: ${{ github.event.repository.default_branch }}
