version: 2
jobs:
  test:
    docker:
      - image: circleci/node:16
    working_directory: /tmp/multipart

    steps:
      - checkout
      - restore_cache:
          keys:
            - dependencies-{{ checksum "package-lock.json" }}
      - run:
          name: Install node dependencies
          command: npm install
      - save_cache:
          paths:
            - node_modules
          key: dependencies-{{ checksum "package-lock.json" }}
      - run:
          name: Run tests
          command: npm run test

  build:
    docker:
      - image: circleci/node:16

    working_directory: /tmp/multipart

    steps:
      - checkout
      - restore_cache:
          keys:
            - dependencies-{{ checksum "package-lock.json" }}
      - run:
          name: Install node dependencies
          command: npm install
      - run:
          name: Build library
          command: npm run build
      - save_cache:
          paths:
            - dist
          key: dist-{{ .Branch }}-{{ .Revision }}
      - run:
          name: Release
          command: npx semantic-release

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