44
loading...
This website collects cookies to deliver better user experience
npx husky install
npx husky add .husky/pre-commit "npm test"
.husky
folder in the root directory with pre-commit
file in it. This file would have a single command npm test in it.#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npm test
npm install husky lint-staged eslint-plugin-prettier eslint-config-prettier --save-dev
npm install --save-dev --save-exact prettier
--save-dev
keeps these libraries in devDependencies
because they won't be used in production and are here for development only..estlintignore
and .prettierignore
files and place the following code
build
node_modules
.github
.eslintrc.js
with this code:
module.exports = {
env: {
browser: true,
es6: true,
},
extends: [
'eslint:recommended',
'plugin:react/recommended',
'plugin:react-hooks/recommended',
'plugin:prettier/recommended',
'plugin:jsx-a11y/strict',
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaFeatures: {
jsx: true,
},
ecmaVersion: 2018,
sourceType: 'module',
},
plugins: ['react', 'jsx-a11y', '@typescript-eslint'],
rules: {
'react-hooks/exhaustive-deps': 'error',
'no-var': 'error',
'brace-style': 'error',
'prefer-template': 'error',
radix: 'error',
'space-before-blocks': 'error',
'import/prefer-default-export': 'off',
},
overrides: [
{
files: [
'**/*.test.js',
'**/*.test.jsx',
'**/*.test.tsx',
'**/*.spec.js',
'**/*.spec.jsx',
'**/*.spec.tsx',
],
env: {
jest: true,
},
},
],
};
.prettierrc.js
and put the following code:
module.exports = {
printWidth: 100,
tabWidth: 2,
singleQuote: true,
semi: true,
trailingComma: 'all',
arrowParens: "always",
overrides: [
{
files: '*.{js,jsx,tsx,ts,scss,json,html}',
options: {
tabWidth: 4,
},
},
],
};
"prepare": "cd .. && husky install frontend/.husky"
npm prepare
command runs before we commit our code. What essentially we're doing here is that we are moving out of frontend directory and installing husky in the front-end. lint-staged
. Place the following code after scripts section:
"lint-staged": {
"*.{js,ts,tsx, jsx}": [
"eslint --quiet --fix"
],
"*.{json,md,html,js,jsx,ts,tsx}": [
"prettier --write"
]
},
Finally, we'll be adding a script that would invoke linting. Add this line in your scripts:
"lint-front": "lint-staged"
Running npm run lint-front
would trigger linting our application.
Let's just inform our husky to run npm run lint-front
before commit. Go to the husky folder in the project root and replace pre-commit file with this code:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
cd frontend
npm run lint-frontend