Skip to content

docs: improve local hook install explanation #2457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,27 +93,28 @@ npm install --save-dev @commitlint/config-conventional @commitlint/cli
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
```

To lint commits before they are created you can use Husky's 'commit-msg' hook.
To lint commits before they are created you can use Husky's `commit-msg` hook:

Install in your project `npm install husky --save-dev` or `yarn add -D husky`.

After that, you can create a `.huskyrc` file or add to your `package.json` the following code for
```sh
# Install Husky v5
npm install husky --save-dev
# or
yarn add husky --dev

Husky V4:
# Active hooks
npx husky install
# or
yarn husky install

```json
{
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
}
}
# Add hook
npx husky add .husky/commit-msg "npx --no-install commitlint --edit $1"
# or
yarn husky add .husky/commit-msg "yarn commitlint --edit $1"
```

Husky V5
If the file `.husky/commit-msg` already exists, you can edit the file and put this:

```
```sh
# .husky/commit-msg
# ...
npx --no-install commitlint --edit $1
Expand Down
41 changes: 23 additions & 18 deletions docs/guides-local-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ Install `commitlint` and a `commitlint-config-*` of your choice as devDependency
configure `commitlint` to use it.

```bash
# Create a package.json if needed
npm init

# Install and configure if needed
npm install --save-dev @commitlint/{cli,config-conventional}
echo "module.exports = {extends: ['@commitlint/config-conventional']};" > commitlint.config.js
Expand All @@ -24,24 +21,32 @@ Alternatively the configuration can be defined in `.commitlintrc.js`, `.commitli

Install `husky` as devDependency, a handy git hook helper available on npm.

```bash
npm install --save-dev husky
```sh
# Install Husky v5
npm install husky --save-dev
# or
yarn add husky --dev

# Active hooks
npx husky install
# or
yarn husky install

# Add hook
npx husky add .husky/commit-msg "npx --no-install commitlint --edit $1"
# or
yarn husky add .husky/commit-msg "yarn commitlint --edit $1"
```

This allows us to add [git hooks](https://git-scm.com/docs/githooks) directly into our `package.json` via the `husky.hooks` field.

```json
// package.json
{
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
}
}
```
If the file `.husky/commit-msg` already exists, you can edit the file and put this:

Using `commit-msg` gives us exactly what we want: It is executed whenever a new commit is created. Passing husky's `HUSKY_GIT_PARAMS` to `commitlint` via the `-E|--env` flag directs it to the relevant edit file. `-e` would default to `.git/COMMIT_EDITMSG`.
```sh
# .husky/commit-msg
# ...
npx --no-install commitlint --edit $1
# or
yarn commitlint --edit $1
```

## Test

Expand Down