name: Build

on:
  push:
  pull_request:
  workflow_dispatch:
  schedule:
    - cron: '0 22 * * *'

jobs:
  jest:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        node-version: [ 14, 16, 18, 20 ]
    steps:
      - uses: actions/checkout@v2

      - name: Setup Node.js
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}

      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - uses: actions/cache@v1
        id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/package.json') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: Replace package dependencies
        run: npx replace-pkg-dep package.json main

      - name: Yarn install
        run: yarn install

      - name: Run
        run: yarn miaoxing-jest

      - name: Coveralls
        if: startsWith(hashFiles('coverage/lcov.info'), '5df6e0e27613') == false
        continue-on-error: true
        env:
          COVERALLS_FLAG_NAME: node-${{ matrix.node-version }}
        uses: coverallsapp/github-action@master
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          parallel: true

  coveralls-finish:
    needs: [ jest ]
    runs-on: ubuntu-latest
    continue-on-error: true
    steps:
      - name: Coveralls Finished
        uses: coverallsapp/github-action@master
        with:
          github-token: ${{ secrets.github_token }}
          parallel-finished: true

  eslint:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [ 20 ]

    steps:
      - uses: actions/checkout@v2

      - name: Setup Node.js
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}

      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - uses: actions/cache@v1
        id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/package.json') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: Replace package dependencies
        run: npx replace-pkg-dep package.json main

      - name: Yarn install
        run: yarn install

      - name: Run
        run: yarn miaoxing-eslint

  stylelint:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [ 20 ]
    steps:
      - uses: actions/checkout@v2

      - name: Setup Node.js
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}

      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"

      - uses: actions/cache@v1
        id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('**/package.json') }}
          restore-keys: |
            ${{ runner.os }}-yarn-

      - name: Replace package dependencies
        run: npx replace-pkg-dep package.json main

      - name: Yarn install
        run: yarn install

      - name: Run
        run: yarn miaoxing-stylelint
