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

executors:
  node:
    docker:
      - image: circleci/node:12.7
    working_directory: ~/repo
    environment:
      GIT_AUTHOR_EMAIL: visjsbot@gmail.com
      GIT_AUTHOR_NAME: vis-bot
      GIT_COMMITTER_EMAIL: visjsbot@gmail.com
      GIT_COMMITTER_NAME: vis-bot

jobs:
  prepare:
    executor: node

    steps:
      - checkout

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

      - run: npm ci

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

      - persist_to_workspace:
          root: .
          paths:
            - "*"

  build:
    executor: node

    steps:
      - attach_workspace:
          at: .

      - run: npm run build

      - persist_to_workspace:
          root: .
          paths:
            - "dist"

  examples:
    executor: node

    steps:
      - attach_workspace:
          at: .

      - run:
          name: Install dependencies for headless Chromium
          command: |
            sudo apt-get install -yq \
            ca-certificates fonts-liberation gconf-service libappindicator1 \
            libasound2 libatk-bridge2.0-0 libatk1.0-0 libc6 libcairo2 libcups2 \
            libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 \
            libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libnss3 \
            libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 \
            libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 \
            libxi6 libxrandr2 libxrender1 libxss1 libxtst6 lsb-release wget \
            xdg-utils
      - run: npm run generate-examples-index

      - persist_to_workspace:
          root: .
          paths:
            - "examples"

  lint_js:
    executor: node

    steps:
      - attach_workspace:
          at: .

      - run: npm run lint:js

  lint_ts:
    executor: node

    steps:
      - attach_workspace:
          at: .

      - run: npm run lint:ts

  test:
    executor: node

    steps:
      - attach_workspace:
          at: .

      - run: npm run test-cov

  gh_pages:
    executor: node

    steps:
      - attach_workspace:
          at: .

      - run:
          name: Set Git credentials
          command: |
            echo 'machine github.com' >> ~/.netrc
            echo "        login $GIT_AUTHOR_NAME" >> ~/.netrc
            echo "        password $GH_TOKEN" >> ~/.netrc
      - run:
          name: Publish GitHub Pages
          command: |
            npx gh-pages --dist . --message "chore: update to $(git rev-parse HEAD) [ci skip]" --repo "$(node -e 'process.stdout.write(require("./package.json").repository.url)')" --src '{{coverage,dist,docs,examples}/**/*,LICENSE*,*.{html,md}}'

  release:
    executor: node

    steps:
      - attach_workspace:
          at: .

      - run:
          name: Prepare NPM
          command: |
            npm set //registry.npmjs.org/:_authToken=$NPM_TOKEN

      - run:
          name: Release
          command: |
            npx semantic-release

workflows:
  version: 2

  build:
    jobs:
      - prepare

      - build:
          requires:
            - prepare

      - examples:
          requires:
            - prepare
            - build

      # - lint_js:
      #     requires:
      #       - prepare

      - lint_ts:
          requires:
            - prepare

      - test:
          requires:
            - prepare

      - gh_pages:
          requires:
            - prepare
            - build
            - examples
            # - lint_js
            - lint_ts
            - test
          filters:
            branches:
              only:
                - master

      - release:
          requires:
            - prepare
            - build
            # - lint_js
            - lint_ts
            - test
          filters:
            branches:
              only:
                - master
