name: Publish Chakra theme package to NPM

on:
  workflow_dispatch:
    inputs:
      version:
        description: Package version
        type: string
        required: true
  workflow_call:
    inputs:
      version:
        description: Package version
        type: string
        required: true

jobs:
  publish:
    runs-on: ubuntu-latest
    name: Publish package to NPM registry
    permissions:
      id-token: write

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

      # Also it will setup .npmrc file to publish to npm
      - name: Setup node
        uses: actions/setup-node@v4
        with:
          node-version: '22.11.0'
          registry-url: 'https://registry.npmjs.org'

      - name: Update package version
        run: |
          cd ./theme
          npm version ${{ inputs.version }}
    
      - name: Build the package
        run: |
          yarn
          cd ./theme
          yarn
          yarn build
    
      - name: Publish to NPM registry
        run: |
          cd ./theme
          npm publish --provenance --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
