Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juniormartinxo/4f7342e62164296063ead2dd813db965 to your computer and use it in GitHub Desktop.
Save juniormartinxo/4f7342e62164296063ead2dd813db965 to your computer and use it in GitHub Desktop.

Como melhorar commits com Husky, Commitlint, Commitizen e Lint-staged

Caso ainda não tenha criado, crie o repositório git ou clone um que já exista.

Instale e configure o commitlint

Execute no terminal

npm i -D @commitlint/config-conventional @commitlint/cli cz-conventional-changelog

Depois, rode o seguinte comando para criar o arquivo de configuração do commintlint

echo '{ "extends": ["@commitlint/config-conventional"] }' > .commitlintrc.json

Dica 😉

Se o seu package.json estiver configurado com "type": "module", altere a extensão do commitlint.config.js para .cjs.

Instale o Commitizen globalmente

npm install -g commitizen

A instalação global evita o erro:

git: 'cz' is not a git command. See 'git --help'

Insira a configuração do commitzen no package.json

"devDependencies": {
"comment": "Sua dependências..."
},
"config": {
    "commitizen": {
        "path": "./node_modules/cz-conventional-changelog"
    }
}

Edite o package.json > commit com

npm pkg set scripts.commit="git-cz"

Instalando Lint-staged

npm install lint-staged

Instalando o Husky

npm install husky --save-dev

Depois, rode o seguinte comando para criar o arquivo de configuração do lintstagedrc

echo '{ "*.{js,jsx,ts,tsx}": "eslint --fix" }' > .lintstagedrc.json

Para versão v8 do Husky

Edite o package.json > prepare com

npm pkg set scripts.prepare="husky install" && npm run prepare

Para versão v9 do Husky

husky init

O comando irá criar o script no package.json e irá executar o install do Husky

Adicione os hooks

Dispara o commintlint

Para versão v8 do Husky

npx husky add .husky/commit-msg 'npx commitlint --edit $1'

Para versão v9 do Husky

echo 'npx commitlint --edit $1' > .husky/commit-msg

Dispara o pre-commit

Para versão v8 do Husky

npx husky add .husky/pre-commit "npx lint-staged"

Para versão v9 do Husky

echo 'npx lint-staged' > .husky/pre-commit

Dispara o menu de perguntas do Commitzen

Para versão v8 do Husky

npx husky add .husky/prepare-commit-msg "exec < /dev/tty && git cz --hook || true"

Para versão v9 do Husky

echo 'exec < /dev/tty && git cz --hook || true' > .husky/prepare-commit-msg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment