# Use the latest 2.1 version of CircleCI pipeline process engine.
# See: https://circleci.com/docs/2.0/configuration-reference
version: 2.1

# Define a job to be invoked later in a workflow.
# See: https://circleci.com/docs/2.0/configuration-reference/#jobs
defaults: &defaults
  working_directory: ~/repo
  docker:
    - image: cimg/node:20.17.0

jobs:
  build:
    <<: *defaults
    steps:
      - checkout
      - run:
          name: Installing dependencies
          command: npm install
      - run:
          name: Building package
          command: npm run build
      - persist_to_workspace:
          root: ~/
          paths: .

  unit-tests:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/
      - run:
          name: "Running unit tests"
          command: npm run test

  lint-check:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/
      - run:
          name: "Linting the code with tslint"
          command: npm run lint

  deploy:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/
      - run:
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
      - run: git config user.email "art@deliverr.com"
      - run: git config user.name "Github Bot"
      - run:
          name: Bumping package version
          command: npm version patch -m "[skip ci] updated to v%s. published to npm." --yes
      - run:
          name: Push changes to Github
          command: git push -u origin HEAD && git push --tags
      - run:
          name: Publishing packages to npm
          command: npm publish


workflows:
  version: 2
  build-test-lint-deploy:
    jobs:
      - build
      - unit-tests:
          requires:
            - build
      - lint-check:
          requires:
            - build
      - deploy:
          context: [ npm-publish ]
          requires:
            - lint-check
            - unit-tests
          filters:
            branches:
              only:
                - master
                - 0.x
