---
version: 2
jobs:
  fetch:
    working_directory: ~/cp-translations
    docker:
      - image: circleci/node:8
    steps:
      - checkout
      - run:
          name: Install Crowdin CLI
          command: |
            sudo apt-get update
            sudo apt-get install ruby-dev rubygems
            sudo gem install mime-types -v 2.6.2
            sudo gem install rest-client -v 1.8
            sudo gem install crowdin-cli -v 0.5.5
      - run:
          name: Install deps
          command: yarn
      - run:
          name: Download translations
          command: |
            for lang in it pt-PT de nl ro mt sl sk fr uk es-ES es-AR el pl et bg tr ja hi lt zh-CN id si-LK ur-PK zh-TW fil ar; do crowdin-cli download -l $lang; done
          no_output_timeout: 20m
      - save_cache:
          key: translations-{{ .Branch }}-{{ .Revision }}-{{ .Environment.CIRCLE_WORKFLOW_ID }}
          paths:
            - email-templates
            - strings
  test:
    working_directory: ~/cp-translations
    docker:
      - image: circleci/node:8
    steps:
      - checkout
      - restore_cache:
          key: translations-{{ .Branch }}-{{ .Revision }}-{{ .Environment.CIRCLE_WORKFLOW_ID }}
      - run:
          name: Install deps
          command: yarn
      - run:
          name: Test translations
          command: yarn test
  lex:
    working_directory: ~/cp-translations
    docker:
      - image: circleci/node:8
    steps:
      - checkout
      - restore_cache:
          key: translations-{{ .Branch }}-{{ .Revision }}-{{ .Environment.CIRCLE_WORKFLOW_ID }}
      - run:
          name: Install deps
          command: yarn
      - run:
          name: Test translations
          command: yarn lex 
  publish:
    working_directory: ~/cp-translations
    docker:
      - image: circleci/node:8
    steps:
      - add-ssh-keys:
          fingerprints:
            - "7c:58:32:cd:93:00:d6:85:ff:34:84:1b:a1:e7:ee:39"
      - checkout
      - restore_cache:
          key: translations-{{ .Branch }}-{{ .Revision }}-{{ .Environment.CIRCLE_WORKFLOW_ID }}
      - run:
          name: Setup .npmrc
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
      - run:
          name: Install Crowdin CLI
          command: |
            sudo apt-get update
            sudo apt-get install ruby-dev rubygems
            sudo gem install mime-types -v 2.6.2
            sudo gem install rest-client -v 1.8
            sudo gem install crowdin-cli -v 0.5.5
      - run:
          name: Upload sources
          command: crowdin-cli upload sources
      - run:
          name: Publish to NPM
          command: |
            git config --global user.email "webteam@coderdojo.com"
            git config --global user.name "CoderDojo Foundation"
            npm version patch -m "[skip ci] Update to %s"
            git push
            npm publish
workflows:
  version: 2
  test-and-deploy:
    jobs:
      - fetch
      - test:
          requires:
            - fetch
      - lex:
          requires:
            - fetch
      - publish:
          requires:
            - test
            - lex 
          filters:
            branches:
              only:
                - master
