name: "CI"
on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

permissions:
  contents: read

jobs:
  test_deno:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        deno-version: ["v2.x"]
    steps:
      - name: Harden Runner
        uses: step-security/harden-runner@a90bcbc6539c36a85cdfeb73f7e2f433735f215b # v2.15.0
        with:
          egress-policy: audit

      - name: Git Checkout Deno Module
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - name: Use Deno Version ${{ matrix.deno-version }}
        uses: denolib/setup-deno@4df079f84f6c4ef488b04806df9af59e51d09bfb # v2.3.0
        with:
          deno-version: ${{ matrix.deno-version }}
      - name: Test Module
        run: deno task build

  test_node:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [windows-latest, ubuntu-latest, macos-latest]
        node-version: [22.x, 24.x]
    steps:
      - name: Harden Runner
        uses: step-security/harden-runner@a90bcbc6539c36a85cdfeb73f7e2f433735f215b # v2.15.0
        with:
          egress-policy: audit
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm ci
      - run: npm run lint
      - run: npm run build

  release:
    if: github.ref == 'refs/heads/master' && needs.test_node.result == 'success' && needs.test_deno.result == 'success'
    needs: [test_node, test_deno]
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pages: write
      id-token: write # Required for OIDC
    steps:
      - name: Harden Runner
        uses: step-security/harden-runner@a90bcbc6539c36a85cdfeb73f7e2f433735f215b # v2.15.0
        with:
          egress-policy: audit
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          fetch-depth: 0
          token: ${{ secrets.GH_TOKEN }}
      - uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
        with:
          node-version: "24.x"
          registry-url: 'https://registry.npmjs.org'
      - uses: denolib/setup-deno@4df079f84f6c4ef488b04806df9af59e51d09bfb # v2.3.0
        with:
          deno-version: "2.x"
      - name: Get current package version
        id: package_version
        uses: martinbeentjes/npm-get-version-action@3cf273023a0dda27efcd3164bdfb51908dd46a5b # v1.3.1
      - run: deno task build
      - run: npm ci
      - run: npm run build
      - run: git config --global user.name "JamesCullum (Pseudonym)"
      - run: git config --global user.email "https://mailhide.io/e/Wno7k"
      - name: Check if tag already exists
        id: tag_exists
        uses: mukunku/tag-exists-action@5c39604fe8aef7e65acb6fbcf96ec580f7680313 # v1.7.0
        with:
          tag: "${{ steps.package_version.outputs.current-version}}"
        env:
          GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
      - name: Git Auto Commit Deno Artifacts
        if: steps.tag_exists.outputs.exists == 'false'
        uses: stefanzweifel/git-auto-commit-action@04702edda442b2e678b25b537cec683a1493fcb9 # v7.1.0
        with:
          commit_message: "[skip ci] v${{ steps.package_version.outputs.current-version}}: Build artifacts for Deno"
          tagging_message: "${{ steps.package_version.outputs.current-version}}"
          file_pattern: dist/*.js
      - name: Update npm
        run: npm install -g npm@latest
      - run: npm run docs
      - run: npm run publish-docs
        env:
          GH_TOKEN: ${{ secrets.GH_TOKEN }}
      - run: npm publish
