# 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://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages

name: Node.js Package

on:
  release:
    types: [created]

env:
  NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

jobs:
  test:
    runs-on: ubuntu-20.04
    steps:      
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Setup Node.js Env
        run: echo "NODE_VER=`cat .nvmrc`" >> $GITHUB_ENV

      - name: Setup Node.js
        uses: dcodeIO/setup-node-nvm@v4
        with:
          node-version: "${{ env.NODE_VER }}"

      - name: Install dependencies
        run: npm ci

      - name: Lint
        run: npm run lint

      - name: Test
        run: npm run test

  publish-npm:
    needs: test
    runs-on: ubuntu-20.04
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Setup Node.js Env
        run: echo "NODE_VER=`cat .nvmrc`" >> $GITHUB_ENV

      - name: Setup Node.js
        uses: dcodeIO/setup-node-nvm@v4
        with:
          node-version: "${{ env.NODE_VER }}"

      - name: Install dependencies
        run: npm ci

      - name: Lint
        run: npm run lint

      - name: Test
        run: npm run test
        
      - run: git config --global user.name "${{ github.actor }}"
      - run: git config --global user.email "github-action-${{ github.actor }}@users.noreply.github.com"

      - name: Tag NPM version
        run: npm version --allow-same-version ${{ github.event.release.tag_name }}

      - name: Publishing to npmjs.
        run: npm publish --userconfig .npmrc --registry https://registry.npmjs.org/
