name: Test and Publish the Demo

on: [push, pull_request]

permissions:
  contents: read
  id-token: write

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout (with submodules)
        uses: actions/checkout@v4
        with:
          submodules: recursive
          fetch-depth: 0

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 24
          cache: npm
          registry-url: https://registry.npmjs.org

      - name: Install wasm-pack
        uses: jetli/wasm-pack-action@v0.4.0
        with:
          version: "latest"

      - name: Build sourmash wasm pkg
        run: |
          cd sourmash/src/core
          wasm-pack build --target web --release
          ls -la pkg

      - name: Install deps
        run: npm ci

      - name: Lint
        run: npm run eslint

      - name: Build Component
        run: |
          npm run build
          ls -l dist

      - name: Publish to npm
        if: github.ref == 'refs/heads/main' && github.event_name == 'push'
        run: npm publish --provenance --access public

      - name: Deploy Site 🚀
        if: github.ref == 'refs/heads/main'
        uses: JamesIves/github-pages-deploy-action@3.7.1
        with:
          GITHUB_TOKEN: ${{ secrets.MGNIFY_SOURMASH_COMPONENT_NPM_PUSH_TOKEN }}
          BRANCH: gh-pages
          FOLDER: dist
          CLEAN: true