# Use the latest 2.1 version of CircleCI pipeline process engine.
# See: https://circleci.com/docs/2.0/configuration-reference
version: 2.1

executors:
  cypress-v22:
    docker:
      - image: cypress/base:22.20.0
  cypress-v12:
    docker:
      - image: cypress/base:12

jobs:
  install-deps:
    executor: cypress-v22
    steps:
      - checkout
      - run:
          name: Install Dependencies
          command: npm ci
      - persist_to_workspace:
          root: '.'
          paths: [node_modules]

  lint:
    executor: cypress-v22
    steps:
      - checkout
      - attach_workspace: {at: '.'}
      - run:
          name: Prettier Check
          command: npm run format:check

  build:
    executor: cypress-v22
    steps:
      - checkout
      - attach_workspace: {at: '.'}
      - run:
          name: Build
          command: npm run build
      - persist_to_workspace:
          root: 'src'
          paths: [.]

  test:
    executor: cypress-v22
    parallelism: 4
    steps:
      - checkout
      - attach_workspace: {at: src}
      - run:
          name: Install Test Dependencies
          command: cd test && npm ci
      - run:
          name: Test
          command: cd test && npm run test-ci

  backwards-compatibility:
    executor: cypress-v12
    parallelism: 4
    steps:
      - checkout
      - run:
          name: Install Dependencies
          command: npm i --only=prod
      - attach_workspace: {at: src}
      - run:
          name: Set backward package json
          command: cd test && rm package-lock.json && mv package.backward.json package.json
      - run:
          name: Install Test Dependencies
          command: cd test && npm i
      - run:
          name: Test
          command: cd test && npm run test-backwards-ci

workflows:
  # Below is the definition of your workflow.
  # Inside the workflow, you provide the jobs you want to run, e.g this workflow runs the build-and-test job above.
  # CircleCI will run this workflow on every commit.
  # For more details on extending your workflow, see the configuration docs: https://circleci.com/docs/2.0/configuration-reference/#workflows
  build_lint_test:
    jobs:
      - install-deps
      - lint:
          requires:
            - install-deps
      - build:
          requires:
            - install-deps
      - test:
          requires:
            - build
      - backwards-compatibility:
          requires:
            - build
