name: TypeScript / WASM CI

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

jobs:
  wasm-build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
          target: wasm32-unknown-unknown
      - name: Install wasm-pack
        run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
      - name: Build WASM (bundler)
        run: wasm-pack build --target bundler --features wasm --out-dir pkg
      - name: Build WASM (nodejs)
        run: wasm-pack build --target nodejs --features wasm --out-dir pkg-nodejs
      - name: Upload WASM artifacts
        uses: actions/upload-artifact@v4
        with:
          name: wasm-pkg
          path: pkg/

  publish-npm:
    needs: wasm-build
    if: github.ref == 'refs/heads/main' && github.event_name == 'push'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: "20"
          registry-url: "https://registry.npmjs.org"
      - uses: actions/download-artifact@v4
        with:
          name: wasm-pkg
          path: pkg/
      - name: Publish to npm
        run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
