version: 2

test_env: &test_env
  RUN_POSTGRES_TESTS: 1
  POSTGRES_USER: postgres
  POSTGRES_DB: circle_database
  POSTGRES_HOST: localhost
  POSTGRES_PORT: 5432

postgres_service: &postgres_service
  image: circleci/postgres:9.6-alpine
  environment: # env to pass to CircleCI, specified values must match test_env
    POSTGRES_USER: postgres
    POSTGRES_DB: circle_database

node_unit_tests: &node_unit_tests
  steps:
    - checkout
    - run:
        name: Create Checksum
        command: sh .circleci/checksum.sh /tmp/checksums.txt
    - restore_cache:
        keys:
          - npm-cache-{{ checksum "/tmp/checksums.txt" }}
    - run:
        name: Install Dependencies
        command: yarn install
    - save_cache:
        key: npm-cache-{{ checksum "/tmp/checksums.txt" }}
        paths:
          - ./node_modules
          - ./yarn.lock
          - ./packages/*/node_modules
          - ./packages/*/yarn.lock
    - run:
        name: Compile code
        command: yarn compile
    - run:
        name: Unit tests
        command: yarn test
    - run:
        name: report coverage
        command: yarn codecov

browsers_unit_tests: &browsers_unit_tests
  steps:
    - checkout
    - run:
        name: Install Dependencies
        command: yarn install
    - run:
        name: Compile code
        command: yarn compile
    - run:
        name: Unit tests
        command: yarn test:browser
    - run:
        name: report coverage
        command: yarn codecov:browser

jobs:
  lint:
    docker:
      - image: node:12
    steps:
      - checkout
      - run:
          name: Install modules and dependencies.
          command: yarn install
      - run:
          name: Check code style and linting
          command: yarn run check
  docs:
    docker:
      - image: node:12
    steps:
      - checkout
      - run:
          name: Install modules and dependencies.
          command: yarn install
      - run:
          name: Docs tests
          command: yarn docs-test
  node8:
    docker:
      - image: node:8
        environment: *test_env
      - *postgres_service
    <<: *node_unit_tests
  node10:
    docker:
      - image: node:10
        environment: *test_env
      - *postgres_service
    <<: *node_unit_tests
  node11:
    docker:
      - image: node:11
        environment: *test_env
      - *postgres_service
    <<: *node_unit_tests
  node12:
    docker:
      - image: node:12
        environment: *test_env
      - *postgres_service
    <<: *node_unit_tests
  node12-browsers:
    docker:
      - image: circleci/node:12-browsers
    <<: *browsers_unit_tests

workflows:
  version: 2
  build:
    jobs:
      - lint
      - docs
      - node8
      - node10
      - node11
      - node12
      - node12-browsers
