on:
  release:
    types: [created]

name: Publish to crate

permissions:
  contents: write
  id-token: write  # Required for OIDC

jobs:
  publish:
    name: Publish to cargo
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

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

      - name: Enable Corepack
        run: |
          corepack enable
          corepack prepare yarn@4.12.0 --activate
          yarn --version

      - uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: stable

      - uses: Swatinem/rust-cache@v2

      - name: Set sparse registry protocol
        run: echo "CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse" >> $GITHUB_ENV

      - run: cargo test
      - run: cargo run --bin cr calcit/test.cirru

      - name: "try js"
        run: >
          yarn
          && yarn tsc
          && cargo run --bin cr calcit/test.cirru js
          && ln -s ../../ node_modules/@calcit/procs
          && cp -v scripts/main.mjs js-out/
          && node js-out/main.mjs

      - run: cargo build --release

      - name: Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            target/release/cr
            target/release/cr-wasm
            target/release/bundle_calcit
            target/release/caps

      - uses: katyo/publish-crates@v1
        with:
          registry-token: ${{ secrets.CARGO_REGISTRY_TOKEN }}
          args: --no-verify

      - name: "generate procs.mjs"
        run: yarn && yarn tsc

      # - run: npm publish --provenance --access public --tag next
      - run: npm publish --provenance --access public
