name: Node.js CI

permissions:
  contents: write
  issues: write
  pull-requests: write

on:
  push:
    branches:
      - master
  pull_request:
    branches: [master]

jobs:
  build:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    strategy:
      fail-fast: false
      matrix:
        node-version: [18.x, 20.x, 22.x]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: Setup Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'
      - name: Install dependencies
        run: npm ci --ignore-scripts
      - name: Build
        run: npm run build --if-present
      - name: Run tests with coverage
        run: npm run coverage
        env:
          CI: true
      - name: Coveralls
        uses: coverallsapp/github-action@v2
        with:
          flag-name: node-${{ matrix.node-version }}
          parallel: true

  coveralls-finish:
    needs: build
    runs-on: ubuntu-latest
    steps:
      - name: Coveralls Finished
        uses: coverallsapp/github-action@v2
        with:
          parallel-finished: true

  release:
    needs: build
    permissions:
      contents: write
      id-token: write
    if: github.event_name == 'push' && github.ref == 'refs/heads/master'
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          persist-credentials: false
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "lts/*"
          registry-url: 'https://registry.npmjs.org'
      - name: Install dependencies
        run: npm ci --ignore-scripts
      - name: Semantic Release
        run: npx semantic-release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
          NPM_CONFIG_PROVENANCE: true
      - run: npm ci
      - name: Npm Publish
        run: npm publish
