# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
# "https://${{secrets.GIT_USER}}:${{secrets.GIT_TOKEN}}@github.com/${{secrets.GIT_REPO}}"

name: mf-react-toolkit publish

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

  workflow_dispatch:

jobs:

  patch-version:   
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: 14
      - name: Setup GIT
        run: |
          git config --global user.email "${{secrets.GIT_ID}}"
          git config --global user.name "${{secrets.GIT_USER}}"
      - name: Patch & Push
        env:
          GITHUB_TOKEN: ${{secrets.GIT_TOKEN}}
        run: |
          npm version patch
          printf "\n\n$(date)" >> README.md
          git add .
          git commit -m "Upgrade to %s for publishing"
          git push

  yarn-build:
    needs: patch-version
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: 14
      - run: npm i -g yarn 
      - run: yarn install
      - run: yarn test
      - run: yarn run build    

  npm-publish:
    needs: yarn-build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: 14
          registry-url: https://registry.npmjs.org/
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{secrets.npm_token}}
