version: 2

references:
  workspace_root: &workspace_root ~/thincloud-v2-node-sdk

  attach_workspace: &attach_workspace
    attach_workspace:
      at: *workspace_root

defaults: &defaults
  working_directory: *workspace_root
  docker:
    - image: circleci/node:8.10.0-stretch

################## ALIASES ##################

aliases:
  - &bootstrap
    run:
      name: Install Dependencies & Bootstrap
      command: |
        echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> ~/.npmrc
        node --version
        npm --version
        npm i

  - &build
    run:
      name: Compile and Build Artifacts
      command: npm run build

  - &restore-cache
    keys:
      - dependencies-{{ .Branch }}-{{ checksum "package.json" }}
      - dependencies-{{ .Branch }}-

  - &save-cache
    paths:
      - node_modules
    key: dependencies-{{ .Branch }}-{{ checksum "package.json" }}

  - &git-setup
    run:
      name: Setup Git
      command: |
        mkdir ~/.ssh/ && echo -e "Host github.com\n\tStrictHostKeyChecking no\n" > ~/.ssh/config
        git config --global user.email "thincloud@yonomi.co"
        git config --global user.name "CircleCI"

  - &run-test-lint
    run:
      name: Linter
      command: npm run lint

  - &run-test-unit
    run:
      name: Unit Test & Coverage
      command: npm run test:unit:ci

  - &run-release
    run:
      name: Release
      command: npm run release

#################### JOBS ####################

jobs:
  build:
    <<: *defaults
    steps:
      - checkout
      - restore_cache: *restore-cache
      - *bootstrap
      - *build
      - save_cache: *save-cache
      - persist_to_workspace:
          root: ~/thincloud-v2-node-sdk
          paths:
            - .

  test-lint:
    <<: *defaults
    steps:
      - *attach_workspace
      - *run-test-lint

  test-unit:
    <<: *defaults
    steps:
      - *attach_workspace
      - *run-test-unit
      - store_artifacts:
          path: coverage
          prefix: coverage

  release:
    <<: *defaults
    steps:
      - *attach_workspace
      - *git-setup
      - *run-release

  publish-to-npm:
    <<: *defaults
    steps:
      - *attach_workspace
      - run:
          name: Publish to npm
          command: npm publish --access public

#################### WORKFLOWS ####################

workflows:
  version: 2
  open-pr:
    jobs:
      - build:
          filters:
            branches:
              ignore: master
      - test-lint:
          requires:
            - build
      - test-unit:
          requires:
            - build

  merged-to-master:
    jobs:
      - build:
          filters:
            branches:
              only: master
            tags:
              ignore: /.*/
      - test-lint:
          requires:
            - build
      - test-unit:
          requires:
            - build
      - release:
          requires:
            - test-lint
            - test-unit
      - hold:
          requires:
            - release
          type: approval
      - publish-to-npm:
          filters:
            tags:
              only: /^v.*/
            branches:
              ignore: /.*/
          requires:
            - hold
