version: 2

aliases:
  - &docker
    - image: circleci/openjdk:8-jdk-node-browsers

  - &environment
    TZ: /usr/share/zoneinfo/America/Los_Angeles

  - &restore_yarn_cache
    restore_cache:
      name: Restore node_modules cache
      keys:
        - v1-node-{{ arch }}-{{ .Branch }}-{{ checksum "yarn.lock" }}
        - v1-node-{{ arch }}-{{ .Branch }}-
        - v1-node-{{ arch }}-
  - &run_yarn
    run:
      name: Install Packages
      command: yarn --frozen-lockfile

  - &attach_workspace
    at: build

jobs:
  setup:
    docker: *docker
    environment: *environment

    steps:
      - checkout
      - run:
          name: Nodejs Version
          command: node --version
      - *restore_yarn_cache
      - *run_yarn
      - save_cache:
          name: Save node_modules cache
          key: v1-node-{{ arch }}-{{ .Branch }}-{{ checksum "yarn.lock" }}
          paths:
            - ~/.cache/yarn

  lint:
    docker: *docker
    environment: *environment

    steps:
      - checkout
      - *restore_yarn_cache
      - *run_yarn
      - run: node ./scripts/prettier/index
      - run: node ./scripts/tasks/eslint
      - run: ./scripts/circleci/check_license.sh
      - run: ./scripts/circleci/check_modules.sh
      - run: ./scripts/circleci/test_print_warnings.sh

  flow:
    docker: *docker
    environment: *environment

    steps:
      - checkout
      - *restore_yarn_cache
      - *run_yarn
      - run: node ./scripts/tasks/flow-ci

  test_source:
    docker: *docker
    environment: *environment

    steps:
      - checkout
      - *restore_yarn_cache
      - *run_yarn
      - run: yarn test --maxWorkers=2

  test_source_persistent:
    docker: *docker
    environment: *environment

    steps:
      - checkout
      - *restore_yarn_cache
      - *run_yarn
      - run: yarn test-persistent --maxWorkers=2

  test_source_prod:
    docker: *docker
    environment: *environment

    steps:
      - checkout
      - *restore_yarn_cache
      - *run_yarn
      - run: yarn test-prod --maxWorkers=2

  build:
    docker: *docker
    environment: *environment
    parallelism: 20
    steps:
      - checkout
      - *restore_yarn_cache
      - *run_yarn
      - run: ./scripts/circleci/add_build_info_json.sh
      - run: ./scripts/circleci/update_package_versions.sh
      - run: yarn build
      - persist_to_workspace:
          root: build
          paths:
            - facebook-www
            - node_modules
            - react-native
            - dist
            - sizes/*.json

  process_artifacts:
    docker: *docker
    environment: *environment
    steps:
      - checkout
      - attach_workspace: *attach_workspace
      - *restore_yarn_cache
      - *run_yarn
      - run: node ./scripts/rollup/consolidateBundleSizes.js
      - run: node ./scripts/tasks/danger
      - run: ./scripts/circleci/upload_build.sh
      - run: ./scripts/circleci/pack_and_store_artifact.sh
      - store_artifacts:
          path: ./node_modules.tgz
      - store_artifacts:
          path: ./build.tgz
      - store_artifacts:
          path: ./build/bundle-sizes.json
      - store_artifacts:
          # TODO: Update release script to use local file instead of pulling
          # from artifacts.
          path: ./scripts/error-codes/codes.json

  lint_build:
    docker: *docker
    environment: *environment
    steps:
      - checkout
      - attach_workspace: *attach_workspace
      - *restore_yarn_cache
      - *run_yarn
      - run: yarn lint-build
      - run: scripts/circleci/check_minified_errors.sh

  test_build:
    docker: *docker
    environment: *environment
    steps:
      - checkout
      - attach_workspace: *attach_workspace
      - *restore_yarn_cache
      - *run_yarn
      - run: yarn test-build --maxWorkers=2

  test_build_devtools:
    docker: *docker
    environment: *environment
    steps:
      - checkout
      - attach_workspace: *attach_workspace
      - *restore_yarn_cache
      - *run_yarn
      - run: yarn test-build-devtools --maxWorkers=2

  test_dom_fixtures:
    docker: *docker
    environment: *environment
    steps:
      - checkout
      - attach_workspace: *attach_workspace
      - *restore_yarn_cache
      - run:
          name: Run DOM fixture tests
          command: |
            cd fixtures/dom
            yarn --frozen-lockfile
            yarn prestart
            yarn test --maxWorkers=2

  test_fuzz:
    docker: *docker
    environment: *environment
    steps:
      - checkout
      - *restore_yarn_cache
      - *run_yarn
      - run:
          name: Run fuzz tests
          command: |
            FUZZ_TEST_SEED=$RANDOM yarn test fuzz --maxWorkers=2
            FUZZ_TEST_SEED=$RANDOM yarn test-prod fuzz --maxWorkers=2

  test_build_prod:
    docker: *docker
    environment: *environment
    steps:
      - checkout
      - attach_workspace: *attach_workspace
      - *restore_yarn_cache
      - *run_yarn
      - run: yarn test-build-prod --maxWorkers=2

workflows:
  version: 2
  commit:
    jobs:
      - setup
      - lint:
          requires:
            - setup
      - flow:
          requires:
            - setup
      - test_source:
          requires:
            - setup
      - test_source_prod:
          requires:
            - setup
      - test_source_persistent:
          requires:
            - setup
      - build:
          requires:
            - setup
      - process_artifacts:
          requires:
            - build
      - lint_build:
          requires:
            - build
      - test_build:
          requires:
            - build
      - test_build_prod:
          requires:
            - build
      - test_build_devtools:
          requires:
            - build
      - test_dom_fixtures:
          requires:
            - build
  hourly:
    triggers:
      - schedule:
          cron: "0 * * * *"
          filters:
            branches:
              only:
                - master
    jobs:
      - setup
      - test_fuzz:
          requires:
            - setup
