name: Publish @trycua/core to npm

on:
  push:
    branches: main

jobs:
  publish:
    permissions:
      id-token: write
      contents: read
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Use Node.js 24.x
        uses: actions/setup-node@v4
        with:
          node-version: "24.x"
          registry-url: "https://registry.npmjs.org"

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

      - name: Check if version changed
        id: check-version
        uses: EndBug/version-check@v2
        with:
          file-name: libs/typescript/core/package.json
          diff-search: true

      - name: Install dependencies
        if: steps.check-version.outputs.changed == 'true'
        working-directory: ./libs/typescript/core
        run: pnpm install --frozen-lockfile

      - name: Build package
        if: steps.check-version.outputs.changed == 'true'
        working-directory: ./libs/typescript/core
        run: pnpm run build --if-present

      - name: Publish to npm
        if: steps.check-version.outputs.changed == 'true'
        working-directory: ./libs/typescript/core
        run: pnpm publish --access public --no-git-checks
        env:
          NPM_CONFIG_PROVENANCE: true
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
