# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2
jobs:
  build:
    docker:
      # specify the version you desire here
      - image: circleci/node:10.17.0-browsers

    working_directory: ~/repo

    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: yarn install --frozen-lockfile --prefer-offline

      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}

      # Build
      - run: yarn run build

      # Store build
      - store_artifacts:
          path: ./dist

      # Generate Workspace
      - run: mkdir -p workspace

      # Version number
      - run: node -p "require('./package.json').version" > workspace/version.txt

      # Copy build
      - run: mkdir -p workspace/build
      - run: cp ./dist/*.* workspace/build

      - persist_to_workspace:
          root: workspace
          paths:
            - version.txt
            - build
  publish:
    docker:
      # specify the version you desire here
      - image: cibuilds/github:0.10

    working_directory: ~/repo

    steps:
      - attach_workspace:
          at: ./workspace
      # Deploy on github
      - run:
          name: 'Publish Release on GitHub'
          command: |
            VERSION=$(cat ./workspace/version.txt)
            ghr -t ${GITHUB_TOKEN} -u ${CIRCLE_PROJECT_USERNAME} -r ${CIRCLE_PROJECT_REPONAME} -c ${CIRCLE_SHA1} -delete ${VERSION} ./workspace/build/
workflows:
  version: 2
  build_and_publish:
    jobs:
      - build
      - publish:
          filters:
            branches:
              only:
                - master
          requires:
            - build
