version: 2

defaults: &defaults
  working_directory: ~/screeny
  docker:
    - image: circleci/node:11.3.0

jobs:
  install:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          key: v1-dependency-cache-{{ checksum "package.json" }}
      - run:
          command: |
            yarn
      - save_cache:
          key: v1-dependency-cache-{{ checksum "package.json" }}
          paths:
            - node_modules
      - persist_to_workspace:
          root: ~/screeny
          paths: .

  lint:
    <<: *defaults
    parallelism: 1
    steps:
      - checkout
      - restore_cache:
          key: v1-dependency-cache-{{ checksum "package.json" }}
      - run: yarn run lint

  deploy:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/screeny
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$npm_TOKEN" > ~/screeny/.npmrc
      - run:
          name: Publish package
          command: npm publish

workflows:
  version: 2
  web-workflow:
    jobs:
      - install

      - lint:
          requires:
            - install

      - deploy:
          requires:
            - lint
          filters:
            branches:
              only: master
