version: 2
jobs:
  deploy-production:
    working_directory: ~/app

    docker:
    - image: cimg/node:20.9

    steps:
    - checkout

    - run:
        name: Install node dependencies
        command: |
          npm install

    - run:
        name: Build static files
        command: |
          npm run build

    - run: sudo npm install -g mocha
    - run: sudo npm install -g mocha-junit-reporter

    - run: mkdir reports

    # Run mocha
    - run:
        name: npm test
        command: mocha tests/*.js --reporter mocha-junit-reporter --reporter-options mochaFile=reports/mocha/test-results.xml
        when: always
        # Upload results

    - store_test_results:
        path: reports

    - store_artifacts:
        path: ./reports/mocha/test-results.xml

  deploy-feature:
    working_directory: ~/app

    docker:
    - image: cimg/node:20.9

    steps:
    - checkout

    - run:
        name: Install node dependencies
        command: |
          npm install

    - run:
        name: Build static files
        command: |
          npm run build

    - run: sudo npm install -g mocha
    - run: sudo npm install -g mocha-junit-reporter

    - run: mkdir reports

    # Run mocha
    - run:
        name: npm test
        command: mocha tests/*.js --reporter mocha-junit-reporter --reporter-options mochaFile=reports/mocha/test-results.xml
        when: always
        # Upload results

    - store_test_results:
        path: reports

    - store_artifacts:
        path: ./reports/mocha/test-results.xml


workflows:
  version: 2
  build:
    jobs:
    - deploy-production:
        filters:
          branches:
            only:
            - master
    - deploy-feature:
        filters:
          branches:
            ignore:
            - master
