name: Publish Package to npmjs
on:
  push:
    branches:
      - master
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      # Setup .npmrc file to publish to npm
      - uses: actions/setup-node@v3
        with:
          node-version: '16.x'
          registry-url: 'https://registry.npmjs.org'

      # waiting https://github.com/ds300/patch-package/issues/339
      - run: npm install; npm run test:init 2>&1 || true && npm run test
      
      # resolve version
      - run: set -x; PACKAGE=$(cat package.json | jq -r '.["name"]')  VERNPM=$(curl https://registry.npmjs.org/$PACKAGE | jq -r '.["dist-tags"].latest' | awk -F. -v OFS=. '{$NF += 1 ; print}')   VERLOC=$(cat package.json | jq -r '.["version"]')   VEROUT=$(printf "$VERNPM\n$VERLOC\n" | sort -V -r | awk 'NR==1 {print; exit}') &&  jq -r '.version = "'${VEROUT}'"' package.json > /tmp/package.json && mv /tmp/package.json ./package.json
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
