name: Publish to npm
on:
  push:
    branches:
      - master
      
permissions:
  contents: write

jobs:
  versions:
    runs-on: ubuntu-latest
    outputs:
      last: ${{ steps.versions.outputs.last }}
      current: ${{ steps.versions.outputs.current }}
    steps:
      - name: Use Node.js
        uses: actions/setup-node@v3
        with:
          node-version: 16.x
      - uses: actions/checkout@v3
      - run: |
          echo "::set-output name=last::$(npm show react-native-use-qili version)"
          echo "::set-output name=current::$(node -p 'require(`./package.json`).version')" 
        id: versions
      - run: echo "${{steps.versions.outputs.last}},${{steps.versions.outputs.current}} "
  
  publish:
    needs: versions
    runs-on: ubuntu-latest
    if: ${{ needs.versions.outputs.last !=  needs.versions.outputs.current }}

    steps:
    - name: Use Node.js
      uses: actions/setup-node@v3
      with:
        node-version: 16.x
    - uses: actions/checkout@v3
      with:
        fetch-depth: 1
    - run: yarn prepublish
    - name: publish to npm
      uses: JS-DevTools/npm-publish@v2.2.1
      with:
        token: ${{ secrets.NPM_TOKEN }}

  sync-bridge:
    needs: publish
    runs-on: ubuntu-latest
    if: ${{ false }}
    steps:
    - name: Use Node.js
      uses: actions/setup-node@v3
      with:
        node-version: 16.x
    - uses: actions/checkout@v3
      with:
        repository: lalalic/qili-web-bridge
        token: ${{ secrets.GIT_PAT_KEY }}

    - name: Use Lastest use-qili
      run: |
        yarn add react-native-use-qili
        cd www
        yarn add react-native-use-qili
        
    - name: commit
      run: |
        git config --global user.email "lalalic@gmail.com"
        git config --global user.name "Li Cheng"
        git commit -a -m "auto sync use-qili"
        git push