version: 2
jobs:
  build:
    docker:
      - image: penpotapp/devenv:latest
      - image: cimg/postgres:14.5
        environment:
          POSTGRES_USER: penpot_test
          POSTGRES_PASSWORD: penpot_test
          POSTGRES_DB: penpot_test
      - image: cimg/redis:7.0.5

    working_directory: ~/repo
    resource_class: medium+

    environment:
      JAVA_OPTS: -Xmx4g -Xms100m -XX:+UseSerialGC
      NODE_OPTIONS: --max-old-space-size=4096

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
         keys:
           - v1-dependencies-{{ checksum "backend/deps.edn" }}-{{ checksum "frontend/deps.edn"}}-{{ checksum "common/deps.edn"}}
           # fallback to using the latest cache if no exact match is found
           - v1-dependencies-

      - run: cd .clj-kondo && cat config.edn
      - run: cat .cljfmt.edn
      - run: clj-kondo --version

      - run:
          name: "fmt check backend [clj]"
          working_directory: "./backend"
          command: |
            yarn install
            yarn run fmt:clj:check

      - run:
          name: "fmt check exporter [clj]"
          working_directory: "./exporter"
          command: |
            yarn install
            yarn run fmt:clj:check

      - run:
          name: "fmt check common [clj]"
          working_directory: "./common"
          command: |
            yarn install
            yarn run fmt:clj:check

      - run:
          name: "fmt check frontend [clj]"
          working_directory: "./frontend"
          command: |
            yarn install
            yarn run fmt:clj:check

      - run:
          name: common lint
          working_directory: "./common"
          command: |
            yarn install
            yarn run lint:clj

      - run:
          name: frontend lint
          working_directory: "./frontend"
          command: |
            yarn install
            yarn run lint:scss
            yarn run lint:clj

      - run:
          name: backend lint
          working_directory: "./backend"
          command: |
            yarn install
            yarn run lint:clj

      - run:
          name: exporter lint
          working_directory: "./exporter"
          command: |
            yarn install
            yarn run lint:clj

      - run:
          name: "common tests"
          working_directory: "./common"
          command: |
            yarn test
            clojure -X:dev:test :patterns '["common-tests.*-test"]'

      - run:
          name: "frontend tests"
          working_directory: "./frontend"
          command: |
            yarn install
            yarn test

      - run:
          name: "backend tests"
          working_directory: "./backend"
          command: |
            clojure -X:dev:test :patterns '["backend-tests.*-test"]'

          environment:
            PENPOT_TEST_DATABASE_URI: "postgresql://localhost/penpot_test"
            PENPOT_TEST_DATABASE_USERNAME: penpot_test
            PENPOT_TEST_DATABASE_PASSWORD: penpot_test
            PENPOT_TEST_REDIS_URI: "redis://localhost/1"

      - save_cache:
         paths:
           - ~/.m2
         key: v1-dependencies-{{ checksum "backend/deps.edn" }}-{{ checksum "frontend/deps.edn"}}-{{ checksum "common/deps.edn"}}