name: Auto Publish to NPM

on:
  push:
    branches: 
      - main

jobs:
  publish:
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    permissions:
      contents: write
      packages: write
    
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}
          
      - name: Git Configuration
        run: |
          git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --global user.name "GitHub Actions[bot]"
          
      - uses: actions/setup-node@v3
        with:
          node-version: '20'
          registry-url: https://registry.npmjs.org/
          
      - name: Install dependencies
        run: npm ci || npm install
          
      - name: Get commit message
        id: commit
        run: |
          # 获取最后一次提交信息并处理多行信息
          FULL_MSG=$(git log -1 --pretty=format:"%B")
          echo "message<<EOF" >> $GITHUB_OUTPUT
          echo "$FULL_MSG" >> $GITHUB_OUTPUT
          echo "EOF" >> $GITHUB_OUTPUT
          
      - name: Determine version bump
        id: version
        if: github.ref == 'refs/heads/main'
        run: |
          MESSAGE="${{ steps.commit.outputs.message }}"
          # 检查提交信息中的关键字
          if [[ $MESSAGE =~ "BREAKING CHANGE:" ]] || [[ $MESSAGE =~ "!" ]]; then
            echo "type=major" >> $GITHUB_OUTPUT
            echo "Found major change trigger in commit message"
          elif [[ $MESSAGE =~ ^feat:.*$ ]] || [[ $MESSAGE =~ ^feature:.*$ ]]; then
            echo "type=minor" >> $GITHUB_OUTPUT
            echo "Found minor change trigger in commit message"
          else
            echo "type=patch" >> $GITHUB_OUTPUT
            echo "Default to patch version"
          fi
          
      - name: Automated Version Bump
        uses: 'phips28/gh-action-bump-version@master'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          version-type: ${{ steps.version.outputs.type }}
          commit-message: 'CI: bumps version to {{version}} [skip ci]'
          
      - name: Publish to NPM
        run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
