name: NPM & WebP

# 在 Release 发布时 或手动执行

on:
  push:
     paths:
       - 'rawimg/**'

  workflow_dispatch:

jobs:
  publish-npm:
    runs-on: ubuntu-latest
    # Clone 仓库
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          ref: main
      # 安装 Node.js    
      - name: Setup Node
        uses: actions/setup-node@v1
        with:
          node-version: "16.x"
          registry-url: https://registry.npmjs.org/
      # 安装python
      - name: Set up Python 3.7
        uses: actions/setup-python@v1
        with:
          python-version: 3.7 
          
        
      # rawimg/ 作为原始图片存储，webpimg/ 作为压缩图片存储处，最后合并。
      # 转换图片后缀
      - name: IMG2img
        run: |
          sudo apt-get install rename -y
          rename 's/.PNG/.png/' rawimg/*
          rename 's/.JPG/.jpg/' rawimg/*
          rename 's/.JPEG/.jpeg/' rawimg/*
          rename 's/.GIF/.gif/' rawimg/*
      
      # 安装相关插件，转换图片。
      - name: Install & Convert
        run: |
          npm install -g webp-batch-convert
          cwebp-batch --in rawimg --out webpimg -q 75 
          mv webpimg/*.webp rawimg/
          
      # 发布 NPM 包
      - name: Publish Package
        run: |
          git config --global user.email "qyu0615@gmail.com"
          git config --global user.name "cherbim"
          npm version patch
          npm publish
        env:
          NODE_AUTH_TOKEN: ${{secrets.npm_token}}
     
          
      
          
      # pip安装requests
      - name: Pip install requests
        run: |
          pip3 install requests
          
       # 推送到telegram
      - name: Post to telegram
        env:
          telegram_token: ${{secrets.telegram_token}}
          user_id: ${{secrets.user_id}}
          cdn: ${{secrets.cdn}}
        run: |
          python3 post2tg.py
          
          
      # 删除 rawimg/ 和 webpimg/ 下的所有文件
      - name: Delete Files
        run: |
          rm -rf webpimg/*
          rm -rf rawimg/*
          touch webpimg/.gitkeep
          touch rawimg/.gitkeep
          
      - name: Push
        run: |
          git add -A
          git commit -m "Publish"
          git push origin main
