Skip to content

Instantly share code, notes, and snippets.

@carlhannes
Last active March 7, 2024 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlhannes/0a8c827f826d330cfc07b36365e5158e to your computer and use it in GitHub Desktop.
Save carlhannes/0a8c827f826d330cfc07b36365e5158e to your computer and use it in GitHub Desktop.
setup-eslint-airbnb.sh
#!/bin/bash
#
# wget -q -O - https://gist.github.com/carlhannes/0a8c827f826d330cfc07b36365e5158e/raw/a4d4ad961d4ba9637d7c7f92c1537e6437a289d9/setup-eslint-airbnb.sh | bash
#
# Step 0: Determine ESLint package and configuration based on project dependencies
PACKAGE_JSON="package.json"
ESLINT_PACKAGE="eslint-config-airbnb-base"
EXTENDS_CONFIG='["airbnb-base"]'
TYPESCRIPT=false
# Check if package.json exists
if [ -f "$PACKAGE_JSON" ]; then
# Check for "react", "react-dom", or "next" in package.json dependencies
if grep -q '"react"' $PACKAGE_JSON || grep -q '"react-dom"' $PACKAGE_JSON || grep -q '"next"' $PACKAGE_JSON; then
ESLINT_PACKAGE="eslint-config-airbnb"
EXTENDS_CONFIG='["airbnb", "airbnb/hooks"]'
fi
fi
# Step 1: Install Airbnb ESLint configuration
npx install-peerdeps --dev $ESLINT_PACKAGE
# Step 2: Check if TypeScript is used (tsconfig.json exists)
if [ -f "tsconfig.json" ]; then
TYPESCRIPT=true
npm install eslint-config-airbnb-typescript \
@typescript-eslint/eslint-plugin@^7.0.0 \
@typescript-eslint/parser@^7.0.0 \
--legacy-peer-deps \
--save-dev
if [ "$ESLINT_PACKAGE" = "eslint-config-airbnb" ]; then
EXTENDS_CONFIG='["airbnb", "airbnb/hooks", "airbnb-typescript"]'
else
EXTENDS_CONFIG='["airbnb-base", "airbnb-typescript/base"]'
fi
fi
# Step 3: Configure ESLint (.eslintrc.json)
if [ -f ".eslintrc.json" ]; then
# Check and modify "extends" based on TypeScript support
EXTENDS_EXISTS=$(jq 'has("extends")' .eslintrc.json)
if [ "$EXTENDS_EXISTS" = "true" ]; then
EXTENDS_IS_ARRAY=$(jq '.extends | type == "array"' .eslintrc.json)
if [ "$EXTENDS_IS_ARRAY" = "true" ]; then
jq ".extends += $EXTENDS_CONFIG" .eslintrc.json > temp.json && mv temp.json .eslintrc.json
else
jq ".extends = [.extends] + $EXTENDS_CONFIG" .eslintrc.json > temp.json && mv temp.json .eslintrc.json
fi
else
jq ". + {extends: $EXTENDS_CONFIG}" .eslintrc.json > temp.json && mv temp.json .eslintrc.json
fi
# Additional TypeScript configuration
if [ "$TYPESCRIPT" = true ]; then
jq '. + {parserOptions: {project: "./tsconfig.json"}}' .eslintrc.json > temp.json && mv temp.json .eslintrc.json
fi
else
# Create .eslintrc.json with necessary configuration, including TypeScript if needed
if [ "$TYPESCRIPT" = true ]; then
echo "{ \"extends\": $EXTENDS_CONFIG, \"parserOptions\": {\"project\": \"./tsconfig.json\"} }" > .eslintrc.json
else
echo "{ \"extends\": $EXTENDS_CONFIG }" > .eslintrc.json
fi
fi
# Step 4: Update VSCode settings (Same as before)
VSCODE_SETTINGS=".vscode/settings.json"
mkdir -p .vscode
if [ -f "$VSCODE_SETTINGS" ]; then
jq '. + {"editor.codeActionsOnSave": {"source.fixAll": false, "source.fixAll.eslint": true}}' $VSCODE_SETTINGS > temp.json && mv temp.json $VSCODE_SETTINGS
else
echo '{"editor.codeActionsOnSave": {"source.fixAll": false, "source.fixAll.eslint": true}}' > $VSCODE_SETTINGS
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment