version: 2

jobs:
  build:
    docker:
      - image: circleci/node:10.0
    working_directory: ~/repo
    filters:
      branches:
        only: master
    
    steps:
      - checkout
      
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "package.json" }}
            - v1-dependencies-

      - run: yarn install
      - run: 
          name: install tsc
          command: sudo npm install -g typescript@3.8.3

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

      - run:
          name: Build package
          command: tsc -v && tsc
      
      - run: git config --global user.email "$USER_MAIL"
      - run: git config --global user.name "$USER_NAME"        

      - run: git stash
      - run: npm version patch

      - run:
          name: Authenticate at npm.org
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/repo/.npmrc

      - run:
          name: Publish package
          command: npm publish