# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2.1

# orbs:
#   windows: circleci/windows@2.4.0

defaults: &defaults
  working_directory: ~/repo
  docker:
    - image: circleci/node:10.16.0-browsers

jobs:
  build:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          keys:
            - v4-dependencies-{{ checksum "yarn.lock" }}
      - run: yarn install --frozen-lockfile
      - save_cache:
          paths:
            - node_modules
          key: v4-dependencies-{{ checksum "yarn.lock" }}
      - run: npm run build
      - run: npm run build:types
      - persist_to_workspace:
          root: ~/
          paths:
            - repo/node_modules
            - repo/packages/*
  test-local:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          keys:
            - v4-dependencies-{{ checksum "yarn.lock" }}
      - attach_workspace:
          at: ~/
      - run: npm run lint
      - run: npm run test:node
      - run: npm run test:browser
  test-bs:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          keys:
            - v4-dependencies-{{ checksum "yarn.lock" }}
      - attach_workspace:
          at: ~/
      - run: npm run test:bs
  # test-windows:
  #   executor: windows/default
  #   steps:
  #     - checkout
  #     - attach_workspace:
  #         at: ~/
  #     # reinstall dependencies, cannot use cached dependencies from a linux VM
  #     - run: yarn install --frozen-lockfile
  #     - run: npm run test:node-windows
  deploy:
    <<: *defaults
    steps:
      # will add github as known host
      - checkout
      - attach_workspace:
          at: ~/
      - add_ssh_keys:
          fingerprints:
            - '3f:ea:54:b7:77:13:b6:cf:29:90:2b:19:bb:eb:b5:f1'
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
      - run: git config --global user.email circleci@circleci
      - run: git config --global user.name CircleCI
      - run:
          name: Publish package
          command: "./node_modules/.bin/lerna publish --message 'chore: release new versions' --yes"

workflows:
  version: 2
  test-deploy:
    jobs:
      - build
      - test-local:
          requires:
            - build
      - test-bs:
          requires:
            - build
      - deploy:
          requires:
            - test-local
            - test-bs
          filters:
            branches:
              only: master
