# Doc : https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
# Must run before publish jobs (npmjs.yml & npm-github.yml)
name: Node.js CI

on:
  push:
    branches:
      - master
    tags:
      - 'v*.*.*'
  pull_request:
    branches:
      - 'master'

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [12.x, 14.x, 16.x, 18.x, 20.x]

    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
      # Using npm ci installs the versions in the package-lock.json or npm-shrinkwrap.json file and prevents updates to the lock file. Using npm ci is generally faster than running npm install.
      - name: Install dependencies
        run: npm ci
      # Will do nothing on this package (no 'build' script)
      # - run: npm run build --if-present
      # Test running it
      - run: npm test
      # Test publishing to npmjs
      - run: npm publish --provenance --access public --dry-run
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
      # Test publishing to github
      - run: npm publish --provenance --access public --dry-run
        env:
          NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
