name: Lint

on:
  push:
    branches:
      - main
  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Lint changelog file
        uses: avto-dev/markdown-lint@v1
        with:
          # Commenting due to
          # https://github.com/avto-dev/markdown-lint/blob/aab9be9823fcd706f6da3e6ad6c64874c23fbd4c/lint/rules/changelog.js#L51-L71
          # TODO: adhere to, or overwrite above rule and uncomment rules
          # rules: "/lint/rules/changelog.js"
          config: "/lint/config/changelog.yml"
          args: "./CHANGELOG.md"

      - name: Lint markdown files
        uses: avto-dev/markdown-lint@v1
        with:
          args: "./**/*.md -i ./CHANGELOG.md"

      - name: restore lock files
        uses: actions/cache@master # must use unreleased master to cache multiple paths
        id: cache
        with:
          # must be done before bootstrap to not include node_modules files in the cache paths
          path: |
            package-lock.json
            packages/*/package-lock.json
            benchmark/*/package-lock.json
            backwards-compatability/*/package-lock.json
            metapackages/*/package-lock.json
            packages/*/package-lock.json
            integration-tests/*/package-lock.json
          key: ${{ runner.os }}-lint-${{ hashFiles('**/package.json') }}

      # On a cache hit, use ci to speed up the install process
      - name: Bootstrap (cache hit)
        if: steps.cache.outputs.cache-hit == 'true'
        run: |
          npm ci --ignore-scripts
          npx lerna bootstrap --ignore-scripts

      # On a cache miss, fall back to a regular install
      - name: Bootstrap (cache miss)
        if: steps.cache.outputs.cache-hit != 'true'
        run: |
          npm install --ignore-scripts
          npx lerna bootstrap --no-ci --ignore-scripts -- --only=dev

      - name: Lint
        run: |
          npm run lint
          npm run lint:examples
