Developing JavaScript libraries with TypeScript rollup+ Commitizen + Husky (1)
Submit specifications
- Install and configure Commitizen
// Install commitizen globally
Mac:Math fanyanbo$ npm install commitizen -g
// Check the version number
Mac:Math fanyanbo$ npm view commitizen version
4.2.2
// Project directory execution to support the Angular specification's Commit Message
Mac:Math fanyanbo$ commitizen init cz-conventional-changelog --save --save-exact
Copy the code
- Install and configure CommitLint
// Install dependency packages
Mac:Math fanyanbo$ npm install -D @commitlint/config-conventional @commitlint/cli --registry https://registry.npm.taobao.org
// Create the configuration file in the root directory
Mac:Math fanyanbo$ touch commitlint.config.js
Copy the code
// commitlint.config.js
/ / configuration rules
module.exports = {
extends: ['@commitlint/config-conventional'].
rules: {
'type-enum': [2.'always'[
'feat'.'fix'.'docs'.'style'.'refactor'.'test'.'chore'.'revert'
]],
'scope-empty': [1.'never'].
'subject-case': [0.'always'].
'scope-case': [0]
}
}
Copy the code
For the Angular specification, the commitlint rules configuration, please click here
- Install and configure Husky
Before installing Husky, configure the git environment for the project. Otherwise, the following error will be displayed
husky > Setting up git hooks
fatal: not a git repository (or any of the parent directories): .git
husky > Failed to install
Copy the code
Create the project repository Math on Github or GitLab at https://github.com/ with your username/math.git
/ / configure git
Mac:Math fanyanbo$ git init
Mac:Math fanyanbo$git remote add Origin https://github.com/ Your username/math.git
Mac:Math fanyanbo$ git remote -v
Origin https://github.com/ Your username/math.git (fetch)
Origin https://github.com/ Your username/math.git (push)
Copy the code
Install Husky locally
Mac:Math fanyanbo$ npm install -D husky --registry https://registry.npm.taobao.org
Mac:Math fanyanbo$ npm view husky version
4.3.6
Copy the code
Configure husky in package.json and commit message with commitLint validation
// package.js
.
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
.
Copy the code
With git Cz commit, there will be a boot prompt after git Cz is executed
Mac:Math fanyanbo$ git add .
Mac:Math fanyanbo$ git cz
[email protected], [email protected]
? Select the type of change that you're committing: (Use arrow keys)
❯ feat: A new feature
fix: A bug fix
docs: Documentation only changes
style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
refactor: A code change that neither fixes a bug nor adds a feature
perf: A code change that improves performance
test: Adding missing tests or correcting existing tests
(Move up and down to reveal more choices)
// Enter type, scope, and description as prompted. Type is mandatory
? Select the type of change that you're committing: feat: A new feature
? What is the scope of this change (e.g. component or file name): (press enter to skip) all
? Write a short, imperative tense description of the change (max 89 chars):
(12) init project
? Provide a longer description of the change: (press enter to skip)
? Are there any breaking changes? No
? Does this change affect any open issues? No
Husky > commit- MSG (node v14.15.0)
[master 14de90e] feat(all): init project
1 file changed, 5 insertions(+)
// Commit to the remote repository
Mac:Math fanyanbo$ git push
Copy the code
Github description (feat(all): Init Project) is a short description
Or commit with Git commit and demonstrate both non-standard and canonical formats respectively
Mac:Math fanyanbo$ git add .
Mac:Math fanyanbo$ git commit -m "This is not a canonical commit message"
Husky > commit- MSG (node v14.15.0)
⧗ input: This is not a canonical commit message
* Subject may not be empty [subject-empty]
✖ type may not be empty [type-empty]
⚠ scope may not be empty [scope-empty]
* Found 2 problems, 1 warnings
ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint
husky > commit-msg hook failed (add --no-verify to bypass)
Copy the code
An invalid message format will throw the above error, preventing the commit. The validation rules are configured in commitlint.config.js
Mac:Math fanyanbo$ git commit -m Feat (all): This is a commit message
Husky > commit- MSG (node v14.15.0)
[Master 1996AC2] feat(all
1 file changed, 1 insertion(+)
Mac:Math fanyanbo$ git push
Copy the code
Description Github Commits (feat(All)
- Install the standard version
Standard-version updates the version number and automatically generates the Changle log
Mac:Math fanyanbo$ npm install -D standard-version --registry https://registry.npm.taobao.org
// Configure in package.json scripts"version": "standard-version -r patch"
Mac:Math fanyanbo$ npm run version
> [email protected] version/Users/fanyanbo/juejin/Math
> standard-version -r patch
✔ bumping version inPackage. json from 1.0.0 to 1.0.1
✔ bumping version inPackage-lock. json from 1.0.0 to 1.0.1
✔ created CHANGELOG. The md
➤ ➤ Outputting changes to changelog.md
Configure package-lock.json and package.json and changelo.md
Husky > commit- MSG (node v14.15.0)
✔ tagging release v1.0.1
ℹ Run `git push --follow-tags origin master && npm publish` to publish
Mac:Math fanyanbo$ git push
Copy the code
As shown above, NPM run version increases the revision number by 1 from v1.0.0 to v1.0.1. It is also convenient to change the major and minor versions
In addition, a changlogel. md file is automatically generated in the root directory, which records the previous commit messages
Of course, the generation of Changle log can also use xconvention-Changelog-CLI tool, this article does not do the specific introduction, interested can learn by themselves, please stamp here
conclusion
Content is more extensive, not in-depth, we take its essence to its dregs.