name: Release (TypeScript)

on:
  push:
    tags:
      - 'ts-v*.*.*'

permissions:
  contents: write
  id-token: write

defaults:
  run:
    working-directory: gwm-ts

jobs:
  release:
    name: Release & Publish
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 10.4.1

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
          registry-url: 'https://registry.npmjs.org'
          cache: 'pnpm'
          cache-dependency-path: 'gwm-ts/pnpm-lock.yaml'

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Run tests
        run: pnpm test:run

      - name: Run linting
        run: pnpm lint

      - name: Build project
        run: pnpm build

      - name: Extract version from tag
        id: version
        run: echo "version=${GITHUB_REF#refs/tags/ts-v}" >> $GITHUB_OUTPUT

      - name: Create GitHub Release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: gwm (TypeScript) v${{ steps.version.outputs.version }}
          draft: false
          prerelease: false
          body: |
            ## Changes in TypeScript version ${{ steps.version.outputs.version }}

            > **Note**: This is the deprecated TypeScript version. Consider using the Rust version instead: `cargo install gwm`

            See the full changelog at: https://github.com/${{ github.repository }}/compare/ts-v${{ steps.version.outputs.version }}...main

      - name: Publish to npm
        run: pnpm publish --no-git-checks
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
          NPM_CONFIG_PROVENANCE: true
