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

executors:
  browser-executor:
    docker:
      - image: cimg/node:16.20.2-browsers
    working_directory: ~/repo
# Define a job to be invoked later in a workflow.
# See: https://circleci.com/docs/2.0/configuration-reference/#jobs
jobs:
  build-plugin:
    executor: browser-executor
    # Add steps to the job
    # See: https://circleci.com/docs/2.0/configuration-reference/#steps
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "package-lock.json" }}
      - run:
          name: Install
          command: npm ci
      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package-lock.json" }}
      - run:
          name: Audit
          command: npm audit --audit-level=low
      - persist_to_workspace:
          root: .
          paths: [.]
  deploy-plugin:
    executor: browser-executor
    steps:
      - attach_workspace:
          at: ~/repo
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$npm_TOKEN" > .npmrc
      - run:
          name: Publish package
          command: npm publish --access public

# Invoke jobs via workflows
# See: https://circleci.com/docs/2.0/configuration-reference/#workflows
workflows:
  test-deploy:
    jobs:
      - build-plugin:
          filters:
            tags:
              only: /.*/
      - hold:
          type: approval
          requires:
            - build-plugin
          filters:
            # hold only on version tags
            tags:
              only: /^v.*/
            branches:
              ignore: /.*/
      - deploy-plugin:
          context:
            - fullstorydev mobile projects
          requires:
            - hold
          filters:
            # deploy only on version tags
            tags:
              only: /^v.*/
            branches:
              ignore: /.*/
