# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

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

jobs:
  # building job
  build:
    runs-on: ubuntu-latest
    name: Build

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

    steps:
      - uses: actions/checkout@v2
      - name: Building on Node.js ${{ matrix.node-version }} 🛠️
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm ci --ignore-scripts
      - run: npm run build
      - run: ./scripts/ci-build-check.sh
        env:
          CI: true

  # testing job
  test:
    runs-on: ubuntu-latest
    name: Test

    strategy: 
      matrix:
        node-version: [14.x]
    steps:
      - uses: actions/checkout@v2
      - name: Testing on node ${{ matrix.node-version }} 🧪
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm ci --ignore-scripts
      - run: npm test

  # coverage uploading job
  coverage:
    runs-on: ubuntu-latest
    name: Coverage
    needs: Test

    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v1
        with:
          # latest nodejs LTS
          node-version: 14.x 
      - run: npm ci --ignore-scripts
      - run: npm test
      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v1
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          directory: ./coverage

  # release to npm job
  release:
    runs-on: ubuntu-latest
    name: Release
    needs: Test

    # release only when pushed to master
    if: github.event_name == 'push'

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      - uses: actions/checkout@v2
        with:
          token: ${{ secrets.GIT_ACCESS_TOKEN }}
      - uses: actions/setup-node@v1
        with:
          # latest nodejs LTS
          node-version: 14.x 
      - run: npm ci --ignore-scripts
      - name: Preparing a release for npm and github 🚀
        # more about this action here: https://github.com/cycjimmy/semantic-release-action
        uses: cycjimmy/semantic-release-action@v2
        with:
          extra_plugins: |
            @semantic-release/changelog
            @semantic-release/git
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
