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

    working_directory: ~/repo
    resource_class: large

    environment:
      # Customize the JVM maximum heap limit
      JVM_OPTS: -Xmx1g

    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:
          name: common lint
          working_directory: "./common"
          command: |
            clj-kondo --version
            clj-kondo --parallel --lint src/

      - run:
          name: frontend lint
          working_directory: "./frontend"
          command: |
            clj-kondo --version
            clj-kondo --parallel --lint src/

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

      - run:
          name: backend lint
          working_directory: "./backend"
          command: |
            clj-kondo --version
            clj-kondo --parallel --lint src/

      # run backend test
      - run:
          name: backend test
          working_directory: "./backend"
          command: "clojure -X:dev: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"

      - run:
          name: frontend tests
          working_directory: "./frontend"
          command: |
            yarn install
            clojure -M:dev:shadow-cljs compile test
            node target/tests.js

          environment:
            PATH: /usr/local/nodejs/bin/:/usr/local/bin:/bin:/usr/bin

      - run:
          working_directory: "./common"
          name: common tests (cljs)
          command: |
            yarn install
            yarn run compile-test
            node target/test.js

          environment:
            PATH: /usr/local/nodejs/bin/:/usr/local/bin:/bin:/usr/bin

      - run:
          working_directory: "./common"
          name: common tests (clj)
          command: |
            clojure -X:dev:test

          environment:
            PATH: /usr/local/nodejs/bin/:/usr/local/bin:/bin:/usr/bin

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