Sharable lint-staged config enforcing SNGULAR style guidelines.
npm install --save-dev @sngular/lint-staged-config
echo "module.exports = require('@sngular/lint-staged-config');" > lint-staged.config.cjs
echo "module.exports = require('@sngular/lint-staged-config/prettier.cjs');" > lint-staged.config.cjs
After that you can use husky to add a pre-commit hook to lint your code before each commit:
npx husky add .husky/pre-commit 'npx --no -- lint-staged'
husky install
must be run in the same directory as .git, but you can change directory during prepare
script and pass a different directory. If your .git file and package.json are in different folders, you will have to specific the package.json directory in both the prepare
script and the husky hooks.
In this example, the .git file is one level below the folder with the package.json (located at /front
).
{
"scripts": {
"prepare": "cd .. && husky install front/.husky"
}
}
If you had a pre-commit hook, you would need to specify the directory as follows:
# ...
cd front
npm test
Based on the selected configuration, it will execute different actions according to the following rules:
*.{js,cjs,mjs,jsx,ts,tsx}
eslint --fix
*.{js,cjs,mjs,jsx,ts,tsx}
eslint --fix
prettier --write
*.{md,html,css,yml,yaml}
prettier --write