name: CI

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the main branch
on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
  release:
    types:
      - published

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2

      - name: Setup Node.js environment
        uses: actions/setup-node@v2.1.2
        with:
          node-version: 14.x

      - name: Setup PNPM
        run: npm install -g pnpm@^6.0.0

      - name: Install
        run: pnpm install

      - name: Lint
        run: pnpm lint:check

      - name: Format
        run: pnpm format:check

      - name: API Check
        run: pnpm api:check

      - name: build
        run: pnpm build --filter ./packages
        env:
          NODE_ENV: production

      - name: Release
        if: ${{ github.event_name == 'release' }}
        run: pnpm release:package
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

      - name: Build docs
        if: ${{ github.event_name == 'release' }}
        run: pnpm run docs

      - name: Release Docs
        if: ${{ github.event_name == 'release' }}
        run: |
          git remote set-url origin https://git:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git
          pnpm release:docs -- -- -u "github-actions-bot <support+actions@github.com>"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
