#!/usr/bin/env bash

# Project override: use module .eslintrc.json (supports JSX) and lint module paths only.
## Description: Run eslint inside the web container (module config + JS/JSX)
## Usage: eslint [paths...]
## Example: "ddev eslint" or "ddev eslint js/react"

set -eu -o pipefail

if "$DDEV_DOCROOT/core/node_modules/.bin/eslint" --version >/dev/null ; then
  test -e .prettierrc.json || ln -sf $DDEV_DOCROOT/core/.prettierrc.json .
  test -e .prettierignore || echo '*.yml' > .prettierignore
  cd "$DDEV_DOCROOT/$DRUPAL_PROJECTS_PATH/${DDEV_SITENAME//-/_}" || exit
  if [ -f .eslintrc.json ]; then
    "$DDEV_COMPOSER_ROOT/$DDEV_DOCROOT/core/node_modules/.bin/eslint" \
      -c .eslintrc.json \
      --no-error-on-unmatched-pattern \
      --resolve-plugins-relative-to="$DDEV_COMPOSER_ROOT/$DDEV_DOCROOT/core" \
      --ext=.js,.jsx,.yml \
      "${@:-js/}"
  else
    "$DDEV_COMPOSER_ROOT/$DDEV_DOCROOT/core/node_modules/.bin/eslint" \
      --config="$DDEV_COMPOSER_ROOT/$DDEV_DOCROOT/core/.eslintrc.passing.json" \
      --no-error-on-unmatched-pattern \
      --ignore-pattern="*.es6.js" \
      --resolve-plugins-relative-to="$DDEV_COMPOSER_ROOT/$DDEV_DOCROOT/core" \
      --ext=.js,.yml \
      . "$@"
  fi
else
  echo "eslint is not available. You may need to 'ddev exec \"cd $DDEV_DOCROOT/core && yarn install\"'"
  exit 1
fi
