name: Release package
on:
  workflow_dispatch:
    inputs:
      release-version:
        description: 'New version number (e.g. 1.0.0)'
        required: true
jobs:
  release:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Using Node.js 18.x
        uses: actions/setup-node@v2
        with:
          node-version: '18.x'
          registry-url: 'https://registry.npmjs.org'

      - name: Install Node.js dependencies
        run: npm ci

      - name: Git configuration
        run: |
          git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --global user.name "GitHub Actions"

      - name: Set version
        run: |
          npm --no-git-tag-version version ${{ env.NEW_VERSION }}
        env:
          NEW_VERSION: ${{ github.event.inputs.release-version }}

      - name: Upload prebuild artifacts
        uses: actions/upload-artifact@v2
        with:
          name: prebuilds
          path: prebuilds/

      - name: Commit changed files and create tag
        run: |
          git add "package.json"
          git commit -m "Version ${{ env.NEW_VERSION }}"
          git tag ${{ env.NEW_VERSION }}
        env:
          NEW_VERSION: ${{ github.event.inputs.release-version }}

      - name: Publish
        run: npm publish --access public --verbose
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPMJS_ACCESS_TOKEN }}

      - name: Push changes to repository
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          git push origin && git push --tags
