name: NPM package

on:
  push:
    branches:
      - main
    tags:
      - "**"
  pull_request:
    branches:
      - "**"

concurrency:
  group: ${{ github.event_name == 'pull_request' && format('{0}-{1}', github.workflow_ref, github.event.pull_request.number) || github.run_id }}
  cancel-in-progress: true

permissions:
  contents: read
  actions: write
  id-token: write

jobs:
  build_npm:
    name: Build npm
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Set up Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Install wasm-pack
        run: cargo install wasm-pack --locked

      - name: WASM build and pack
        run: |
          cd wasm
          wasm-pack build
          wasm-pack pack

      - name: Upload NPM pkg artifacts
        uses: actions/upload-artifact@v4
        with:
          name: npm-pkg
          path: ./wasm/pkg/*-*.tgz

      # To update to the latest npm without permission issues, we need to not use the system npm
      - uses: actions/setup-node@v6
        with:
          node-version: lts/*

      - name: Ensure we have a new enough npm version for trusted publishing
        if: startsWith(github.event.ref, 'refs/tags')
        run: |
          npm install -g npm
          npm --version

      - name: Publish (npmjs.com)
        if: startsWith(github.event.ref, 'refs/tags')
        uses: JS-DevTools/npm-publish@v4
        with:
          provenance: true
          package: wasm/pkg/package.json
