name: CI
on:
  push:
    branches:
      - master

jobs:
  build-deploy:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      id-token: write
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 18
          registry-url: 'https://registry.npmjs.org'
      
      - run: npm install
      - run: npm run test
      - name: Create idoc config.
        run: |
          cat > idoc.yml << EOF
          site: "stylus-px2rem {{version}}"
          menus:
            Home: index.html
            Sponsor: https://jaywcjlove.github.io/#/sponsor
          EOF

      - run: npm install idoc@1.27.2 -g
      - run: idoc
      - name: Generate Contributors Images
        uses: jaywcjlove/github-action-contributors@main
        with:
          filter-author: (renovate\[bot\]|renovate-bot|dependabot\[bot\])
          output: dist/CONTRIBUTORS.svg
          avatarSize: 42

      - name: Create Tag
        id: create_tag
        uses: jaywcjlove/create-tag-action@main
        with:
          package-path: ./package.json

      - name: get tag version
        id: tag_version
        uses: jaywcjlove/changelog-generator@main

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          commit_message: ${{steps.tag_version.outputs.tag}} ${{ github.event.head_commit.message }}
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./dist

      - name: Generate Changelog
        id: changelog
        uses: jaywcjlove/changelog-generator@main
        with:
          head-ref: ${{steps.create_tag.outputs.version}}
          filter-author: (小弟调调™|Renovate Bot)
          filter: '[R|r]elease[d]\s+[v|V]\d(\.\d+){0,2}'
    
      - name: Create Release
        uses: ncipollo/release-action@v1
        if: steps.create_tag.outputs.successful
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          name: ${{ steps.create_tag.outputs.version }}
          tag: ${{ steps.create_tag.outputs.version }}
          body: |
            [![Buy me a coffee](https://img.shields.io/badge/Buy%20me%20a%20coffee-048754?logo=buymeacoffee)](https://jaywcjlove.github.io/#/sponsor) [![](https://img.shields.io/badge/Open%20in-unpkg-blue)](https://uiwjs.github.io/npm-unpkg/#/pkg/stylus-px2rem@${{steps.create_tag.outputs.versionNumber}}/file/README.md) 

            Documentation ${{ steps.changelog.outputs.tag }}: https://raw.githack.com/jaywcjlove/stylus-px2rem/${{ steps.changelog.outputs.gh-pages-short-hash }}/index.html  
            Comparing Changes: ${{ steps.changelog.outputs.compareurl }}  

            ${{ steps.changelog.outputs.changelog }}
  
      - run: npm publish --access public --provenance
        name: 📦  stylus-px2rem publish to NPM
        continue-on-error: true
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
