name: Build

on:
    push:
        tags:
            - "v*"
    workflow_dispatch:

jobs:
    build:
        strategy:
            matrix:
                include:
                    - os: linux
                      cpu: x64
                      target: x86_64-unknown-linux-musl
                      runner: ubuntu-latest

                    - os: linux
                      cpu: arm64
                      target: aarch64-unknown-linux-musl
                      runner: ubuntu-24.04-arm

        runs-on: ${{ matrix.runner }}
        steps:
            - uses: actions/checkout@v6

            - name: Install Rust
              uses: dtolnay/rust-toolchain@stable
              with:
                  targets: ${{ matrix.target }}

            - name: Install Nushell
              uses: hustcer/setup-nu@v3

            - name: Build
              run:
                  # prettier-ignore
                  nu scripts.nu build --os ${{ matrix.os }} --cpu ${{ matrix.cpu }} --target ${{ matrix.target }}

            - name: Upload artifact
              uses: actions/upload-artifact@v6
              with:
                  name: ${{ matrix.os }}-${{ matrix.cpu }}
                  path: dist/${{ matrix.os }}-${{ matrix.cpu }}

    publish:
        needs: build
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v6

            - uses: actions/download-artifact@v6
              with:
                  path: dist

            - uses: actions/setup-node@v4
              with:
                  node-version: 22
                  registry-url: https://registry.npmjs.org

            - name: Publish packages
              run: |
                  for dir in dist/*/; do
                    npm publish "$dir" --access public
                  done
                  npm publish
              env:
                  NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
