# https://circleci.com/docs/language-javascript/
# This config is equivalent to both the '.circleci/extended/orb-free.yml' and the base '.circleci/config.yml'
version: 2.1

# https://circleci.com/blog/publishing-npm-packages-using-circleci-2-0/

# Orbs are reusable packages of CircleCI configuration that you may share across projects, enabling you to create encapsulated, parameterized commands, jobs, and executors that can be used across multiple projects.
# See: https://circleci.com/docs/orb-intro/
orbs:
  node: circleci/node@5.0.2

defaults: &defaults
  working_directory: ~/project
  executor: node/default # use the default executor defined within the orb

jobs:
  build-job: # this can be any name you choose
    <<: *defaults
    steps:
      - checkout
      - node/install-packages:
          pkg-manager: npm
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "package.json" }}
            - v1-dependencies- # fallback to using the latest cache if no exact match is found
      - run:
          name: Setup code climate test-reporter
          command: |
            # download test reporter as a static binary
            curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
            chmod +x ./cc-test-reporter
      - run:
          command: npm run build
          name: Build app
      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}
      - persist_to_workspace:
          root: ~/project
          paths:
            - .
  test-job: # this can be any name you choose
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/project
      - run:
          name: Setup code climate test-reporter
          command: |
            # download test reporter as a static binary
            curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
            chmod +x ./cc-test-reporter
      - run:
          command: npm run lint
          name: Lint app
      - run:
          command: npm run test
          name: Run tests
      - run:
          name: Code Climate Test Coverage
          command: |
            ./cc-test-reporter before-build
            npm run codeCoverage
            ./cc-test-reporter format-coverage -t clover coverage/clover.xml --output coverage/codeclimate.json
            cp coverage/clover.xml .     
            ./cc-test-reporter after-build --coverage-input-type clover --exit-code $?
  deploy-job: # this can be any name you choose
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/project
      - run:
          name: Authenticate with registry
          command: npm set //registry.npmjs.org/:_authToken=$NPM_TOKEN
      - run:
          name: Publish package
          # make sure you run `npm vestion major|minor|patch` locally before this step
          command: npm publish

workflows:
  pr-workflow: # this can be any name you choose
    when:
      not:
        equal: [main, << pipeline.git.branch >>]
    jobs:
      - build-job
      - test-job:
          requires:
            - build-job
          filters:
            branches:
              ignore: main
  merge-workflow:
    when:
      and:
        - equal: [main, << pipeline.git.branch >>]
    jobs:
      - build-job:
          filters:
            tags:
              only: /v.*/ # tags are inherited
      - deploy-job:
          requires:
            - build-job
          filters:
            tags:
              only: /v.*/ # only on commits that have this tag

# to deploy, run:
# npm version patch
# git push --atomic origin <tag-name> # push your commits and tags to origin, thus enabling deploy related jobs
