version: 2.1

orbs:
  node: circleci/node@4.1.0
  codecov: codecov/codecov@1.0.5

jobs:

  checkout:
    executor:
      name: node/default
    steps:
      - checkout
      - persist_to_workspace:
          root: .
          paths:
            - .

  lint:
    executor:
      name: node/default
    steps:
      - attach_workspace:
          at: .
      - node/install-packages
      - run: npm run lint

  test:
    executor:
      name: node/default
    steps:
      - attach_workspace:
          at: .
      - node/install-packages
      - run: npm test
      - codecov/upload:
          file: ./coverage/lcov.info

  build:
    executor:
      name: node/default
    steps:
      - attach_workspace:
          at: .
      - node/install-packages
      - run: npm run build:prod
      - persist_to_workspace:
          root: .
          paths:
            - ./build # Only persist the build files, since persisting large folders (like node_modules) results in long persisting/attaching times

  release:
    docker:
      - image: 'circleci/node:latest'
    executor:
      name: node/default
    steps:
      - attach_workspace:
          at: .
      - node/install-packages:
          override-ci-command: npm install --legacy-peer-deps
      - run: npm run cleanup
      - run:
          name: copy files
          command: |
            cp package.json README.md LICENSE ./build
            cd ./build
            mv node_modules external
            find . -type f | xargs sed -i  's/node_modules/external/g'
      - run:
          name: release
          command: |
            cd ./build
            node ../node_modules/.bin/semantic-release

workflows:
    build-and-test:
      jobs:
        - checkout
        - lint:
            requires:
              - checkout
        - test:
            requires:
              - checkout
        - build:
            requires:
              - checkout
        - release:
            requires:
              - lint
              - test
              - build