Declare git hooks in your package.json.
@jsenv/git-hooks
create a git hook for every scripts your package.json matching git-hook-*
.
npm install @jsenv/git-hooks@1.3.0
import { installGitHooks } from "@jsenv/git-hooks"
installGitHooks({
projectDirectoryUrl: "file:///directory",
})
If you use node < 13 you can use the commonjs export.
const { installGitHooks } = require("@jsenv/git-hooks")
installGitHooks
is an async function writing a hook file for every git hook script declared in a project package.json.
import { installGitHooks } from "@jsenv/git-hooks"
await installGitHooks({
projectDirectoryUrl: "file:///directory",
logLevel: "info",
})
— source code at src/installGitHooks.js.
projectDirectoryUrl
parameter is a string leading to a directory as documented in https://github.com/jsenv/jsenv-util#assertandnormalizedirectoryurl. It is used to locate your package.json and the directory where git hooks will be written.
logLevel
parameter is a string controlling the verbosity of logs during function exectuion as documented in https://github.com/jsenv/jsenv-logger#logLevel.
uninstallGitHooks
is an async function removing all git hooks installed by installGitHooks.
import { uninstallGitHooks } from "@jsenv/git-hooks"
await uninstallGitHooks({
projectDirectoryUrl: "file:///directory",
logLevel: "info",
})
— source code at src/uninstallGitHooks.js.
I wanted a git precommit hook to run prettier on staged files as documented in https://prettier.io/docs/en/precommit.html.
I wanted to declare my hooks in package.json script to benefit from vscode script explorer feature, see npm.enableScriptExplorer.
After that I discovered husky, https://github.com/typicode/husky, which could do the job but they have deprecated declaring hooks inside scripts field.