version: 2

aliases:
  - &restore-cache
    restore_cache:
      name: Restore Yarn Package Cache
      keys:
        - yarn-packages-{{ checksum "yarn.lock" }}
  - &install-deps
    run:
      name: Install dependencies
      command: yarn
  - &build-packages
    run:
      name: Build
      command: npm run build

jobs:
  build:
    working_directory: ~/nest
    docker:
      - image: cimg/node:20.3
    steps:
      - checkout
      - restore_cache:
          name: Restore Yarn Package Cache
          keys:
            - yarn-packages-{{ checksum "yarn.lock" }}
      - run:
          name: Install dependencies
          command: yarn
      - save_cache:
          name: Save Yarn Package Cache
          key: yarn-packages-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache/yarn
      - run:
          name: Build
          command: npm run build

  unit_tests:
    working_directory: ~/nest
    docker:
      - image: cimg/node:20.3
    steps:
      - checkout
      - restore_cache:
          name: Restore Yarn Package Cache
          keys:
            - yarn-packages-{{ checksum "yarn.lock" }}
      - run:
          name: Install dependencies
          command: yarn
      - save_cache:
          name: Save Yarn Package Cache
          key: yarn-packages-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache/yarn
      - run:
          name: Tests
          command: npm run test

workflows:
  version: 2
  build-and-test:
    jobs:
      - build
      - unit_tests:
          requires:
            - build
