name: Release WASM

on:
  workflow_dispatch:
  push:
    branches:
      - main
    paths:
      - wasm/parser/package.json # Please only commit this file, so we don't need to wait for all the other CI jobs to finish.

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  check:
    name: Check version
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.version.outputs.version }}
      version_changed: ${{ steps.version.outputs.changed }}
    steps:
      - uses: taiki-e/checkout-action@v1

      - name: Check version changes
        uses: EndBug/version-check@v2
        id: version
        with:
          static-checking: localIsNew
          file-url: https://unpkg.com/@oxc-parser/wasm/package.json
          file-name: wasm/parser/package.json

      - name: Set version name
        if: steps.version.outputs.changed == 'true'
        run: |
          echo "Version change found! New version: ${{ steps.version.outputs.version }} (${{ steps.version.outputs.version_type }})"

  build:
    needs: check
    if: needs.check.outputs.version_changed == 'true'
    name: Release WASM
    runs-on: ubuntu-latest
    permissions:
      id-token: write # for `npm publish --provenance`
    steps:
      - uses: taiki-e/checkout-action@v1

      - name: Install Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 20
          registry-url: 'https://registry.npmjs.org'

      - name: Install Rust Toolchain
        uses: ./.github/actions/rustup
        with:
          shared-key: 'wasm'

      - name: Build
        working-directory: wasm/parser
        run: |
          rustup target add wasm32-unknown-unknown
          corepack enable
          pnpm install
          pnpm run build
          pnpm run test

      - name: Publish
        working-directory: npm/parser-wasm
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: npm publish --provenance --access public # --tag alpha
