diff --git a/README.md b/README.md index 1d92871c4f..387a1572c0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/docs/guides-local-setup.md b/docs/guides-local-setup.md index df2f9f8f5d..2a3899adf2 100644 --- a/docs/guides-local-setup.md +++ b/docs/guides-local-setup.md @@ -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 @@ -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