defaults: &defaults
  working_directory: ~/repo
  docker:
    - image: circleci/node:current

whitelist: &whitelist
  paths:
    - .npmignore
    - coverage/*
    - dist/*
    - node_modules/*
    - src/*
    - test/*
    - CODE_OF_CONDUCT.md
    - LICENSE.md
    - package.json
    - README.md
    - tsconfig.json
    - tslint.json
version: 2
jobs:
  checkout:
    <<: *defaults

    steps:
      - checkout

      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "package.json" }}
          - v1-dependencies-

      - run:
          name: Install Dependencies
          command: yarn install

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

      - persist_to_workspace:
          root: ~/repo
          <<: *whitelist

  build:
    <<: *defaults

    steps:
      - attach_workspace:
          at: ~/repo

      - run:
          name: install TypeScript
          command: yarn add typescript --dev

      - run:
          name: Build TypeScript code
          command: yarn build

      - persist_to_workspace:
          root: ~/repo
          <<: *whitelist

  test:
    <<: *defaults

    steps:
      - attach_workspace:
          at: ~/repo

      - run:
          name: install mocha
          command: yarn global add mocha --dev

      - run:
          name: Run test
          command: yarn test

      - persist_to_workspace:
          root: ~/repo
          <<: *whitelist

  deploy:
    <<: *defaults

    steps:
      - attach_workspace:
          at: ~/repo

      - run: 
          name: Write NPM Token to ~/.npmrc
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc

      - run: 
          name: Install dot-json package
          command: npm install dot-json

      - run:
          name: Write version to package.json
          command: $(yarn bin)/dot-json package.json version ${CIRCLE_TAG:1}

      - run:
          name: Publish to NPM
          command: npm publish --access=public

workflows:
  version: 2

  build:
    jobs:
      - checkout
      - build:
          requires:
            - checkout
            
  test:
    jobs:
      - checkout
      - build:
          requires:
            - checkout
      - test:
          requires:
            - build

  release:
    jobs:
      - checkout:
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/
      - build:
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/
      - deploy:
          filters:
            tags:
              only: /v[0-9]+(\.[0-9]+)*/
            branches:
              ignore: /.*/
          requires:
            - build
