# generator: n-circle2-cli
# template: component

references:

  container_config_node: &container_config_node
    working_directory: ~/project/build
    docker:
      - image: cimg/node:<< parameters.node-version >>-browsers
    parameters:
      node-version:
        default: "18.16"
        type: string

  workspace_root: &workspace_root ~/project

  attach_workspace: &attach_workspace
    attach_workspace:
      at: *workspace_root

  npm_cache_keys: &npm_cache_keys
    keys:
      - cache-v6-{{ .Branch }}-{{ checksum "package.json" }}-
      - cache-v6-{{ .Branch }}-{{ checksum "package.json" }}

  cache_npm_cache: &cache_npm_cache
    save_cache:
      key: cache-v6-{{ .Branch }}-{{ checksum "package.json" }}
      paths:
        - ./node_modules/

  restore_npm_cache: &restore_npm_cache
    restore_cache:
      <<: *npm_cache_keys

  filters_only_main: &filters_only_main
    branches:
      only: main

  filters_ignore_main: &filters_ignore_main
    branches:
      ignore: main

  filters_ignore_tags: &filters_ignore_tags
    tags:
      ignore: /.*/

  filters_version_tag: &filters_version_tag
    tags:
      only:
        - /^v?\d+\.\d+\.\d+(?:-beta\.\d+)?$/
    branches:
      ignore: /.*/

version: 2.1

orbs:
  node: circleci/node@4.6.0

jobs:

  build:
    <<: *container_config_node
    steps:
      - checkout
      - run:
          name: Checkout next-ci-shared-helpers
          command: git clone --depth 1
            git@github.com:Financial-Times/next-ci-shared-helpers.git --branch
            unpin-heroku .circleci/shared-helpers
      - *restore_npm_cache
      - node/install-npm:
          version: "^7"
      - run:
          name: Install project dependencies
          command: make install
      - run:
          name: shared-helper / generate-build-state-artifacts
          command: .circleci/shared-helpers/helper-generate-build-state-artifacts
          when: always
      - *cache_npm_cache
      - store_artifacts:
          path: build-state
          destination: build-state
      - persist_to_workspace:
          root: *workspace_root
          paths:
            - build

  test:
    <<: *container_config_node
    steps:
      - *attach_workspace
      - run:
          name: Run tests
          command: make test
          environment:
            JEST_JUNIT_OUTPUT: test-results/jest/results.xml
            MOCHA_FILE: test-results/mocha/results.xml
      - store_test_results:
          path: test-results
      - store_artifacts:
          path: test-results
          destination: test-results

  publish:
    <<: *container_config_node
    steps:
      - *attach_workspace
      - run:
          name: shared-helper / npm-store-auth-token
          command: .circleci/shared-helpers/helper-npm-store-auth-token
      - run:
          name: shared-helper / npm-version-and-publish-public
          command: .circleci/shared-helpers/helper-npm-version-and-publish-public

workflows:

  version: 2

  build-test:
    when:
      not:
        equal:
          - scheduled_pipeline
          - << pipeline.trigger_source >>
    jobs:
      - build:
          filters:
            <<: *filters_ignore_tags
          name: build-v<< matrix.node-version >>
          matrix:
            parameters:
              node-version: [ "16.20", "18.16" ]
      - test:
          requires:
            - build-v<< matrix.node-version >>
          name: test-v<< matrix.node-version >>
          matrix:
            parameters:
              node-version: [ "16.20", "18.16" ]

  build-test-publish:
    when:
      not:
        equal:
          - scheduled_pipeline
          - << pipeline.trigger_source >>
    jobs:
      - build:
          filters:
            <<: *filters_version_tag
          name: build-v<< matrix.node-version >>
          matrix:
            parameters:
              node-version: [ "16.20", "18.16" ]
      - test:
          filters:
            <<: *filters_version_tag
          requires:
            - build-v<< matrix.node-version >>
          name: test-v<< matrix.node-version >>
          matrix:
            parameters:
              node-version: [ "16.20", "18.16" ]
      - publish:
          context: npm-publish-token
          filters:
            <<: *filters_version_tag
          requires:
            - test-v18.16

  nightly:
    when:
      and:
        - equal:
            - scheduled_pipeline
            - << pipeline.trigger_source >>
        - equal:
            - nightly
            - << pipeline.schedule.name >>
    jobs:
      - build:
          context: next-nightly-build
          name: build-v<< matrix.node-version >>
          matrix:
            parameters:
              node-version: [ "16.20", "18.16" ]
      - test:
          requires:
            - build-v<< matrix.node-version >>
          context: next-nightly-build
          name: test-v<< matrix.node-version >>
          matrix:
            parameters:
              node-version: [ "16.20", "18.16" ]

notify:
  webhooks:
    - url: https://ft-next-webhooks.herokuapp.com/circleci2-workflow
