Last active
May 4, 2017 13:14
-
-
Save aslakhellesoy/a175b2fef0d5861a74598f184040ab36 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install a bunch of tools: | |
# | |
# eslint - looks for logical mistakes in the code (unused params etc), *not* formatting | |
# eslint-config-prettier - overrides default eslint rules to disable formatting rules | |
# eslint-plugin-prettier - configures `eslint --fix` to format code with prettier | |
# prettier - prettifies code | |
# lint-staged - only runs `eslint --fix` on files that have been staged with `git add` | |
# husky - automatically installs a precommit hook that runs lint-staged | |
# | |
yarn add --dev eslint eslint-config-prettier eslint-plugin-prettier prettier lint-staged husky | |
# Remove old eslint configuration | |
rm -f .eslintrc* | |
# Write new eslint configuration for modern JavaScrpt | |
cat > .eslintrc <<- ESLINTRC | |
{ | |
"parserOptions": { | |
"ecmaVersion": 8 | |
}, | |
"plugins": [ | |
"prettier" | |
], | |
"rules": { | |
"prettier/prettier": [ | |
"error", {"trailingComma": "es5", "singleQuote": true, "semi": false} | |
] | |
}, | |
"env": { | |
"node": true, | |
"es6": true | |
}, | |
"extends": [ | |
"eslint:recommended", | |
"prettier" | |
] | |
} | |
ESLINTRC | |
# Tell lint-staged to prettify all *.js files | |
# Change the value of the "gitDir" property if package.json is below the git repo root. | |
cat > .lintstagedrc << LINTSTAGEDRC | |
{ | |
"gitDir": ".", | |
"linters": { | |
"*.js": ["eslint --fix", "git add"] | |
} | |
} | |
LINTSTAGEDRC | |
# Declare the precommit hook. Gets installed on yarn|npm install. | |
cat package.json | jq '.scripts.precommit = "lint-staged"' > package.json.tmp && mv package.json.tmp package.json | |
# Reformat all the files once before committing | |
./node_modules/.bin/eslint --fix **/*.js | |
# Fix your linting errors. | |
# You may have to add a header to your mocha tests: | |
# /* eslint-env mocha */ | |
# | |
# You're done. From now on, only modified files get reformatted and linted. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment