name: Publish air-beautify-wasm snapshot

on:
  workflow_call:
    inputs:
      ref:
        description: "git ref to checkout to"
        type: string
        default: "master"
    outputs:
      version:
        description: "@fluencelabs/air-beautify-wasm version"
        value: ${{ jobs.snapshot.outputs.version }}

env:
  FORCE_COLOR: true
  RUSTFLAGS: "-D warnings"
  RUSTC_WRAPPER: "sccache"

jobs:
  snapshot:
    name: "Publish air-beautify-wasm"
    runs-on: ubuntu-latest
    timeout-minutes: 60

    outputs:
      version: "${{ steps.snapshot.outputs.version }}"

    permissions:
      contents: read
      id-token: write

    steps:
      - name: Checkout aquavm
        uses: actions/checkout@v4
        with:
          repository: fluencelabs/aquavm
          ref: ${{ inputs.ref }}

      - name: Import secrets
        uses: hashicorp/vault-action@v2.7.3
        with:
          url: https://vault.fluence.dev
          path: jwt/github
          role: ci
          method: jwt
          jwtGithubAudience: "https://github.com/fluencelabs"
          jwtTtl: 300
          exportToken: false
          secrets: |
            kv/npm-registry/basicauth/ci token | NODE_AUTH_TOKEN

      - name: Setup Rust toolchain
        uses: dsherret/rust-toolchain-file@v1

      - name: Setup cache
        uses: Swatinem/rust-cache@v2
        with:
          cache-directories: ~/.cache/sccache
          shared-key: aquavm
          save-if: false

      - name: Setup sccache
        uses: mozilla-actions/sccache-action@v0.0.3

      - name: Setup wasm-pack
        uses: jetli/wasm-pack-action@v0.4.0

      - name: Setup node with self-hosted npm registry
        uses: actions/setup-node@v3
        with:
          node-version: "16"
          registry-url: "https://npm.fluence.dev"

      - name: Build air-beautify-wasm
        run: |
          rm -rf pack
          wasm-pack build --release
        working-directory: tools/wasm/air-beautify-wasm

      - name: Generate snapshot version
        id: version
        uses: fluencelabs/github-actions/generate-snapshot-id@main

      - name: Publish snapshot
        id: snapshot
        uses: fluencelabs/github-actions/npm-publish-snapshot@main
        with:
          working-directory: tools/wasm/air-beautify-wasm/pkg
          id: ${{ steps.version.outputs.id }}
