version: 2.1

jobs:
#   "node-8":
#     docker:
#       - image: circleci/node:8
#     steps:
#       - checkout
#       - restore_cache:
#           keys:
#             - node-v1-{{ .Branch }}-{{ checksum "package-lock.json" }}
#             - node-v1-{{ .Branch }}-
#             - node-v1-
#       - run: make node_modules
#       - save_cache:
#           paths:
#             - ~/project/node_modules
#           key: node-v1-{{ .Branch }}-{{ checksum "package-lock.json" }}
# # - run: make lint
#       - run: make build test-node
#   "node-10":
#     docker:
#       - image: circleci/node:10
#     steps:
#       - checkout
#       - restore_cache:
#           keys:
#             - node-v1-{{ .Branch }}-{{ checksum "package-lock.json" }}
#             - node-v1-{{ .Branch }}-
#             - node-v1-
#       - run: make node_modules
#       - save_cache:
#           paths:
#             - ~/project/node_modules
#           key: node-v1-{{ .Branch }}-{{ checksum "package-lock.json" }}
# # - run: make lint
#       - run: make build test-node        
  "node-12":
    docker:
      - image: circleci/node:12
    steps:
      - checkout
      - restore_cache:
          keys:
            - node-v1-{{ .Branch }}-{{ checksum "package-lock.json" }}
            - node-v1-{{ .Branch }}-
            - node-v1-
      - run: make node_modules
      - save_cache:
          paths:
            - ~/project/node_modules
          key: node-v1-{{ .Branch }}-{{ checksum "package-lock.json" }}
      - run: make lint
      - run: make build test-node
  "node-14":
    docker:
      - image: circleci/node:14
    steps:
      - checkout
      - restore_cache:
          keys:
            - node-v1-{{ .Branch }}-{{ checksum "package-lock.json" }}
            - node-v1-{{ .Branch }}-
            - node-v1-
      - run: make node_modules
      - save_cache:
          paths:
            - ~/project/node_modules
          key: node-v1-{{ .Branch }}-{{ checksum "package-lock.json" }}
      - run: make lint
      - run: make build test-node
  "node-16":
    docker:
      - image: circleci/node:16
    steps:
      - checkout
      - restore_cache:
          keys:
            - node-v1-{{ .Branch }}-{{ checksum "package-lock.json" }}
            - node-v1-{{ .Branch }}-
            - node-v1-
      - run: make node_modules
      - save_cache:
          paths:
            - ~/project/node_modules
          key: node-v1-{{ .Branch }}-{{ checksum "package-lock.json" }}
      - run: make lint
      - run: make build test-node          
  "node-latest":
    working_directory: ~/repo
    docker:
      - image: circleci/node:latest
    steps:
      - checkout
      - restore_cache:
          keys:
            - node-v1-{{ .Branch }}-{{ checksum "package-lock.json" }}
            - node-v1-{{ .Branch }}-
            - node-v1-
      - run: make node_modules
      - save_cache:
          paths:
            - ~/project/node_modules
          key: node-v1-{{ .Branch }}-{{ checksum "package-lock.json" }}
      - run: make lint
      - run: make build test-node
      - persist_to_workspace:
          root: ~/repo
          paths: .

  deploy:
    working_directory: ~/repo
    docker:
      - image: circleci/node:latest
    steps:
      - attach_workspace:
          at: ~/repo
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/repo/.npmrc
      - run:
          name: Publish package
          command: npm run release

workflows:
  version: 2
  test:
    jobs:
      # - "node-8":
      #     filters:
      #       branches:
      #         only: /.*/
      # - "node-10":
      #     filters:
      #       branches:
      #         only: /.*/
      - "node-12":
          filters:
            branches:
              only: /.*/
      - "node-14":
          filters:
            branches:
              only: /.*/
      - "node-16":
          filters:
            branches:
              only: /.*/
      - "node-latest":
          filters:
            branches:
              only: /.*/
  release:
    jobs:
      - "node-latest":
          filters:
            tags:
              only: /^v.*-no-protobuf$/
            branches:
              ignore: /.*/
      - deploy:
          requires:
            - "node-latest"
          filters:
            tags:
              only: /^v.*-no-protobuf$/
            branches:
              ignore: /.*/