name: Publish package

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  publish-npm:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          ref: master
        
      - name: Npm install
        uses: actions/setup-node@v1
        with:
          node-version: 12
      - run: npm install
      
      - name: Check if version has been updated
        uses: EndBug/version-check@v1
        id: check

      - name: Log when changed
        if: steps.check.outputs.changed == 'true'
        run: 'echo "Version change found in commit ${{ steps.check.outputs.commit }}! New version: ${{ steps.check.outputs.version }} (${{ steps.check.outputs.type }})"'

      - name: Log when unchanged
        if: steps.check.outputs.changed != 'true'
        run: 'echo "Version unchanged"'

      - name: Set up Node.js for NPM
        uses: actions/setup-node@v1
        with:
          registry-url: "https://registry.npmjs.org"

      - name: Publish package to NPM
        if: steps.check.outputs.changed == 'true'
        run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.npm_token }}
