---
name: Packages - Publish NPM packages

on:
  push:
    branches: [main]
    paths:
      - packages/tsconfig/**
      - packages/eslint-config/**
      - packages/prettier-config/**
      - packages/docusaurus-config/**
      - packages/esbuild-plugin-live-reload/**
  workflow_dispatch:

permissions:
  # Required for NPM OIDC trusted publisher
  id-token: write
  contents: read

jobs:
  publish:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        package:
          # The order of the `*config` packages should not be changed, as they depend on each other.
          - packages/tsconfig
          - packages/eslint-config
          - packages/prettier-config
          - packages/docusaurus-config
          - packages/logger-js
          - packages/esbuild-plugin-live-reload
    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v5
        with:
          fetch-depth: 2
      - uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v5
        with:
          node-version-file: ${{ matrix.package }}/package.json
          registry-url: "https://registry.npmjs.org"
      - name: Get changed files
        id: changed-files
        uses: tj-actions/changed-files@22103cc46bda19c2b464ffe86db46df6922fd323 # 24d32ffd492484c1d75e0c0b894501ddb9d30d62
        with:
          files: |
            ${{ matrix.package }}/package.json
      - name: Install Dependencies
        run: npm ci
      - name: Publish package
        if: steps.changed-files.outputs.any_changed == 'true'
        working-directory: ${{ matrix.package }}
        run: |
          npm ci
          npm run build
          npm publish
