name: Manual Trigger Workflow

on:
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    permissions:
      contents: write
      id-token: write

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

      - name: Setup Deno environment
        uses: denoland/setup-deno@v2

      - name: Run build
        run: deno run -A --no-lock ./.github/std-to-http.ts

      - name: Read the new version
        run: |
          echo "VERSION=$(cat package.json | jq -r '.version')" >> $GITHUB_ENV

      - uses: stefanzweifel/git-auto-commit-action@v5
        id: auto-commit
        with:
          commit_message: Update code
          tagging_message: ${{ env.VERSION }}

      - name: "Run if no changes have been detected"
        if: steps.auto-commit.outputs.changes_detected == 'false'
        run: |
          echo "No changes"
          exit 1

      - name: Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: ${{ env.VERSION }}

      - name: Setup node to publish to NPM
        uses: actions/setup-node@v4
        with:
          node-version: "20.x"
          registry-url: "https://registry.npmjs.org"

      - name: Publish to NPM
        run: npm publish --provenance --access public
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
