version: 2 # use CircleCI 2.0

jobs: # a collection of steps
    test: # runs not using Workflows must have a `build` job as entry point
        docker: # run the steps with Docker
            - image: circleci/node:10.15.0 # ...with this image as the primary container; this is where all `steps` will run
        environment:
            SONAR_QUBE_VERSION: 3.3.0.1492-linux
        steps: # a collection of executable commands
            - checkout # special step to check out source code to working directory
            - run:
                name: Update npm to latest
                command: 'sudo npm install -g npm@latest'
            - run:
                name: Install npm packages
                command: npm install
            - run:
                name: Run lint
                command: npm run lint
            - restore_cache:
                key: sonar-scanner-{{ .Environment.SONAR_QUBE_VERSION }}
            - run:
                name: Install sonar scanner
                command: |
                  if [ ! -d ~/sonar-scanner-${SONAR_QUBE_VERSION}/  ]; then
                      wget --quiet https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-${SONAR_QUBE_VERSION}.zip -O ~/sonar-scanner.zip
                      unzip ~/sonar-scanner.zip -d ~/
                  fi
            - run:
                name: Run Sonar Qube analysis
                command: ~/sonar-scanner-${SONAR_QUBE_VERSION}/bin/sonar-scanner -Dsonar.login=${SONAR_QUBE_LOGIN_KEY} -Dsonar.branch.name=${CIRCLE_BRANCH}
            - save_cache:
                key: sonar-scanner-{{ .Environment.SONAR_QUBE_VERSION }}
                paths:
                    - ~/sonar-scanner-${SONAR_QUBE_VERSION}/
                    - ~/.sonar/cache/
                    - ./.scannerwork/
workflows:
    version: 2
    test:
        jobs:
            - test
