version: 2.1

orbs:
  javascript: wealthsimple/javascript@dev:master
  sonarqube: wealthsimple/sonarqube@dev:master

jobs:
  test:
    executor: javascript/node
    steps:
      - checkout
      - javascript/attach_code_workspace
      - javascript/yarn_install_dependencies
      - javascript/run_unit_tests
      - sonarqube/scan
  convert_to_typescript:
    executor: javascript/node
    steps:
      - checkout
      - javascript/attach_code_workspace
      - run:
          name: Convert to TypeScript
          command: ./scripts/tsconvert.bash
  dual_publish:
    executor: javascript/node
    steps:
      - checkout
      - javascript/attach_code_workspace
      - javascript/yarn_install_dependencies
      - run:
          name: Dual Release (FlowType, TypeScript)
          command: |
            # Get the last release tag.
            export LAST_VERSION=$(git describe --tags --abbrev=0 --match "v*" | cut -c 2-)
            echo "Current version is $LAST_VERSION"
            # Run semantic release to compute the next version and 
            # do the FlowType release if necessary.
            # This package is on npm, not Nexus.
            export NPM_TOKEN="${PUBLIC_NPM_TOKEN}"
            npm set //registry.npmjs.org/:_authToken "$NPM_TOKEN"
            GITHUB_TOKEN="${DEVTOOLS_GITHUB_API_TOKEN}" yarn run semantic-release
      
            # Get the new release tag
            export NEXT_VERSION=$(git describe --tags --abbrev=0 --match "v*" | cut -c 2-)
            echo "Next version is $NEXT_VERSION"
      
            # Do the typescript release if necessary.
            echo "Checking if TypeScript release is needed"
            echo "Last version: $LAST_VERSION"
            echo "Next version: $NEXT_VERSION"
            if [ "$LAST_VERSION" != "$NEXT_VERSION" ]; then
              echo "Versions are different - making a typescript release."
              # Convert package to typescript.
              ./scripts/tsconvert.bash
              # Publish TS version under a different package name until we have the full
              # stack sorted out.  
              jq '.name = "@wealthsimple/patchwork-ts"' package.json \
                | jq ".version = \"$NEXT_VERSION\"" \
                > package.json.tmp
              mv package.json.tmp package.json
              npm publish
            else
              echo "Versions are the same. Skipping TypeScript release"
            fi

workflows:
  version: 2.1
  approval:
    jobs:
      - ready:
          context: wealthsimple
          filters:
            branches:
              ignore: master
          type: approval
      - javascript/chromatic-test:
          name: 'Chromatic: check for visual diffs'
          context: wealthsimple
          requires:
            - ready
          filters:
            branches:
              ignore:
                - master

  build-and-deploy:
    jobs:
      - javascript/security:
          context: wealthsimple
      - test:
          context: wealthsimple
      - javascript/static_checks:
          context: wealthsimple
      - javascript/accessibility_scan:
          name: "A11y Scan: Invest Theme"
          context: wealthsimple
          patchwork_theme: "wealthsimple"
      - javascript/accessibility_scan:
          name: "A11y Scan: Trade Theme"
          context: wealthsimple
          patchwork_theme: "trade"
      - javascript/accessibility_scan:
          name: "A11y Scan: Crypto Theme"
          context: wealthsimple
          patchwork_theme: "crypto"
      - javascript/accessibility_scan:
          name: "A11y Scan: Cash Theme"
          context: wealthsimple
          patchwork_theme: "cash"
      - javascript/accessibility_scan:
          name: "A11y Scan: Atlas Theme"
          context: wealthsimple
          patchwork_theme: "atlas"
      - javascript/accessibility_scan:
          name: "A11y Scan: Tax Theme"
          context: wealthsimple
          patchwork_theme: "tax"
      - javascript/deploy-docs:
          context: wealthsimple
          filters:
            branches:
              only: master
      - javascript/chromatic-accept-all:
          context: wealthsimple
          filters:
            branches:
              only:
                - master
      - dual_publish:
          name: Semantic Release (FlowType and TypeScript)
          context: wealthsimple
          requires:
            - javascript/static_checks
            - javascript/security
            - test
            - convert_to_typescript
          filters:
            branches:
              only: master
      - convert_to_typescript:
          context: wealthsimple