name: Build

on:
  pull_request:
  merge_group:
  workflow_dispatch:
  push:
    branches:
      - develop
    tags:
      - '*'

jobs:
  build:
    name: 'Build and Test on Node ${{ matrix.node_version }} and ${{ matrix.os }}'
    runs-on: '${{ matrix.os }}'
    strategy:
      matrix:
        node_version:
          - 22
          - 24
        os:
          - ubuntu-latest
          - macOS-latest
          - windows-latest
    steps:
      - uses: actions/checkout@v7
      - name: 'Use Node.js ${{ matrix.node_version }}'
        uses: actions/setup-node@v7
        with:
          node-version: '${{ matrix.node_version }}'
      - name: npm build
        run: |
          npm run clean
          npm run build
          npm run test

  end2end:
    name: 'End2End Test on Node ${{ matrix.node_version }} and ${{ matrix.os }}'
    runs-on: '${{ matrix.os }}'
    strategy:
      fail-fast: true
      matrix:
        node_version:
          # Can not run in parallel due to Anker login limiting
          # renovate: datasource=docker depName=node
          - 24
        os:
          - ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - name: 'Use Node.js ${{ matrix.node_version }}'
        uses: actions/setup-node@v7
        with:
          node-version: '${{ matrix.node_version }}'
      - name: npm test
        run: |
          npm run clean
          npm run e2e
        env:
          ANKER_USERNAME: ${{ vars.ANKER_E2E_USER }}
          ANKER_PASSWORD: ${{ secrets.ANKER_E2E_PASS }}
          ANKER_COUNTRY: ${{ vars.ANKER_E2E_COUNTRY }}
          DEVICE_SN: ${{ secrets.ANKER_E2E_DEVICE_SN }}
          GH_BRANCH: ${{ github.head_ref || github.ref_name }}

  build-results:
    name: Build results
    if: ${{ always() }}
    runs-on: ubuntu-latest
    needs:
      - build
      - end2end
    steps:
      - run: exit 1
        # see https://stackoverflow.com/a/67532120/4907315
        if: >-
          ${{
              contains(needs.*.result, 'failure')
              || contains(needs.*.result, 'cancelled')
              || contains(needs.*.result, 'skipped')
              }}
  package:
    name: Package Application
    runs-on: ubuntu-latest
    needs:
      - build
      - end2end
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
        with:
          # renovate: datasource=docker depName=node
          node-version: '24'
      - name: 'Build Package'
        run: |
          npm run clean
          npm run build

      - name: Write version vars
        run: |
          BUILD_DATE=`date -u +"%Y-%m-%dT%H:%M:%SZ"`
          BRANCH=${GITHUB_REF_NAME#v}
          APP_VERSION=$(cat package.json | grep version| head -1 | awk -F: '{ print $2 }' | sed 's/[",]//g')
          echo Version: $APP_VERSION
          echo "VERSION=$APP_VERSION" >> $GITHUB_ENV
          echo "APP_VERSION=$APP_VERSION" >> $GITHUB_ENV
          echo "BUILD_DATE=$BUILD_DATE" >> $GITHUB_ENV

      - name: Push To NPM Registry
        id: push-to-npm-tagged
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
        run: |
          echo "//registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN" >> ~/.npmrc
          npm publish
        if: github.ref_type == 'tag' || github.tag != ''
