version: 2

defaults: &defaults
  working_directory: ~/cupertino-pane
  docker:
    - image: circleci/node:16

jobs:
  test:
    <<: *defaults  
    steps:
      - checkout

      - run:
          name: update-npm
          # command: 'sudo npm install -g npm@latest && sudo npm install -g gulp'
          command: 'sudo npm install -g gulp'

      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "package.json" }}
          - v1-dependencies- # fallback to using the latest cache if no exact match is found

      - run: npm install

      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}

      - run: # run build
          name: Make build
          command: npm run build

      - persist_to_workspace:
          root: ~/cupertino-pane
          paths: .

  deploy:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/cupertino-pane
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$npm_TOKEN" > ~/cupertino-pane/.npmrc
      - run:
          name: Publish package
          command: npm publish
            
workflows:
  version: 2
  test-deploy:
    jobs:
      - test:
          filters:
            tags:
              only: /^v.*/
      - deploy:
          requires:
            - test
          filters:
            tags:
              only: /^v.*/
            branches:
              ignore: /.*/
