name: Build and release
on:
  release:
    types: [published]

permissions:
  contents: read
  id-token: write

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: "22"
          registry-url: "https://registry.npmjs.org"
          scope: "@aikidosec"
          cache: 'npm'

      - name: Update npm
        run: npm install -g npm@11.10.0 # pinned to 11.10.0 to work around https://github.com/nodejs/node/issues/62425

      - name: Install dependencies
        run: npm ci

      - name: Build the library
        run: npm run build

      - name: Get the version
        id: get_version
        run: echo "tag=${GITHUB_REF/refs\/tags\//}" >> $GITHUB_OUTPUT

      - name: Set the version
        run: npm --no-git-tag-version version ${{ steps.get_version.outputs.tag }}

      - name: Publish to NPM
        run: |
          echo "Publishing version ${{ steps.get_version.outputs.tag }} to NPM"
          npm publish --access public
