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

defaults: &defaults
  docker:
    - image: circleci/node:10.16.3
  working_directory: ~/vue-tour

jobs:
  build:
    <<: *defaults
    steps:
      - checkout

      # install Cypress dependencies
      - run: sudo apt-get update && sudo apt-get install -y libgtk2.0-0 libnotify-dev libgconf-2-4 libnss3 libxss1 libasound2 xvfb

      # 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" }}

      # run unit tests!
      - run: npm run test:unit
      - run: npm run build

      - run: git worktree add ../vue-tour-landing origin/landing

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

      - run:
          command: npm ci
          working_directory: ~/vue-tour-landing

      # - save_cache:
      #     paths:
      #       - ../vue-tour-landing/node_modules
      #       - /home/circleci/.cache/Cypress
      #     key: v1-dependencies-{{ checksum "../vue-tour-landing/package.json" }}

      # run e2e tests!
      - run:
          command: npm run test:e2e -- --headless
          working_directory: ~/vue-tour-landing

      - persist_to_workspace:
          root: ~/
          paths:
            - vue-tour
            - vue-tour-landing

  publish:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/

      - add_ssh_keys:
          fingerprints:
            - "eb:09:ae:75:b5:8e:66:63:27:9a:c0:cb:64:6e:79:d5"

      # generate a new landing build and commit it to the gh-pages branch
      - run:
          command: npm run build
          working_directory: ~/vue-tour-landing

      - run:
          command: cp -R ./dist ../
          working_directory: ~/vue-tour-landing

      - run:
          command: |
            ssh-keyscan github.com >> ~/.ssh/known_hosts
            git checkout --track origin/gh-pages
            rm -rf *
            cp -R ../dist/. ./
            git config user.email "mathieumorainville@hotmail.com"
            git config user.name "Mathieu Morainville"
            git status
            git add .
            git commit -m "chore(ci): generate a new build"
            git push origin gh-pages
          working_directory: ~/vue-tour-landing

      # we could use standard-version here to generating a changelog, bump the version with a tag and commit it

      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc
          when: always

      - run:
          name: Publish package
          command: cat .npmrc
          # command: npm publish

# use workflows to publish only on tagged commits (see: https://circleci.com/blog/publishing-npm-packages-using-circleci-2-0/)
workflows:
  version: 2
  build-publish:
    jobs:
      - build:
          filters:
            branches:
              only:
                - master
                - staging
      - publish:
          requires:
            - build
          filters:
            tags:
              only: /^v.*/
            branches:
              only:
                - master
