version: 2.1

# Variables
var_1: &working_directory ~/repo
var_2:
  &nx_cli [
    'e2e-workspace',
    'e2e-cli,e2e-nx-plugin,dep-graph-client-e2e',
    'e2e-cypress,e2e-jest',
    'e2e-react',
    'e2e-next,e2e-gatsby',
    'e2e-node',
    'e2e-web,e2e-linter,e2e-storybook',
  ]
var_3: &ng_cli ['e2e-angular']

executors:
  linux:
    working_directory: *working_directory
    docker:
      - image: cimg/node:12.20-browsers

  windows:
    working_directory: *working_directory
    resource_class: windows.medium
    machine:
      image: windows-server-2019-vs2019:stable
    shell: cmd.exe

commands:
  setup:
    parameters:
      os:
        type: string
    steps:
      - checkout
      - restore_cache:
          name: Restore Yarn Package Cache
          keys:
            - nrwl-nx-yarn-packages-<< parameters.os >>-{{ checksum "yarn.lock" }}
      - run:
          name: Install dependencies
          command: yarn install --frozen-lockfile --non-interactive --cache-folder ~/.cache/yarn
      - save_cache:
          name: Save Yarn Package Cache
          # Windows needs its own cache key because binaries in node_modules are different.
          key: nrwl-nx-yarn-packages-<< parameters.os >>-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache/yarn

      - when:
          condition:
            equal: [<< parameters.os >>, linux]
          steps:
            - run:
                name: Install PNPM
                command: npm install --prefix=$HOME/.local -g pnpm
      - when:
          condition:
            equal: [<< parameters.os >>, windows]
          steps:
            - run:
                name: Install PNPM
                command: npm install -g pnpm

jobs:
  checks-and-unit-tests:
    parameters:
      os:
        type: string
        default: 'linux'
    executor: << parameters.os >>
    steps:
      - setup:
          os: << parameters.os >>
      - 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:
    parameters:
      os:
        type: string
      packages:
        type: string
      cli:
        type: string
        default: ''
      pm:
        type: string
        default: pnpm
    executor: << parameters.os >>
    steps:
      - setup:
          os: << parameters.os >>
      - run:
          name: Run E2E tests - << parameters.packages >>
          command: yarn e2e << parameters.packages >> affected
          no_output_timeout: 30m
    environment:
      NX_E2E_CI_CACHE_KEY: e2e-circleci-<< parameters.os >>-<< parameters.pm >>
      SELECTED_PM: << parameters.pm >>
      SELECTED_CLI: << parameters.cli >>

workflows:
  build:
    jobs:
      - checks-and-unit-tests
      - e2e:
          matrix:
            parameters:
              os: ['linux']
              packages: *nx_cli
      - e2e:
          matrix:
            parameters:
              os: ['linux']
              packages: *ng_cli
          cli: 'angular'

  build-windows:
    triggers:
      - schedule:
          cron: '0 0 * * *'
          filters:
            branches:
              only:
                - master
    jobs:
      - e2e:
          matrix:
            parameters:
              os: ['windows']
              packages: *nx_cli
      - e2e:
          matrix:
            parameters:
              os: ['windows']
              packages: *ng_cli
          cli: 'angular'
