version: 2.1
orbs:
  node: circleci/node@2.1.0
defaults: &defaults
  working_directory: ~/project
  executor:
    name: node/default
    tag: 'lts'
jobs:
  build-and-test:
    <<: *defaults
    steps:
      - checkout
      - node/install-packages #node orb cmd caches
      - run: npm run test
      - persist_to_workspace:
          root: ~/project
          paths: .
  deploy-npm:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/project
      - add_ssh_keys:
          fingerprints:
            - "39:ed:d1:e0:6c:8b:00:03:a0:0d:0b:2a:5c:0e:fd:ab"
      - run: ssh-keyscan -H github.com >> ~/.ssh/known_hosts
      - run:
          name: "configure release version"
          command: |
            VPKG=$($(npm bin)/json -f package.json version)
            echo "export VERSION=${VPKG/%?/}$(date +%Y%m%d%H%M%S)" >> $BASH_ENV
      - run:
          name: "publish"
          command: |
            npm --no-git-tag-version version $VERSION
            npm set //registry.npmjs.org/:_authToken=$NPM_TOKEN
            npm publish
      - run:
          name: "tag release"
          command: |
            git tag $VERSION
            git push $CIRCLE_REPOSITORY_URL $VERSION
  pull-translations:
    <<: *defaults
    steps:
      - checkout
      - node/install-packages
      - run:
          name: "pull editor and www translations"
          command: |
            npm run pull:editor
            npm run pull:desktop
            npm run pull:linkDesktop
            npm run pull:www
            npm run test
      - persist_to_workspace:
          root: ~/project
          paths: .
  commit-translations:
    <<: *defaults
    steps:
      - attach_workspace:
          at: ~/project
      - add_ssh_keys:
          fingerprints:
            - "39:ed:d1:e0:6c:8b:00:03:a0:0d:0b:2a:5c:0e:fd:ab"
      - run: ssh-keyscan -H github.com >> ~/.ssh/known_hosts
      - run:
          name: "commit translation updates"
          command: |
            git config --global user.email $(git log --pretty=format:"%ae" -n1)
            git config --global user.name $(git log --pretty=format:"%an" -n1)
            git pull
            git add .
            git commit -m "pull new editor translations from Transifex"
            git push $CIRCLE_REPOSITORY_URL HEAD:master
workflows:
  version: 2
  build-test-and-deploy:
    jobs:
      - build-and-test
      - deploy-npm:
          requires:
            - build-and-test
          filters:
            branches:
              only: master
  manual-pull:
    when:
      equal: [ tx-pull-manual, << pipeline.git.branch >> ]
    jobs:
      - pull-translations
      - commit-translations:
          requires:
            - pull-translations
          filters:
            branches:
              only: tx-pull-manual
  weekly-tx-pull:
      triggers:
        - schedule: # weekly on Wednesday at 3am
            cron: "0 3 * * 3"
            filters:
              branches:
                only: master
      jobs:
        - pull-translations
        - commit-translations:
            requires:
              - pull-translations # don't commit if there were errors
