# 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: CI

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

permissions:
  contents: read # to fetch code (actions/checkout)

jobs:
  build:

    runs-on: ubuntu-latest

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

    steps:
    - uses: actions/checkout@v2
    - name: Fetch the master branch
      run: git fetch origin HEAD:refs/remotes/origin/HEAD --depth 1
    - name: Fetch the master branch
      run: git diff --name-only origin/HEAD --no-renames --diff-filter=ACMR
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
    - run: npm i
    - run: npm run cover
    # run linter
    - run: |
        set -x;
        git status;
        git checkout HEAD -- package.json;
        changes=$(git diff --name-only origin/HEAD --no-renames --diff-filter=ACMR);
        if [ "$changes" == "" ]; then
            echo "checking all files";
            node node_modules/eslint/bin/eslint "lib/ace/**/*.js";
        else
            jsChanges=$(echo "$changes" | grep -P '.js$' || :);
            if [ "$jsChanges" == "" ]; then
                echo "nothing to check";
            else
                echo "checking $jsChanges";
                node node_modules/eslint/bin/eslint $jsChanges;
            fi
        fi
    # check types
    - run: |
        set -x;
        npx tsc -v;
        npm run update-types;
        git diff --color --exit-code ./ace*d.ts;
        npm run typecheck;
        node_modules/.bin/tsc --noImplicitAny --strict --noUnusedLocals --noImplicitReturns --noUnusedParameters --noImplicitThis ace.d.ts;
    - run: |
        set -x;
        ./tool/test-npm-package.sh
    # upload to codecov
    - uses: codecov/codecov-action@v3
      with:
        token: d8edca4b-8e97-41e5-b54e-34c7cf3b2d47
        file: ./coverage/coverage.json
        flags: unittests
        name: codecov-umbrella
        fail_ci_if_error: true
