name: Build

on: [ push, pull_request, workflow_dispatch ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: "Install Chrome Drivers"
        run: sudo apt-get install xvfb

      - name: "Node Cache"
        uses: actions/cache@v2
        with:
          path: ~/.npm
          key: ${{ runner.os }}-cache-node-${{ hashFiles('**/package.json', '**/package-lock.json') }}

      - name: "Node Modules Cache"
        uses: actions/cache@v2
        with:
          path: node_modules
          key: ${{ runner.os }}-cache-node_modules-${{ hashFiles('**/package.json', '**/package-lock.json') }}

      - name: "Setup Node"
        uses: actions/setup-node@v2
        with:
          node-version: 16

      - name: "Install Node"
        run: npm install

      - name: "Node Build"
        run:  npm run build

      - name: "Node Tests"
        run: xvfb-run --auto-servernum npm run test:ci

      - name: "Coveralls Coverage Report"
        uses: coverallsapp/github-action@master
        with:
          github-token: ${{ secrets.github_token }}
          path-to-lcov: ${{ github.workspace }}/static/tamu-library-components/reports/coverage/tl-elements/lcov.info

      - name: "Build Static Production"
        run: xvfb-run --auto-servernum npm run build:static-production
        env:
          LHCI_GITHUB_APP_TOKEN: ${{ secrets.LHCI_GITHUB_APP_TOKEN }}

      - name: "Deploy GH-Pages"
        if: github.ref == 'refs/heads/master' && github.event_name == 'push'
        uses: JamesIves/github-pages-deploy-action@3.7.1
        with:
          github_token: ${{ secrets.github_token }}
          branch: gh-pages
          folder: static/tamu-library-components
          clean: true
          single_commit: true
