name: Build and Publish NPM package on release

on:
  release:
    types: [ published ]  # Triggers when you publish a release through GitHub UI
  workflow_dispatch:     # Enables manual trigger via GitHub web

permissions:
  contents: write  # To push updated files back to main
  packages: write  # For GitHub packages (optional)

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          ref: main  # Always checkout main branch, not the tag
          token: ${{ secrets.GITHUB_TOKEN }}
          fetch-depth: 0  # Fetch all history

      - name: Publish NPM Package
        uses: phucbm/publish-npm-action@v1 # Docs https://github.com/phucbm/publish-npm-action
        with:
          npm-token: ${{ secrets.NPM_TOKEN }}
          # github-token: ${{ secrets.CUSTOM_TOKEN }}        # Custom GitHub token, default is ${{ github.token }}
          # node-version: '20'                               # Node.js version, default is '18'
          # pnpm-version: '9'                                # PNPM version, default is '8'
          # build-command: 'npm run build:prod'              # Build command, default is 'pnpm build' (use '' to skip build)
          # install-command: 'npm ci'                        # Install command, default is 'pnpm install --no-frozen-lockfile'
          # test-command: 'npm run test:ci'                  # Test command, default is 'pnpm test'
          # publish-access: 'restricted'                     # NPM access level, default is 'public'
          # skip-tests: 'true'                               # Skip tests, default is 'false'
          # output-dir: 'build/'                             # Output directory, default is 'dist/'
          # target-branch: 'develop'                         # Target branch, default is 'main'
          # commit-files: 'CHANGELOG.md docs/ types/'        # Additional files to commit, default is ''