name: Test and deploy to GitHub Pages

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Setup Node
        uses: actions/setup-node@v3.5.0
        with:
          node-version-file: ".nvmrc"
          cache: npm

      - name: Setup NPM
        run: |
          NPM_VERSION=$(jq -r .engines.npm package.json)
          NPM_VERSION=${NPM_VERSION/\^/}
          if [ "$(npm --version)" != "$NPM_VERSION" ]; then
            npm install -g npm@$NPM_VERSION && npm --version
          else
            echo "NPM version is same as package.json engines.npm"
          fi

      - name: Install dependencies
        run: npm ci

      - name: Test before production
        run: npm test

      - name: Build tool
        run: npm run build
        env:
          NODE_ENV: production

      - name: Deploy master to GitHub Pages
        uses: JamesIves/github-pages-deploy-action@2.0.0
        env:
          ACCESS_TOKEN: ${{ secrets.DEV_GITHUB_TOKEN }}
          BASE_BRANCH: master
          BRANCH: gh-pages
          FOLDER: dist
