# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI - Test

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

jobs:
    build:
        runs-on: ubuntu-latest

        strategy:
            matrix:
                node-version: [18.12.1]

        steps:
            - uses: actions/checkout@v3
            - name: Use Node.js ${{ matrix.node-version }}
              uses: actions/setup-node@v3
              with:
                  node-version: ${{ matrix.node-version }}
            - run: npm i --force
            - run: node main.js --createConfig -y
            - run: npm run compile
            - run: npx ts-node run.ts setProject --project 'example'
            - run: npm run build
            - run: npm test

            - name: Cleanup
              run: |
                  gh extension install actions/gh-actions-cache

                  REPO=${{ github.repository }}
                  BRANCH="refs/pull/${{ github.event.pull_request.number }}/merge"

                  echo "Fetching list of cache key"
                  cacheKeysForPR=$(gh actions-cache list -R $REPO -B $BRANCH -L 100 | cut -f 1 )

                  ## Setting this to not fail the workflow while deleting cache keys. 
                  set +e
                  echo "Deleting caches..."
                  for cacheKey in $cacheKeysForPR
                  do
                    gh actions-cache delete $cacheKey -R $REPO -B $BRANCH --confirm
                  done
                  echo "Done"
        env:
            GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
