name: Node.js Publish

on:
  release:
    types: [created]
  workflow_dispatch:
    inputs:
      release_tag_to_publish:
        description: 'The name of the published release tag (e.g. v1.0.0)'
        required: true
        type: string
jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [14.x, 16.x, 18.x, 20.x, 22.x, 24.x]
    steps:
    - uses: actions/checkout@v2
      with:
        ref: ${{ github.event_name == 'release' && github.event.release.tag_name || github.event.inputs.release_tag_to_publish }}
    - name: Set up Node.js version ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}

    - run: npm install
    - run: npm run lint
    - run: npm run test

  publish-npm:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          ref: ${{ github.event_name == 'release' && github.event.release.tag_name || github.event.inputs.release_tag_to_publish }}
      - uses: actions/setup-node@v2
        with:
          node-version: '22.x'
          registry-url: https://registry.npmjs.org/
      - run: npm ci
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}    