Skip to content

Commit

Permalink
feat: new approach (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Sep 25, 2023
1 parent 01e133c commit a0031bd
Show file tree
Hide file tree
Showing 19 changed files with 193 additions and 299 deletions.
1 change: 1 addition & 0 deletions .shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
disable=SC1090,SC2164
35 changes: 0 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,3 @@
# husky

> Modern native Git hooks made easy
Husky improves your commits and more 🐶 *woof!*

# Install

```
npm install husky --save-dev
```

# Usage

Edit `package.json > prepare` script and run it once:

```sh
npm pkg set scripts.prepare="husky install"
npm run prepare
```

Add a hook:

```sh
npx husky add .husky/pre-commit "npm test"
git add .husky/pre-commit
```

Make a commit:

```sh
git commit -m "Keep calm and commit"
# `npm test` will run
```

# Documentation

https://typicode.github.io/husky
144 changes: 68 additions & 76 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
@@ -1,105 +1,66 @@
# Getting started

## Automatic (recommended)

`husky-init` is a one-time command to quickly initialize a project with husky.
Install husky and configure Git hooks

::: code-group

```shell [npm]
npx husky-init && npm install
npm install --save-dev husky
npx husky
```

```shell [pnpm]
pnpm dlx husky-init && pnpm install
pnpm add --save-dev husky
pnpm exec husky
```

```shell [yarn]
yarn dlx husky-init --yarn2 && yarn
yarn add --dev husky
yarn exec husky
```

:::

It will:

1. Add `prepare` script to `package.json`
1. Create a sample `pre-commit` hook that you can edit (by default, `npm test` will run when you commit)
1. Configure Git hooks path

To add another hook use `husky add`. For example:

```shell
npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'
```

::: info
For Windows users, if you see the help message when running `npx husky add ...`, try `node node_modules/husky/lib/bin add ...` instead. This isn't an issue with husky code.
:::

## Manual

### Install

1. Install `husky`

```shell
npm install husky --save-dev
```

2. Enable Git hooks

```shell
npx husky install
```

3. To automatically have Git hooks enabled after install, edit `package.json`

```shell
npm pkg set scripts.prepare="husky install"
```

You should have:
To automatically have Git hooks enabled after install, edit `package.json`

::: code-group

```json [package.json]
{
"scripts": {
"prepare": "husky install" // [!code hl]
"prepare": "husky" // [!code hl]
}
}
```

:::

::: info
Yarn 2+ doesn't support `prepare` lifecycle script, so husky needs to be installed differently (this doesn't apply to Yarn 1 though). See [Yarn 2+ install](#yarn-2).
:::
Create a `pre-commit` hook

## Create a hook

To add a command to a hook or create a new one, use `husky add <file> [cmd]` (don't forget to run `husky install` before).
::: code-group

```shell
npx husky add .husky/pre-commit "npm test"
git add .husky/pre-commit
```shell [.husky/pre-commit]
npm test
```

Try to make a commit
:::

Make a commit

```shell
git commit -m "Keep calm and commit"
# `npm test` will run every time you commit
```

If `npm test` command fails, your commit will be automatically aborted.
::: info
Yarn 2+ doesn't support `prepare` lifecycle script, so husky needs to be installed differently (this doesn't apply to Yarn 1 though). See [Yarn 2+ install](#yarn-2).
:::

::: warning
**Using Yarn to run commands? There's an issue on Windows with Git Bash, see [Yarn on Windows](#yarn-on-windows).**
:::

_For Windows users, if you see the help message when running `npx husky add ...`, try `node node_modules/.bin/husky add ...` instead. This isn't an issue with husky code and is fixed in recent versions of npm 8._

### Uninstall
Uninstall

```shell
npm uninstall husky && git config --unset core.hooksPath
Expand All @@ -119,35 +80,26 @@ yarn add pinst --dev # ONLY if your package is not private
2. Enable Git hooks

```shell
yarn husky install
yarn husky
```

3. To automatically have Git hooks enabled after install, edit `package.json`

::: code-group

```js [package.json]
```js [package.json private=true]
{
"private": true, // ← your package is private, you only need postinstall
"private": true,
"scripts": {
"postinstall": "husky install"
"postinstall": "husky"
}
}
```

:::

::: tip
if your package is not private and you're publishing it on a registry like [npmjs.com](https://npmjs.com), you need to disable `postinstall` script using [pinst](https://github.com/typicode/pinst)\*\*. Otherwise, `postinstall` will run when someone installs your package and result in an error.
:::

::: code-group

```js [package.json]
```js [package.json private=false]
{
"private": false, // ← your package is public
"scripts": {
"postinstall": "husky install",
"postinstall": "husky",
"prepack": "pinst --disable",
"postpack": "pinst --enable"
}
Expand All @@ -156,10 +108,50 @@ if your package is not private and you're publishing it on a registry like [npmj

:::

::: tip
if your package is not private and you're publishing it on a registry like [npmjs.com](https://npmjs.com), you need to disable `postinstall` script using [pinst](https://github.com/typicode/pinst)\*\*. Otherwise, `postinstall` will run when someone installs your package and result in an error.
:::

### Uninstall

Remove `"postinstall": "husky install"` from `package.json` and run:
Remove `"postinstall": "husky"` from `package.json` and run:

```shell
yarn remove husky && git config --unset core.hooksPath
```

## Automatic (recommended)

`husky-init` is a one-time command to quickly initialize a project with husky.

::: code-group

```shell [npm]
npx husky-init
```

```shell [pnpm]
pnpm dlx husky-init
```

```shell [yarn]
yarn dlx husky-init --yarn2
```

:::

`husky-init` will:

1. Add `prepare` script to `package.json`
1. Create a sample `pre-commit` hook that you can edit (by default, `npm test` will run when you commit)
1. Set Git `core.hooksPath` repository option

To add another hook, simply create a new file in `.husky/`. For example to add `commit-msg` hook:

:::code-group

```shell [.husky/commit-msg]
npx --no -- commitlint --edit "$1"
```

:::
20 changes: 11 additions & 9 deletions docs/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ If you want to install husky in another directory, for example `.config`, you ca
```js [package.json]
{
"scripts": {
"prepare": "husky install .config/husky"
"prepare": "husky -d .config/husky"
}
}
```
Expand All @@ -29,7 +29,7 @@ By design, `husky install` must be run in the same directory as `.git`, but you
```js [package.json]
{
"scripts": {
"prepare": "cd .. && husky install front/.husky"
"prepare": "cd .. && husky -d front/.husky"
}
}
```
Expand Down Expand Up @@ -103,7 +103,7 @@ if (!isCi) {
Or make `prepare` script fail silently if husky is not installed:

```json [package.json]
"prepare": "node -e \"try { require('husky').install() } catch (e) {if (e.code !== 'MODULE_NOT_FOUND') throw e}\""
"prepare": "node -e \"try { require('husky').default() } catch (e) {if (e.code !== 'MODULE_NOT_FOUND') throw e}\""
```

### With env variables
Expand Down Expand Up @@ -134,14 +134,14 @@ npm install is-ci --save-dev
```js [package.json]
{
"scripts": {
"prepare": "is-ci || husky install"
"prepare": "is-ci || husky"
}
}
```

:::

## Test hooks
## Testing hooks without commiting

If you want to test a hook, you can add `exit 1` at the end of the script to abort git command.

Expand All @@ -156,16 +156,18 @@ exit 1 # Commit will be aborted

## Git-flow

If using [git-flow](https://github.com/petervanderdoes/gitflow-avh/) you need to ensure your git-flow hooks directory is set to use Husky's (`.husky` by default).
If you're using [git-flow](https://github.com/petervanderdoes/gitflow-avh/) you need to ensure your git-flow hooks directory is set to use Husky's (`.husky/_` by default).

```shell
git config gitflow.path.hooks .husky
git config gitflow.path.hooks .husky/_
```

**Note:**
::: info

- If you are configuring git-flow _after_ you have installed husky, the git-flow setup process will correctly suggest the .husky directory.
- If you have set a [custom directory](#custom-directory) for husky you need to specify that (ex. `git config gitflow.path.hooks .config/husky`)
- If you have set a [custom directory](#custom-directory) for husky you need to specify that (ex. `git config gitflow.path.hooks .config/husky/_`)

:::

To **revert** the git-flow hooks directory back to its default you need to reset the config to point to the default Git hooks directory.

Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Click [here](/getting-started) to get started.

## Features

- Lightweight with zero dependencies (`6 kB`)
- Lightweight only `6 kB` with zero dependencies
- Powered by modern new Git feature (`core.hooksPath`)
- Follows [npm](https://docs.npmjs.com/cli/v8/using-npm/scripts#best-practices) and [Yarn](https://yarnpkg.com/advanced/lifecycle-scripts#a-note-about-postinstall) best practices regarding `autoinstall`
- User-friendly messages
Expand Down
7 changes: 2 additions & 5 deletions docs/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ For example, for `nvm` that would be:

::: code-group

```shell [~/.huskyrc]
```shell [~/.config/husky/init.sh]
# This loads nvm.sh, sets the correct PATH before running hook, and ensures the project version of Node
export NVM_DIR="$HOME/.nvm"

Expand All @@ -35,8 +35,7 @@ For some apps (e.g., VS Code), you can resolve this simply by restarting the app
## Hooks not running

1. Ensure that you don't have a typo in your filename. For example, `precommit` or `pre-commit.sh` are invalid names. See Git hooks [documentation](https://git-scm.com/docs/githooks) for valid names.
1. Check that `git config core.hooksPath` returns `.husky` (or your custom hooks directory).
1. Verify that hook files are executable. This is automatically set when using `husky add` command but you can run `chmod +x .husky/<hookname>` to fix that.
1. Check that `git config core.hooksPath` returns `.husky/_` (or your custom hooks directory).
1. Check that your version of Git is greater than `2.9`.

## .git/hooks/ not working after uninstall
Expand Down Expand Up @@ -65,8 +64,6 @@ fi
2. Source it in in places where Yarn is used to run commands:

```shell
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
. "$(dirname -- "$0")/common.sh"

yarn ...
Expand Down
Loading

0 comments on commit a0031bd

Please sign in to comment.