name: LLama for nodejs

on:
  push:
    branches: ["main"]
  pull_request:
    branches: ["main"]

env:
  DEBUG: 'napi:*'
  MACOSX_DEPLOYMENT_TARGET: '10.13'

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        os: [
          'ubuntu-20.04',
          'macos-latest',
          'windows-latest'
        ]
        include:
          - target: aarch64-apple-darwin
            os: macos-latest
          - target: x86_64-unknown-linux-musl
            os: ubuntu-20.04
          - target: x86_64-pc-windows-msvc
            os: windows-latest
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v3

      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
          target: ${{ matrix.target }}

      - uses: Swatinem/rust-cache@v2

      - uses: actions/setup-node@v3
        with:
          node-version: '18'

      - uses: pnpm/action-setup@v2
        name: Setup pnpm
        with:
          version: 7
          run_install: false

      - name: Get pnpm store directory
        id: pnpm-cache
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT

      - uses: actions/cache@v3
        name: Setup pnpm cache
        with:
          path: ${{ steps.pnpm-cache.outputs.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

      - run: sudo apt install -y musl-tools && sudo ln -s /bin/g++ /bin/musl-g++
        if: matrix.os == 'ubuntu-20.04'
      - run: git submodule update --init --recursive
      - run: pnpm install
      - run: cargo clippy --all-targets
      - run: pnpm build:llm-rs
      - run: pnpm build:llama-cpp
      - run: pnpm build:rwkv-cpp
      - run: pnpm build
      - run: pnpm type-check
      - uses: actions/upload-artifact@v3
        with:
          name: binding-${{ matrix.os }}
          path: packages/**/@llama-node/*.node
          if-no-files-found: error

  # test-host:
  #   strategy:
  #     fail-fast: false
  #     matrix:
  #       settings:
  #         - target: x86_64-apple-darwin
  #           host: macos-latest
  #         - target: x86_64-pc-windows-msvc
  #           host: windows-latest
  #         - target: i686-pc-windows-msvc
  #           host: windows-latest
  #   runs-on: ${{ matrix.settings.host }}
  #   needs:
  #     - build
  #   steps:
  #     - uses: actions/checkout@v3
  #     - uses: actions/setup-node@v3
  #       with:
  #         node-version: ${{ matrix.node }}
  #         cache: 'yarn'
  #     - name: Setup node x86
  #       if: matrix.settings.target == 'i686-pc-windows-msvc'
  #       run: yarn config set supportedArchitectures.cpu "ia32"
  #       shell: bash
  #     - name: Install dependencies
  #       run: yarn install
  #     - name: Setup node x86
  #       uses: actions/setup-node@v3
  #       if: matrix.settings.target == 'i686-pc-windows-msvc'
  #       with:
  #         node-version: 16
  #         check-latest: true
  #         cache: yarn
  #         architecture: x86
  #     - name: Download bindings
  #       uses: actions/download-artifact@v3
  #       with:
  #         name: bindings-${{ matrix.settings.target }}
  #         path: 01-pure-rust/
  #     - name: Run tests
  #       run: yarn workspace @napi-cross-build/01-pure-rust test
  # test-docker:
  #   strategy:
  #     fail-fast: false
  #     matrix:
  #       settings:
  #         - target: x86_64-unknown-linux-gnu
  #           docker: node:18-slim
  #           setup: 'exit 0'
  #         - target: aarch64-unknown-linux-gnu
  #           docker: ghcr.io/napi-rs/napi-rs/nodejs:aarch64-18
  #           setup: 'exit 0'
  #         - target: x86_64-unknown-linux-musl
  #           docker: node:18-alpine
  #           setup: 'exit 0'
  #         - target: aarch64-unknown-linux-musl
  #           docker: multiarch/alpine:aarch64-latest-stable
  #           setup: apk add --no-cache nodejs npm yarn
  #         - target: armv7-unknown-linux-gnueabihf
  #           docker: ghcr.io/napi-rs/napi-rs/nodejs:armhf-18
  #           setup: 'exit 0'
  #   runs-on: ubuntu-20.04
  #   needs:
  #     - build
  #   steps:
  #     - run: docker run --rm --privileged multiarch/qemu-user-static:register --reset
  #     - uses: actions/checkout@v3
  #     - uses: actions/setup-node@v3
  #       with:
  #         node-version: ${{ matrix.node }}
  #         cache: 'yarn'
  #     - name: Install dependencies
  #       run: yarn install
  #     - name: Download bindings
  #       uses: actions/download-artifact@v3
  #       with:
  #         name: bindings-${{ matrix.settings.target }}
  #         path: 01-pure-rust/
  #     - name: Run tests
  #       uses: addnab/docker-run-action@v3
  #       with:
  #         image: ${{ matrix.settings.docker }}
  #         options: -v ${{ github.workspace }}:/build -w /build
  #         run: |
  #           set -e
  #           ${{ matrix.settings.setup }}
  #           yarn workspace @napi-cross-build/01-pure-rust test
