version: 2.1
aliases:
  - &defaults
    docker:
      - image: circleci/node:12
  - &attach_workspace
    - attach_workspace:
        at: ~/
jobs:
  build:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          key: dependency-cache-{{ arch }}-{{ .Branch }}-{{ checksum "package-lock.json" }}
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
      - run:
          name: Install dependencies
          command: npm ci
      - save_cache:
          key: dependency-cache-{{ arch }}-{{ .Branch }}-{{ checksum "package-lock.json" }}
          paths:
            - ~/.npm
            - ~/.cache
      - run:
          name: Build source
          command: npm run build
      - persist_to_workspace:
          root: ~/
          paths:
            - dist
            - node_modules
            - project
  lint:
    <<: *defaults
    steps:
      - checkout
      - <<: *attach_workspace
      - run:
          name: Run lint
          command: npm run lint
  unit-test:
    <<: *defaults
    steps:
      - checkout
      - <<: *attach_workspace
      - run:
          name: Run unit tests
          command: npm run test
workflows:
  version: 2
  commit:
    jobs:
      - build
      - lint:
          requires:
            - build
      - unit-test:
          requires:
            - build