version: 2

defaults: &defaults
  working_directory: ~/repo

jobs:
    build_14:
        docker:
            -
                image: 'circleci/node:14'
        steps:
            - checkout
            -
                restore_cache:
                    key: 'v1-dependency-cache-{{ checksum "package.json" }}'
            -
                run:
                    name: install-npm
                    command: yarn
            -
                save_cache:
                    key: 'v1-dependency-cache-{{ checksum "package.json" }}'
                    paths:
                        - ./node_modules
            -
                run:
                    name: test
                    command: |
                        npm run test
                        mkdir -p test-results/junit
                        bin/cucumber-json-merge test/fixtures/1.json test/fixtures/2.json
                        cp test-results.xml test-results/junit/
            -
                store_artifacts:
                    path: test-results
                    destination: tests
            -
                store_artifacts:
                    path: merged-test-results.json
                    destination: tests
            -
                store_artifacts:
                    path: coverage
                    destination: coverage
            -
                store_test_results:
                    path: test-results
    build_12:
        docker:
            -
                image: 'circleci/node:12'
        steps:
            - checkout
            -
                restore_cache:
                    key: 'v1-dependency-cache-{{ checksum "package.json" }}'
            -
                run:
                    name: install-npm
                    command: yarn
            -
                save_cache:
                    key: 'v1-dependency-cache-{{ checksum "package.json" }}'
                    paths:
                        - ./node_modules
            -
                run:
                    name: test
                    command: |
                        npm run test
                        mkdir -p test-results/junit
                        bin/cucumber-json-merge test/fixtures/1.json test/fixtures/2.json
                        cp test-results.xml test-results/junit/
            -
                store_artifacts:
                    path: test-results
                    destination: tests
            -
                store_artifacts:
                    path: merged-test-results.json
                    destination: tests
            -
                store_artifacts:
                    path: coverage
                    destination: coverage
            -
                store_test_results:
                    path: test-results
    build_10:
        docker:
            -
                image: 'circleci/node:10'
        steps:
            - checkout
            -
                restore_cache:
                    key: 'v1-dependency-cache-{{ checksum "package.json" }}'
            -
                run:
                    name: install-npm
                    command: yarn
            -
                save_cache:
                    key: 'v1-dependency-cache-{{ checksum "package.json" }}'
                    paths:
                        - ./node_modules
            -
                run:
                    name: test
                    command: |
                        npm run test
                        mkdir -p test-results/junit
                        bin/cucumber-json-merge test/fixtures/1.json test/fixtures/2.json
                        cp test-results.xml test-results/junit/
            -
                store_artifacts:
                    path: test-results
                    destination: tests
            -
                store_artifacts:
                    path: merged-test-results.json
                    destination: tests
            -
                store_artifacts:
                    path: coverage
                    destination: coverage
            -
                store_test_results:
                    path: test-results
    package:
        <<: *defaults
        docker:
            -
                image: 'circleci/node:8'
        steps:
            - checkout
            -
                restore_cache:
                    key: 'v1-dependency-cache-{{ checksum "package.json" }}'
            -
                run:
                    name: install-npm
                    command: yarn
            -
                save_cache:
                    key: 'v1-dependency-cache-{{ checksum "package.json" }}'
                    paths:
                        - ./node_modules
            -
                run:
                    name: Install GHR
                    command: >
                        wget
                        https://github.com/tcnksm/ghr/releases/download/v0.5.4/ghr_v0.5.4_linux_amd64.zip

                        unzip ghr_v0.5.4_linux_amd64.zip
            -
                run:
                    name: Package
                    command: >
                        RELEASE_TAG=v$(cat package.json | jq --raw-output
                        .version)

                        echo $RELEASE_TAG

                        yarn run pkg

                        ./ghr -u bitcoder -r cucumber-json-merge --replace $RELEASE_TAG
                        pkg
            -
                persist_to_workspace:
                    root: ~/repo
                    paths: .
            -
                store_artifacts:
                    path: pkg
                    destination: packages

    deploy:
      <<: *defaults
      docker:
          -
              image: 'circleci/node:10'
      steps:
        - attach_workspace:
            at: ~/repo
        - run:
            name: Authenticate with registry
            command: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > ~/repo/.npmrc
        - run:
            name: Publish package
            command: npm publish

workflows:
    version: 2
    test_deploy:
        jobs:
            - build_14
            - build_12
            - build_10
            -
                package:
                    type: approval
                    filters:
                        branches:
                            only: master
                    requires:
                        - build_14
                        - build_12
                        - build_10
            -
                deploy:
                    filters:
                        branches:
                            only: master
                    requires:
                        - package
