version: 2.1

defaults: &defaults
  working_directory: ~/repo
  docker:
    - image: cimg/node:20.10

commands:
  install-pnpm:
    description: "Install pnpm globally"
    steps:
      - run:
          name: Install pnpm
          command: |
            sudo npm install -g pnpm@latest
  generate_github_pr_for_docs:
    parameters:
      checkout:
        default: true
        description: Boolean value used for whether or not to checkout bitbucket repo as a first step. Default is true.
        type: boolean
    steps:
      - add_ssh_keys
      - when:
          condition: <<parameters.checkout>>
          steps:
            - checkout
      - run:
          name: Docs PR Generating
          command: |
            . ./.sentiance/doc-pr-gen.sh

jobs:
  test:
    <<: *defaults
    steps:
      - checkout
      - install-pnpm
      - run: pnpm install
      - run: pnpm -r lint
      - run: pnpm tsc
      - run: pnpm test
  android_test:
    docker:
      - image: cimg/android:2024.01.1-node
    environment:
      BUNDLE_PATH: vendor/bundle
      _JAVA_OPTIONS: -Xmx3g
    steps:
      - checkout
      - install-pnpm
      - run: java -version
      - run: pnpm install
      - run: pnpm androidTest
  deploy:
    <<: *defaults
    steps:
      - checkout
      - install-pnpm
      - run: pnpm install
      - attach_workspace:
          at: ~/repo
      - run:
          name: Authenticate with registry
          command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/repo/.npmrc
      - run:
          name: Sync package versions for all modules
          command: pnpm sync:package_versions
      - run:
          name: Compile Typescript code
          command: pnpm tsc
      - run:
          name: Tag and publish all subpackages
          command: |
            NPMTAG=$(pnpm fetch:npm_tag --silent)
            pnpm -r publish \
            --tag "$NPMTAG" \
            --access public \
            --no-git-checks \
            --report-summary
  publish_docs:
    environment:
      DOCKER_BUILDKIT: "1"
    machine:
      image: ubuntu-2004:202201-02
    steps:
      - generate_github_pr_for_docs

workflows:
  version: 2
  workflow:
    jobs:
      - test:
          filters:
            tags:
              only: /^v.*/
      - android_test:
          filters:
            tags:
              only: /^v.*/
      - deploy:
          requires: [test, android_test]
          filters:
            tags:
              only: /^v.*/
            branches:
              ignore: /.*/
