# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2.1

executors:
  default:
    working_directory: ~/repo
    environment:
      # install Cypress in Linux-like cache folder
      CYPRESS_CACHE_FOLDER: '~/.cache/Cypress'
    docker:
      - image: circleci/node:12-browsers

commands:
  install_rsync:
    description: 'Install Rsync'
    steps:
      - run: sudo apt install rsync
  yarn_install:
    description: 'Install Dependencies'
    steps:
      - run: yarn install --frozen-lockfile --non-interactive
      - run: ./node_modules/.bin/cypress verify
      - save_cache:
          key: nrwl-nx-node12-yarn-key-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache/yarn
      - save_cache:
          key: nrwl-nx-node12-cypress-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache/Cypress
  restore_cypress_cache:
    description: 'Restore Cypress Cache'
    steps:
      - restore_cache:
          keys:
            - nrwl-nx-node12-cypress-{{ checksum "yarn.lock" }}
            # fallback to using the latest cache if no exact match is found
            - nrwl-nx-node12-cypress-
  restore_yarn_cache:
    description: 'Restore Cached Dependencies'
    steps:
      - restore_cache:
          keys:
            - nrwl-nx-node12-yarn-key-{{ checksum "yarn.lock" }}
            # fallback to using the latest cache if no exact match is found
            - nrwl-nx-node12-yarn-key-
  setup:
    description: 'Setup Executor'
    steps:
      - checkout
      - install_rsync
      - attach_workspace:
          # Must be absolute path or relative path from working_directory
          at: ..

jobs:
  install:
    executor: default
    steps:
      - checkout
      - restore_yarn_cache
      - restore_cypress_cache
      - yarn_install
      - persist_to_workspace:
          # Must be an absolute path, or relative path from working_directory. This is a directory on the container which is
          # taken to be the root directory of the workspace.
          root: ..
          # Must be relative path from root
          paths:
            - repo/node_modules
            - .cache/Cypress
  checks-and-unit-tests:
    executor: default
    steps:
      - setup
      - run:
          name: Run Unit Tests
          command: yarn test
      - run:
          name: Check Documentation
          command: yarn documentation
      - run:
          name: Check Imports
          command: yarn check-imports
      - run:
          name: Check Formatting
          command: yarn check-format
      - run:
          name: Run linting
          command: yarn lint
      - run:
          name: Check Commit Message Format
          command: yarn check-commit
      - run:
          name: Check Package dependencies
          command: yarn depcheck
  e2e-1:
    executor: default
    steps:
      - setup
      - run:
          name: Tests Part 1
          command: yarn e2e-ci 1
          no_output_timeout: 30m
  e2e-2:
    executor: default
    steps:
      - setup
      - run:
          name: Tests Part 2
          command: yarn e2e-ci 2
          no_output_timeout: 30m
  e2e-3:
    executor: default
    steps:
      - setup
      - run:
          name: Tests Part 3
          command: yarn e2e-ci 3
          no_output_timeout: 30m
  e2e-4:
    executor: default
    steps:
      - setup
      - run:
          name: Tests Part 4
          command: yarn e2e-ci 4
          no_output_timeout: 30m
  e2e-5:
    executor: default
    steps:
      - setup
      - run:
          name: Tests Part 5
          command: yarn e2e-ci 5
          no_output_timeout: 30m
  e2e-6:
    executor: default
    steps:
      - setup
      - run:
          name: Tests Part 6
          command: yarn e2e-ci 6
          no_output_timeout: 30m
  e2e-7:
    executor: default
    steps:
      - setup
      - run:
          name: Tests Part 7
          command: yarn e2e-ci 7
          no_output_timeout: 30m
  e2e-8:
    executor: default
    steps:
      - setup
      - run:
          name: Tests Part 8
          command: yarn e2e-ci 8
          no_output_timeout: 30m
workflows:
  version: 2.1
  default_workflow:
    jobs:
      - install
      - checks-and-unit-tests:
          requires:
            - install
      - e2e-1:
          requires:
            - install
      - e2e-2:
          requires:
            - install
      - e2e-3:
          requires:
            - install
      - e2e-4:
          requires:
            - install
      - e2e-5:
          requires:
            - install
      - e2e-6:
          requires:
            - install
      - e2e-7:
          requires:
            - install
      - e2e-8:
          requires:
            - install
