version: 2.1
orbs:
  node: circleci/node@3.0.0

executors:
    node:
      parameters:
        image:
          type: string
          default: "12.13"
      docker:
        - image: circleci/node:<< parameters.image >>

aliases:
  install_node_modules: &install_node_modules
    run:
      name: Install node modules
      command: yarn --frozen-lockfile
  attach_to_bootstrap: &attach_to_bootstrap
    attach_workspace:
        at: ./

jobs:
  bootstrap:
    executor: node
    steps:
      - checkout
      - run: ./scripts/assert-changed-files.sh "packages/*|(e2e|integration)-tests/*|.circleci/*|yarn.lock"
      - <<: *install_node_modules
      - run: yarn bootstrap --concurrency=2
        # Persist the workspace again with all packages already built
      - persist_to_workspace:
          root: ./
          paths:
            - "*"

  unit_test:
    executor: node
    steps:
      - checkout
      - run: ./scripts/assert-changed-files.sh "packages"
      - <<: *attach_to_bootstrap
      - run:
          command: node --max-old-space-size=2048 ./node_modules/.bin/jest -w 1

workflows:
  test_all:
    jobs:
      - bootstrap
      - unit_test:
          requires:
            - bootstrap
