name: publish

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

jobs:
  install: 
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [14.x]

    steps:
    - uses: actions/checkout@v2

    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}

    - name: Cache node_modules
      uses: actions/cache@v2
      id: cache-node_modules
      with: 
        path: node_modules
        key: node_modules-${{ hashFiles('**/package-lock.json') }}

    - name: Install
      if: steps.cache-node_modules.outputs.cache-hit != 'true'
      run: npm ci

  publish:
    runs-on: ubuntu-latest
    needs: install

    strategy:
      matrix:
        node-version: [14.x]

    steps:
    - uses: actions/checkout@v2

    - uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
        registry-url: https://registry.npmjs.org/

    - name: Restore cache from node_modules
      uses: actions/cache@v2
      with: 
        path: node_modules
        key: node_modules-${{ hashFiles('**/package-lock.json') }}

    - name: Cache package version
      uses: actions/cache@v2
      id: cache-package-version
      with: 
        path: package.json
        key: package-version-${{ hashFiles('**/package.json') }}

    - name: Publish
      if: steps.cache-package-version.outputs.cache-hit != 'true'
      run: npm publish
      env:
        NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
