name: CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  lint:
    name: 'Lint'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '18'
      - uses: actions/cache@v3
        with:
          path: ~/.npm
          key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: ${{ runner.OS }}-node-
      - run: npm ci
      - run: npm run lint
      - run: npm run check-git-clean

  type-check:
    name: 'Type Check'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '18'
      - uses: actions/cache@v3
        with:
          path: ~/.npm
          key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: ${{ runner.OS }}-node-
      - uses: actions/cache@v3
        with:
          path: ~/.dts
          key: ${{ runner.OS }}-dts-${{ hashFiles('**/package-lock.json') }}
          restore-keys: ${{ runner.OS }}-dts-
      - run: npm ci
      - run: npm run type-check
      - run: npm run check-git-clean

  unit-test:
    name: 'Build & Unit Test'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: actions/setup-node@v2
        with:
          node-version: '18'
      - uses: actions/cache@v3
        with:
          path: ~/.npm
          key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: ${{ runner.OS }}-node-
      - run: npm ci
      - run: npm run build
      - run: npm run unit-test
      - run: npm run check-git-clean

  website:
    name: 'Build Website'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - uses: actions/setup-node@v2
        with:
          node-version: '18'
      - uses: actions/cache@v3
        with:
          path: ~/.npm
          key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: ${{ runner.OS }}-node-
      - run: npm ci
      - run: NODE_OPTIONS=--openssl-legacy-provider npm run website:build
      - run: npm run check-git-clean

  publish:
    name: 'Publish'
    needs: [lint, type-check, unit-test, website]
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - uses: actions/setup-node@v2
        with:
          node-version: '18'
      - uses: actions/cache@v3
        with:
          path: ~/.npm
          key: ${{ runner.OS }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: ${{ runner.OS }}-node-
      - run: npm ci
      - run: npm run build
      - run: NODE_OPTIONS=--openssl-legacy-provider npm run website:build
      - name: Push NPM Branch
        if: github.ref == 'refs/heads/main'
        uses: peaceiris/actions-gh-pages@v3
        with:
          enable_jekyll: true
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./npm
          publish_branch: npm
          user_name: 'github-actions[bot]'
          user_email: 'github-actions[bot]@users.noreply.github.com'
      - name: Publish Docs
        if: github.ref == 'refs/heads/main'
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./website/out
          cname: immutable-js.com
          user_name: 'github-actions[bot]'
          user_email: 'github-actions[bot]@users.noreply.github.com'
