husky and lint-staged to run estlint and prettier on pre-commit

20/02/2022

install lint-staged

npm install -D lint-staged

create this file with following content : .lintstagedrc

{
    "*.js": [
        "eslint --fix",
        "prettier --write"
    ]
}

you can now try to lint staged files with

npx lint-staged

install husky

npm install -D husky
npm pkg set "scripts.prepare=husky"
npm run prepare

create hook

npx husky add .husky/pre-commit "npx lint-staged"
git add .husky/pre-commit

create another hook

to automatically run "npm install" on package.json changes.
we can write in this file .husky/post-merge

#!/usr/bin/env sh
if $(git diff --name-only HEAD@{1} HEAD | grep package-lock.json) ; then
    echo "? package-lock.json changed. Running npm ci to update your dependencies..."
    npm ci
else
    echo "? no need to update dependencies"
fi

on another machine you can git clone, and this command is automatically executed on "npm install", otherwise run it :

npm run prepare