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

defaults: &defaults
  working_directory: ~/project
  docker:
    - image: circleci/node:latest

jobs:

  #------------------------------------------------------------
  # 1. Install dependencies
  #------------------------------------------------------------

  install-dependencies:
    <<: *defaults
    steps:
      - checkout

      - restore_cache:
          keys:
            - v1-deps-{{ checksum "yarn.lock" }}
            - v1-deps

      - run:
          name: 'Install dependencies'
          command: yarn --frozen-lockfile --non-interactive

      - save_cache:
          key: v1-deps-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache/yarn

      - persist_to_workspace:
          root: ~/project
          paths:
            - node_modules
            - packages/*/node_modules
            - packages/@vuepress/*/node_modules

  #------------------------------------------------------------
  # 2. Run parallel jobs:
  #   => tsc
  #   => tests
  #   => linter
  #   => docs linter
  #------------------------------------------------------------

  run-tsc:
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: ~/project
      - run:
          name: 'Run tsc'
          command: yarn tsc
      - persist_to_workspace:
          root: ~/project
          paths:
            - packages/@vuepress/shared-utils/lib

  run-tests:
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: ~/project
      - run:
          name: 'Run tests'
          command: yarn test

  run-linter-check:
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: ~/project
      - run:
          name: 'Run linter'
          command: yarn lint

  run-docs-linter-check:
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: ~/project
      - run:
          name: 'Run md linter'
          command: yarn workspace docs lint-md

  #------------------------------------------------------------
  # 3. Build VuePress
  #------------------------------------------------------------

  build:
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: ~/project
      - run:
          name: 'Run tests'
          command: yarn build

#------------------------------------------------------------
# Workflows
#------------------------------------------------------------

workflows:
  version: 2
  build:
    jobs:
      - install-dependencies
      - run-linter-check: { requires: [install-dependencies] }
      - run-docs-linter-check: { requires: [install-dependencies] }
      - run-tsc: { requires: [install-dependencies] }
      - run-tests: { requires: [run-tsc] }
      - build: { requires: [run-tests, run-linter-check, run-docs-linter-check] }
