# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: build

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main, develop ]

permissions:
  contents: read

env:
  FORCE_COLOR: 2

jobs:
  build:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version:
          - { v: 22 }
          - { v: 24, isLatest: yes }

    steps:
    - uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
    - name: Use Node.js ${{ matrix.node-version.v }}
      uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
      with:
        node-version: ${{ matrix.node-version.v }}
    - run: npm clean-install
    - run: npm audit signatures
    - run: npm run all
    - run: npm run test

    - name: '[PR Preview] Upload dist folder'
      if: ${{github.event_name == 'pull_request' && matrix.node-version.isLatest}}
      uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
      with:
        name: dist
        path: |
          dist

    - name: '[PR Preview] Store PR metadata'
      if: ${{github.event_name == 'pull_request' && matrix.node-version.isLatest}}
      run: |
        touch ./pr_metadata.ini
        echo "pr=${{github.event.number}}" >> ./pr_metadata.ini
        echo "ref=${{github.sha}}" >> ./pr_metadata.ini

    - name: '[PR Preview] Upload PR metadata'
      if: ${{github.event_name == 'pull_request' && matrix.node-version.isLatest}}
      uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
      with:
        name: pr_metadata
        path: ./pr_metadata.ini
