name: Publish to npmjs.com

on:
  release:
    types: [published]
  workflow_dispatch:
    inputs:
      release_tag_name:
        description: "Release tag name (e.g. v0.3.12)"
        type: string
        required: true

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - name: Configure credentials
        run: |
          npm set "//registry.npmjs.org/:_authToken" ${{ secrets.NPM_REGISTRY_TOKEN }}
      - name: Publish to npmjs.com
        run: |
          RELEASE_TAG_NAME=${{ inputs.release_tag_name }}
          if [ -z "$RELEASE_TAG_NAME" ]; then
            RELEASE_TAG_NAME=${{ github.event.release.tag_name }}
          fi
          NPM_PACKAGE_NAME=${{ vars.NPM_PACKAGE_NAME }}
          if [ -z "$NPM_PACKAGE_NAME" ]; then
            NPM_PACKAGE_NAME=${GITHUB_REPOSITORY#*/}
          fi
          npm publish "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/releases/download/$RELEASE_TAG_NAME/$NPM_PACKAGE_NAME-npm-package.tar.gz"
