version: 2
jobs:
  build:
    docker:
      - image: circleci/node:9.5.0
    working_directory: ~/html-webpack-externals-plugin
    steps:
      - checkout
      - run:
          name: Authenticate to npm registry
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
      - run:
          name: Update npm
          command: |
            npm install npm@latest --no-save &&
            sudo rm -rf /usr/local/lib/node_modules/npm &&
            sudo mv node_modules/npm /usr/local/lib/node_modules/npm
      - restore_cache:
          key: dependency-cache-{{ checksum "package.json" }}
      - run:
          name: Install dependencies
          command: npm install
      - save_cache:
          key: dependency-cache-{{ checksum "package.json" }}
          paths:
            - node_modules
      - run:
          name: Build
          command: npm run build
      - run:
          name: Test
          command: npm test
      - deploy:
          name: Publish
          command: |
            export TAG="v$(git log -1 --pretty=%B)"
            if echo $TAG | grep "^v[0-9]\+\.[0-9]\+\.[0-9]\+$"; then
              npm publish
            else
              echo "No tag pushed, skipping deploy"
            fi
