name: NPM packages

# on: [push]

on:
  push:
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'
      - 'v[0-9]+.[0-9]+.[0-9]+-alpha.[0-9]+'

# on:
#   push:
#     branches:
#       - production

jobs:
  build:

    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [ubuntu-22.04]

    steps:
      - name: Context
        env:
          GITHUB_CONTEXT: ${{ toJson(github) }}
        run: echo "$GITHUB_CONTEXT"
      - uses: actions/checkout@v2
        with:
          fetch-depth: 1
      - name: Use Node.js 14.x
        uses: actions/setup-node@v1
        with:
          node-version: 14.x

      - name: Configure NPM token
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          npm config set '//registry.npmjs.org/:_authToken' "${NPM_TOKEN}"

      - name: yarn install
        run: |
          yarn install
          
      - name: setCurrentVersion
        run: |
          yarn setCurrentVersion

      - name: Publish types
        working-directory: packages/types
        run: |
          npm publish

      - name: Publish tools
        working-directory: packages/tools
        run: |
          npm publish

      - name: Publish sqltree
        working-directory: packages/sqltree
        run: |
          npm publish
                    
      - name: Publish api
        working-directory: packages/api
        run: |
          npm publish

      - name: Publish datalib
        working-directory: packages/datalib
        run: |
          npm publish

      - name: Publish filterparser
        working-directory: packages/filterparser
        run: |
          npm publish

      - name: Publish web
        working-directory: packages/web
        run: |
          npm publish

      - name: Publish dbgate (obsolete)
        working-directory: packages/dbgate
        run: |
          npm publish

      - name: Publish dbgate-serve
        working-directory: packages/serve
        run: |
          npm publish

      - name: Publish dbmodel
        working-directory: packages/dbmodel
        run: |
          npm publish

      - name: Publish dbgate-plugin-csv
        working-directory: plugins/dbgate-plugin-csv
        run: |
          npm publish

      - name: Publish dbgate-plugin-xml
        working-directory: plugins/dbgate-plugin-xml
        run: |
          npm publish

      - name: Publish dbgate-plugin-excel
        working-directory: plugins/dbgate-plugin-excel
        run: |
          npm publish

      - name: Publish dbgate-plugin-mssql
        working-directory: plugins/dbgate-plugin-mssql
        run: |
          npm publish

      - name: Publish dbgate-plugin-mysql
        working-directory: plugins/dbgate-plugin-mysql
        run: |
          npm publish

      - name: Publish dbgate-plugin-mongo
        working-directory: plugins/dbgate-plugin-mongo
        run: |
          npm publish

      - name: Publish dbgate-plugin-postgres
        working-directory: plugins/dbgate-plugin-postgres
        run: |
          npm publish

      - name: Publish dbgate-plugin-sqlite
        working-directory: plugins/dbgate-plugin-sqlite
        run: |
          npm publish

      - name: Publish dbgate-plugin-redis
        working-directory: plugins/dbgate-plugin-redis
        run: |
          npm publish

      - name: Publish dbgate-plugin-oracle
        working-directory: plugins/dbgate-plugin-oracle
        run: |
          npm publish
