name: Publish to NPM
on:
  push:
    branches:
      - main
    paths-ignore:
      - 'docs/**'
jobs:
  build:
    runs-on: ubuntu-latest
    # use 'if' to prevent endless looping with npm package commit
    if: "!startsWith(github.event.head_commit.message, '[RELEASE]')"
    steps:
      - uses: actions/checkout@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
      - run: git config user.email "$GITHUB_ACTOR@users.noreply.github.com"
      - run: git config user.name "$GITHUB_ACTOR"
      # prepend '[RELEASE]' so that it can be detected above to prevent looping
      #  this is a commit to main that can be ignored by this GitHub action
      #  use 'patch' to update only the last digit in the version (e.g. 1.1.?)
      # - run: npm version patch -m "[RELEASE] %s"
      # - run: git push
      # Setup .npmrc file to publish to npm
      - uses: actions/setup-node@v4
        with:
          node-version: '20.x'
          registry-url: 'https://registry.npmjs.org'
      - run: npm ci
      - run: npm publish --access=public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
