name: Publish

on:
  # allow to manually run this workflow
  workflow_dispatch:
  # run on published release. this will not trigger, when release draft is created
  release:
    types: 
      - published

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          node-version: 15
      - name: Cache Node.js modules
        uses: actions/cache@v2
        with:
          # npm cache files are stored in `~/.npm` on Linux/macOS
          path: ~/.npm
          key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.OS }}-node-
            ${{ runner.OS }}-
        # use fresh install
      - name: Install dependencies
        run: npm ci

        # set auth token here instead of inside .npmrc file
      - name: Set config variables
        run: npm config set //registry.npmjs.org/:_authToken ${NPM_AUTH_TOKEN}
        env:
          NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} # Set NPM auth token from GitHub Secrets

        # publish package to npm registry using token
      - name: Publish to NPM
        run: npm publish
