aliases:
  - &install_yarn_version
    name: Install specific Yarn version
    command: |
      curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.16.0
      echo 'export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"' >> $BASH_ENV

  - &restore_yarn_cache
    name: Restore Yarn cache
    keys:
      - yarn-{{ .Branch }}-packages-{{ checksum "yarn.lock" }}

  - &save_yarn_cache
    name: Save Yarn cache
    key: yarn-{{ .Branch }}-packages-{{ checksum "yarn.lock" }}
    paths:
      - ~/.cache/yarn

  - &run_yarn_install
    name: Install dependencies
    command: yarn install --pure-lockfile --ignore-engines

defaults: &defaults
  working_directory: ~/algoliasearch-client-javascript
  parameters:
    cimg_version:
      type: string
    node_version:
      type: string
      default: ''
  docker:
    - image: cimg/node:<< parameters.cimg_version >>

version: 2.1
jobs:
  test_build:
    description: Testing build & Test Size & Declarations File
    <<: *defaults
    steps:
      - checkout
      - run: *install_yarn_version
      - restore_cache: *restore_yarn_cache
      - run: *run_yarn_install
      - save_cache: *save_yarn_cache
      - run:
          name: Build
          command: yarn build
      - run:
          name: Test size
          command: yarn test:build-size
      - run:
          name: Validate declarations
          command: yarn test:build-declarations

  test_lint:
    description: Testing coding style
    <<: *defaults
    steps:
      - checkout
      - run: *install_yarn_version
      - restore_cache: *restore_yarn_cache
      - run: *run_yarn_install
      - save_cache: *save_yarn_cache
      - run:
          name: Lint tests
          command: yarn test:lint

  test_types:
    description: Testing type checking
    <<: *defaults
    steps:
      - checkout
      - run: *install_yarn_version
      - restore_cache: *restore_yarn_cache
      - run: *run_yarn_install
      - save_cache: *save_yarn_cache
      - run:
          name: Lint tests
          command: yarn test:types

  test_unit:
    description: Testing code against node << parameters.node_version >>
    <<: *defaults
    steps:
      - checkout
      - run:
          name: Install node version
          command: |
            if [ '<< parameters.node_version >>' ]; then
              curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash;
              export NVM_DIR="$HOME/.nvm";
              [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh";
              nvm install '<< parameters.node_version >>';
            fi;
      - run: *install_yarn_version
      - restore_cache: *restore_yarn_cache
      - run: *run_yarn_install
      - save_cache: *save_yarn_cache
      - run:
          name: Unit Tests
          command: yarn test:unit --maxWorkers=4

  test_browser:
    description: Testing code within browsers
    <<: *defaults
    steps:
      - checkout
      - run: *install_yarn_version
      - restore_cache: *restore_yarn_cache
      - run: *run_yarn_install
      - save_cache: *save_yarn_cache
      - run:
          name: Build
          command: yarn build
      - run:
          name: Browser tests
          command: yarn test:browser-ci

  release:
    working_directory: ~/algoliasearch-client-javascript
    description: Perform a new release of the JavaScript client
    docker:
      - image: cimg/node:14.17
    steps:
      - checkout
      - run:
          command: yarn install
      - run:
          command: |
            if [[ -z "$GITHUB_TOKEN" ]]; then echo '$GITHUB_TOKEN is not set'; exit 1; fi
            yarn shipjs trigger

workflows:
  version: 2
  ci:
    jobs:
      - test_unit:
          # workaround: cimg/node:8 certificate is unsigned, we use nvm instead
          cimg_version: '14.17'
          node_version: '8.17'
          name: 'test_unit_8'
      - test_unit:
          cimg_version: '14.17'
          name: 'test_unit_14'
      - test_lint:
          cimg_version: '12.22'
      - test_types:
          cimg_version: '12.22'
      - test_build:
          cimg_version: '12.16.2'
      - test_browser:
          cimg_version: '12.16.2'
      - release:
          requires:
            - test_unit_8
            - test_unit_14
            - test_lint
            - test_types
            - test_build
            - test_browser
          filters:
            branches:
              only:
                - master
