name: build

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master
  release:
    types:
      - released

jobs:

  test:
    strategy:
      matrix:
        os:
          - ubuntu-18.04
          - macos-latest
          - windows-latest
        node:
          - 10
          - 12
          - 14
          - 16
    name: Testing Node ${{ matrix.node }} on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node }}
      - run: npm install --ignore-scripts
      - run: npm run build-debug
      - run: npm test

  publish:
    if: ${{ github.event_name == 'release' }}
    name: Publishing to NPM
    runs-on: ubuntu-18.04
    needs: test
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: 16
          registry-url: https://registry.npmjs.org
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

  prebuild:
    strategy:
      matrix:
        os:
          - ubuntu-18.04
          - macos-latest
          - windows-latest
    name: Prebuild on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    needs: publish
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: 16
      - run: npm install --ignore-scripts
      - run: npx --no-install prebuild -r node -t 10.20.0 -t 12.0.0 -t 14.0.0 -t 16.0.0 --include-regex 'better_sqlite3.node$' -u ${{ secrets.GITHUB_TOKEN }}
      - run: npx --no-install prebuild -r electron -t 10.0.0 -t 11.0.0 -t 12.0.0 -t 13.0.0 --include-regex 'better_sqlite3.node$' -u ${{ secrets.GITHUB_TOKEN }}