on:
  release:
    types: [created]

name: Publish to crate

jobs:
  publish:
    name: Publish to cargo
    runs-on: ubuntu-latest
    permissions:
      contents: write
      id-token: write  # Required for OIDC

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: 20
          cache: yarn
          registry-url: https://registry.npmjs.org/

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

      - uses: Swatinem/rust-cache@v2

      - run: cargo test
      - run: cargo run --bin cr calcit/editor/compact.cirru --once
      - run: cargo run --bin cr calcit/test.cirru --once

      - name: "try js"
        run: >
          yarn
          && yarn tsc
          && cargo run --bin cr calcit/test.cirru --once 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@v1
        with:
          files: |
            target/release/cr
            target/release/bundle_calcit
            target/release/caps

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

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

      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{secrets.npm_token}}
