In front-end project development, standardizing git submission information is also a frequently used method. This article will introduce tools such as husky and lint-staged. Using them well will help us to standardize git and team collaboration in project development.
husky
Husky is a git hooks
management tool that allows us to manage git hooks
scripts more conveniently. It will trigger different hooks when we submit the code, execute different scripts, and help us automate some tasks, such as executing eslint
commands .
First, install husky:
npm install husky -D
Then package.json
, scripts
configure the autoinstall script in the of the file:
"prepare": "husky install"
After setting up the configuration script, when we execute npm install
the command operation, npm run prepare
the command will be executed automatically, that is, husky install
the command , and .husky
the directory will be generated under the root directory of the project, as shown in the figure below:
.husky
two files will be automatically generated under the directory, of which The script file husky.sh
represents husky
the initial basic configuration of the .
In addition, of course, we can also execute prepare
the command , which is also valid:
npm run prepare
Custom configuration directory
.husky
The directory is generated by default. Of course, we can also customize the file directory of husky configuration, just add parameters to the command, as shown below:
husky install gitHooks/husky
In this way, gitHooks
the directory husky
, and the corresponding initialization configuration files will also be automatically generated.
npm life cycle
The execution npm install
command will automatically execute npm run prepare
the command, which is derived from the lifecycle hook of npm.
prepare
is a phase in the lifecycle of npm
a script command action install
that is triggered when executed. When the command
is executed , the corresponding commands will be executed sequentially:install
- npm 6.x:preinstall -> install -> postinstall -> prepublish -> prepare
- npm 7+:preinstall -> install -> postinstall -> prepublish -> preprepare -> prepare -> postprepare
After knowing the specific execution sequence, we can add the operations we want at the failed stage, such as the automatic installation of the husky tool above.
For example, we can also add hooks in scripts to unify the package management tools of the project:
"preinstall": "npx only-allow pnpm",
Through the above command, specify the package installation tool of the project as pnpm
, if you use npm
or yarn
to install
execute , an error will be reported, and the three-party package cannot be installed.
Set up git hooks
After the husky installation is complete and the script is configured, we can git hooks
set it up.
git hooks
Allows us to automatically execute custom scripts when specific commands of git operations occur, to accomplish some additional things.
In our specification of git submission information, the two commonly used stages are: pre-commit
and commit-msg
:
- pre-commit: Executed before the code is submitted. Can handle code formatting specifications, etc.
- commit-msg: Process the message information submitted by the code.
In addition, git hooks have multiple stages that can be enabled when needed, such as the following:
pre-receive: After completing a push from the local version
prepare-commit-msg: After the information is prepared but editing has not yet started
pre-push: Execute
post-update before push: Execute after the workspace update is completed
pre-rebase: In the rebase operation Executed before
pre-commit
In the above introduction, it is mentioned that the husky tool will generate .husky
a directory , which stores the basic configuration of husky. To configure git hooks
, you have to operate in this directory, you can take the following two ways.
- First, we can directly create a new file in
.husky
the directory : pre-commit, pay attention to the no suffix . Then add the following to the file:
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npx eslint src
Among them, npx eslint src
are commands that need to be executed before git submission, and can also be replaced npm run lint
with scripts
scripts like etc.
- The second, or use a script command to generate the file, execute the following script:
npx husky add .husky/pre-commit "npx eslint src"
After the script is executed .husky
, pre-commit
a file will be automatically generated in the directory, and the corresponding script command will be written: npx eslint src
, the content of which is the same as the first method above.
After completing the above operations, when we execute git commit
the command , the eslint command will be automatically executed. In addition to eslint, we can also configure other such as stylelint
, prettier
etc.
commit-msg
message
The specification of git submission information is also an indispensable part of our project development. This content can help teamwork, version logs, etc.
There are many kinds of agreed-upon specifications in the message
submitted specifications, Conventional Commits specification
such as, but it is obviously impossible to guarantee the complete and correct execution of the specification only by the specification agreement, so we need auxiliary tools to help us deal with it, for example commitlint
.
commitlint
commitlint is currently the most widely used git submission specification verification tool, which can better help us verify message
the specification .
First, we need to install two dependencies:
npm install @commitlint/cli -D
npm install @commitlint/config-conventional -D
in:
- @commitlint/config-conventional is a
conventional commits
specification configuration file. - @commitlint/cli is the heart of
commitlint
the tool .
Configure commitlint
Then, add the .commitlintrc.js
configuration file:
You can
package.json
also addcommitlint
the configuration of the field in the file.
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [
2,
'always',
[
'feat', // 新功能(feature)
'fix', // 修复bug
'docs', // 修改文档
'style', // 修改代码格式,不影响代码逻辑
'refactor', // 代码重构,理论上不影响功能逻辑
'test', // 修改测试用例
'build', // 构建或其他工具的变动(如webpack)
'revert', // 还原以前的提交
'merge', // 分支代码合并
],
],
},
};
The above is a basic command configuration, which mainly defines the content type
of , and the submitted message
s type
need to follow the above configuration.
git
TheCommit Message
has three parts:Header
,Body
andFooter
.
Among themHeader
is required, in most of the project development, our git submission information should only contain this part.
Header
Also contains three parts:type
,scope
,subject
, wheretype
andsubject
are required.
type
Represents the type of submitted information, which is mainly regulated in the configuration file above.
subject
Represents the description content of the submitted information.
Set commit-msg hook
Next, configuration commit-msg hook
, through the husky tool introduced earlier, we directly create a new file in .husky
the directory commit-msg
, the content is as follows:
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
npx --no-install commitlint --edit $1
You can also use script commands to automatically generate the file and content:
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1'
If you don't use the npx command, it is also possible to use yarn, yarn commitlint --edit $1
.
After configuring the hook file, the operation is basically completed and verification can be performed.
Verify git commit
Below, we submit a git message that does not conform to the specification:
git commit -m '测试一下'
At this time, the submission will fail, and the information is as follows:
As shown in the figure above, git submission failed, there is a problem: type
and subject
the content may be empty.
Because the information is commitlint
configured type
, we also need to add type
the type . For example, the following command can be submitted normally:
git commit -m 'test: 测试一下'
There is a line in the picture above: No staged files match any configured task.
This is the prompt that we have set up lint-staged
, but no code file is currently hit, and this prompt will appear.
lint-staged
When pre-commit
introducing , we used npx eslint src
the command to process code specifications. But it has a problem, that is, every time git submits, it will detect all the files in the entire project src. In many cases, this is not needed. The best way is to detect the newly modified code. The tool
is based on this, and only checks and processes the submitted code files.lint-staged
Install lint-staged:
npm install lint-staged -D
lint-staged needs to add configuration, and generally two methods can be used.
- Add content directly to the package.json file, as follows:
"lint-staged": {
"*.{js,jsx,vue,ts}": [
"eslint src"
]
}
- Or, add a configuration file, for example
lintstagedrc.json
, the content is as follows:
{
"*.{js,jsx,vue,ts}": ["eslint src"]
}
After configuration, you can modify the pre-commit file and add the following script:
npx lint-staged
At this point, the setting lint-staged
is complete. git commit
When submitting the code using again, actions such as code checking will be performed first, and it is an incremental code checking .
skip git hooks
If we want to skip commit
related hooks, we can add --no-verify
parameters to the script command, as shown below:
git commit -m '跳过hook' --no-verify
In this way, all the checks we set up earlier will be skipped, and the code submission action will be completed directly.