# 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: CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
  # Run on new tags

jobs:
  build:
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        node-version: [20.x]
        
    runs-on: ${{ matrix.os }}

    steps:
    - uses: actions/checkout@v4
      with:
        submodules: true
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v4
      with:
        node-version: ${{ matrix.node-version }}
    - name: Install Linux dependencies
      if: matrix.os == 'ubuntu-latest'
      run: sudo apt-get update && sudo apt-get install -y zlib1g-dev
    - run: npm install
    - run: npm run build --if-present
    - run: npm test
    - run: npm run postci
      id: postci
    # - name: Display structure of downloaded files
    #   run: ls -R
    - name: Upload artifact
      uses: actions/upload-artifact@v4
      with:
        name: dist-${{ steps.postci.outputs.PLATFORM_STRING }}
        path: |
          build/Release/*
        if-no-files-found: error
  debug:
    runs-on: ubuntu-latest
    steps:
    - name: Debug Action
      uses: hmarr/debug-action@v1.0.0
  release:
    needs: build
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && github.ref == 'refs/heads/master'
    permissions:
      contents: write
      id-token: write
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 24.x
      - name: Install npm with Trusted Publishing support
        run: npm install --global npm@11

      - name: Download artifacts
        uses: actions/download-artifact@v4
        with:
          path: helpers
      - run: npm install
        env: 
          SKIP_BUILD: true
      - id: publish
        uses: JS-DevTools/npm-publish@v4
        with:
          strategy: upgrade
      - name: Create Release
        if: steps.publish.outputs.type != ''
        run: gh release create "$RELEASE_VERSION" --target "$GITHUB_SHA" --title "$RELEASE_VERSION" --notes "$RELEASE_VERSION"
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          RELEASE_VERSION: ${{ steps.publish.outputs.version }}
      - name: Display structure of downloaded files
        run: ls -R
