name: Publish version to NPM

on:
  push:
    tags:
      - "*.*.*"

jobs:
  build:
    name: Create Release
    runs-on: ubuntu-latest
    steps:
      - name: Get version from tag
        id: lasttag
        run: echo ::set-output name=version::${GITHUB_REF#refs/tags/v}

      - name: Checkout Repo
        uses: actions/checkout@v4

      - name: Parse Changelog Entry
        id: changelog
        uses: mindsers/changelog-reader-action@v2
        with:
          version: v${{ steps.lasttag.outputs.version }}

      - uses: actions/setup-node@v4
        with:
          node-version: "20"

      - name: Cache NPM modules
        id: npm-cache
        uses: actions/cache@v4
        with:
          path: node_modules
          key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-npm-

      - name: Install dependencies
        run: npm ci

      - name: Build Component
        run: npm run package

      - name: Publish Package
        uses: JS-DevTools/npm-publish@v3
        with:
          token: ${{ secrets.NPM_TOKEN }}
          package: ./package.json

      - name: Create GitHub Release
        id: create_release
        uses: ncipollo/release-action@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          body: ${{ steps.changelog.outputs.changes }}
