### Husky setup with npm/pnpm/bun prepare script Source: https://typicode.github.io/husky/how-to.html Use the `prepare` script in `package.json` to automatically run `husky` after each install. This is the recommended approach for npm, pnpm, and bun. ```json { "scripts": { "prepare": "husky" } } ``` ```json { "scripts": { "prepare": "husky" } } ``` ```json { "scripts": { "prepare": "husky" } } ``` -------------------------------- ### Husky setup with Yarn postinstall script Source: https://typicode.github.io/husky/how-to.html For Yarn, use the `postinstall` script as it does not support the `prepare` script. Include `prepack` and `postpack` for npm publishing. ```json { "scripts": { "postinstall": "husky", "prepack": "pinst --disable", "postpack": "pinst --enable" } } ``` -------------------------------- ### Use custom install script in prepare Source: https://typicode.github.io/husky/how-to.html Use a custom Node.js script (e.g., .husky/install.mjs) in the 'prepare' script of package.json to manage Husky installation. ```json "prepare": "node .husky/install.mjs" ``` -------------------------------- ### Install Husky with bun Source: https://typicode.github.io/husky/get-started.html Use this command to install Husky as a dev dependency in your project using bun. ```shell bun add --dev husky ``` -------------------------------- ### Install Husky with pnpm Source: https://typicode.github.io/husky/get-started.html Use this command to install Husky as a dev dependency in your project using pnpm. ```shell pnpm add --save-dev husky ``` -------------------------------- ### Install Husky with npm Source: https://typicode.github.io/husky/get-started.html Use this command to install Husky as a dev dependency in your project using npm. ```shell npm install --save-dev husky ``` -------------------------------- ### Prevent prepare script failure in package.json Source: https://typicode.github.io/husky/how-to.html Modify the 'prepare' script in package.json to prevent it from failing if Husky is not installed, especially when only installing dependencies. This example uses 'husky || true'. ```json // package.json "prepare": "husky || true" ``` -------------------------------- ### Install Husky and pinst with yarn Source: https://typicode.github.io/husky/get-started.html Use this command to install Husky as a dev dependency with yarn. Add pinst only if your package is not private. ```shell yarn add --dev husky # Add pinst ONLY if your package is not private yarn add --dev pinst ``` -------------------------------- ### Add a pre-commit hook Source: https://typicode.github.io/husky/how-to.html Create a new hook file by creating a file in the .husky directory. This example shows how to add a pre-commit hook that runs 'npm test'. ```shell echo "npm test" > .husky/pre-commit ``` -------------------------------- ### Disable Husky in CI/Docker Source: https://typicode.github.io/husky/how-to.html Use HUSKY=0 to avoid installing Git hooks on CI servers or in Docker. This example shows how to set it in GitHub Actions. ```yml # https://docs.github.com/en/actions/learn-github-actions/variables env: HUSKY: 0 ``` -------------------------------- ### Lint staged files with Prettier Source: https://typicode.github.io/husky/get-started.html This example shows how to lint staged files using Prettier on each commit. It uses POSIX shell scripting. ```shell # .husky/pre-commit prettier $(git diff --cached --name-only --diff-filter=ACMR | sed 's| |\ |g') --write --ignore-unknown git update-index --again ``` -------------------------------- ### Silent Husky install in production/CI Source: https://typicode.github.io/husky/how-to.html Create a .husky/install.mjs file to skip Husky installation in production and CI environments silently. This script checks for NODE_ENV and CI environment variables. ```js // Skip Husky install in production and CI if (process.env.NODE_ENV === 'production' || process.env.CI === 'true') { process.exit(0) } const husky = (await import('husky')).default console.log(husky()) ``` -------------------------------- ### Migrate local binary calls from v4 to v9 Source: https://typicode.github.io/husky/migrate-from-v4.html When calling locally installed binaries, use your package manager (e.g., npm, yarn) to execute them in v9. This ensures binaries are found correctly. ```js { "hooks": { "pre-commit": "jest" } } ``` ```shell jest ``` -------------------------------- ### Commit to test Husky Source: https://typicode.github.io/husky/get-started.html Make a commit to test if your Husky setup is working correctly. The test script will run on every commit. ```shell git commit -m "Keep calm and commit" # test script will run every time you commit ``` -------------------------------- ### Adjust prepare script for non-root Git projects Source: https://typicode.github.io/husky/how-to.html When the project is not in the Git root directory, adjust the 'prepare' script to change the directory before running Husky. This example assumes the package.json is in the 'frontend' directory. ```json "prepare": "cd .. && husky frontend/.husky" ``` -------------------------------- ### Test hooks by aborting Git command Source: https://typicode.github.io/husky/how-to.html To test a hook, add 'exit 1' to the hook script. This will abort the Git command without creating a commit. The example shows this for a .husky/pre-commit hook. ```shell # .husky/pre-commit # Your WIP script # ... exit 1 ``` ```shell git commit -m "testing pre-commit code" # A commit will not be created ``` -------------------------------- ### Adjust hook script for non-root Git projects Source: https://typicode.github.io/husky/how-to.html In hook scripts for projects not in the Git root, change the directory back to the relevant subdirectory before executing commands. This example shows changing to the 'frontend' directory. ```shell # frontend/.husky/pre-commit cd frontend npm test ``` -------------------------------- ### Initialize Husky with bunx Source: https://typicode.github.io/husky/get-started.html Run this command to initialize Husky in your project using bun. ```shell bunx husky init ``` -------------------------------- ### Execute Husky prepare script Source: https://typicode.github.io/husky/how-to.html Manually run the `prepare` script using npm, pnpm, or bun to set up Husky. ```shell npm run prepare ``` ```shell pnpm run prepare ``` ```shell bun run prepare ``` -------------------------------- ### Source shell startup file in Husky Source: https://typicode.github.io/husky/how-to.html Alternatively, source your shell's startup file directly in `~/.config/husky/init.sh` if it's fast and lightweight. ```shell # ~/.config/husky/init.sh . ~/.zshrc ``` -------------------------------- ### Execute Husky postinstall script for Yarn Source: https://typicode.github.io/husky/how-to.html For Yarn users, manually run the `postinstall` script to set up Husky. ```shell # Yarn doesn't support `prepare` yarn run postinstall ``` -------------------------------- ### Initialize nvm in Husky Source: https://typicode.github.io/husky/how-to.html Source nvm initialization script in `~/.config/husky/init.sh` to ensure Node.js is available in GUI environments. ```shell # ~/.config/husky/init.sh export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm ``` -------------------------------- ### Initialize Husky with npx Source: https://typicode.github.io/husky/get-started.html Run this command to initialize Husky in your project. It creates a pre-commit script and updates package.json. ```shell npx husky init ``` -------------------------------- ### Create pre-commit hook with bun Source: https://typicode.github.io/husky/how-to.html Create a `.husky/pre-commit` file to define the command to run before each commit when using bun. ```shell # .husky/pre-commit bun test ``` -------------------------------- ### Initialize Husky with pnpm exec Source: https://typicode.github.io/husky/get-started.html Run this command to initialize Husky in your project using pnpm. ```shell pnpm exec husky init ``` -------------------------------- ### Create pre-commit hook with npm Source: https://typicode.github.io/husky/how-to.html Create a `.husky/pre-commit` file to define the command to run before each commit when using npm. ```shell # .husky/pre-commit npm test ``` -------------------------------- ### Create pre-commit hook with pnpm Source: https://typicode.github.io/husky/how-to.html Create a `.husky/pre-commit` file to define the command to run before each commit when using pnpm. ```shell # .husky/pre-commit pnpm test ``` -------------------------------- ### Create pre-commit hook with Yarn Source: https://typicode.github.io/husky/how-to.html Create a `.husky/pre-commit` file to define the command to run before each commit when using Yarn. ```shell # .husky/pre-commit yarn test ``` -------------------------------- ### Run non-shell hooks with NodeJS Source: https://typicode.github.io/husky/how-to.html To run scripts requiring a scripting language like NodeJS, create an entrypoint file (e.g., .husky/pre-commit) that calls the script (e.g., .husky/pre-commit.js) using the appropriate interpreter. ```shell .husky/pre-commit ``` ```shell node .husky/pre-commit.js ``` ```javascript // Your NodeJS code // ... ``` -------------------------------- ### Migrate npm/yarn scripts from package.json to .husky/hook Source: https://typicode.github.io/husky/migrate-from-v4.html Copy commands directly from your package.json 'hooks' to the corresponding file in the .husky/ directory. Commands can now span multiple lines. ```json // package.json { "hooks": { "pre-commit": "npm test && npm run foo" } } ``` ```shell # .husky/pre-commit # Note that you can now have commands on multiple lines npm test // [!code hl] npm run foo // [!code hl] ``` -------------------------------- ### Unset Git Hooks Path Source: https://typicode.github.io/husky/troubleshoot.html Execute this command if hooks in `.git/hooks/` do not work after uninstalling Husky. ```shell git config --unset core.hooksPath ``` -------------------------------- ### Migrate HUSKY_GIT_PARAMS to native parameters Source: https://typicode.github.io/husky/migrate-from-v4.html The HUSKY_GIT_PARAMS environment variable is replaced by native Git parameters like $1, $2, etc. Use these directly in your scripts. ```js { "hooks": { "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" } } ``` ```shell commitlint --edit $1 ``` -------------------------------- ### Source Common Script in Husky Hook Source: https://typicode.github.io/husky/troubleshoot.html Source the `common.sh` script at the beginning of your Husky hook file (e.g., `.husky/pre-commit`) to apply the Windows Yarn workaround before running Yarn commands. ```shell # .husky/pre-commit . .husky/common.sh yarn ... ``` -------------------------------- ### Husky Common Script for Windows Yarn Source: https://typicode.github.io/husky/troubleshoot.html Create this script to work around the 'stdin is not a tty' error when using Yarn on Windows with Git Bash. This script checks for `winpty` and a TTY, and if both are true, it redirects stdin from the TTY. ```shell command_exists () { command -v "$1" >/dev/null 2>&1 } # Workaround for Windows 10, Git Bash, and Yarn if command_exists winpty && test -t 1; then exec < /dev/tty fi ``` -------------------------------- ### Skip Git hooks for a single command Source: https://typicode.github.io/husky/how-to.html Use the -n or --no-verify option with most Git commands to skip hooks. For commands without this flag, temporarily disable hooks using HUSKY=0. ```sh git commit -m "..." -n # Skips Git hooks ``` ```shell HUSKY=0 git ... # Temporarily disables all Git hooks git ... # Hooks will run again ``` -------------------------------- ### Use Bash in hook scripts Source: https://typicode.github.io/husky/how-to.html If your team does not use Windows, you can use Bash within hook scripts by embedding the Bash script within a heredoc (<< EOF). ```shell # .husky/pre-commit bash << EOF # Put your bash script inside # ... EOF ``` -------------------------------- ### Disable Git hooks globally Source: https://typicode.github.io/husky/how-to.html To disable Git hooks in a GUI client or globally, modify the husky configuration file (~/.config/husky/init.sh) and set HUSKY=0. ```sh # ~/.config/husky/init.sh export HUSKY=0 # Husky won't install and won't run hooks on your machine ``` -------------------------------- ### Temporarily disable Git hooks Source: https://typicode.github.io/husky/how-to.html Disable all Git hooks for an extended period, such as during a rebase or merge operation, by setting the HUSKY environment variable to 0 and then unsetting it when done. ```shell export HUSKY=0 # Disables all Git hooks git ... git ... unset HUSKY # Re-enables hooks ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.