使用 TypeScript + rollup + commitizen + husky 开发JavaScript类库(2)

1,140 阅读3分钟

接着上篇内容,使用TypeScript+rollup+commitizen+husky开发JavaScript类库(1)

提交规范

  1. 安装配置commitizen
// 全局安装commitizen
Mac:Math fanyanbo$ npm install commitizen -g
// 查看版本号
Mac:Math fanyanbo$ npm view commitizen version 
4.2.2
// 项目目录执行,使其支持Angular规范的commit message
Mac:Math fanyanbo$ commitizen init cz-conventional-changelog --save --save-exact
  1. 安装配置commitlint
// 安装相关依赖包
Mac:Math fanyanbo$ npm install -D @commitlint/config-conventional @commitlint/cli --registry https://registry.npm.taobao.org
// 根目录创建配置文件
Mac:Math fanyanbo$ touch commitlint.config.js
// commitlint.config.js
// 配置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]
    }
}

上面提到的 Angular 规范说明,commitlint 的 rules 配置说明, 请戳这里

  1. 安装配置husky

安装husky前先配置项目的 git 环境,否则会提示如下错误

husky > Setting up git hooks
fatal: not a git repository (or any of the parent directories): .git
husky > Failed to install

在 github 或 gitlab 上创建项目仓库 Math,创建后仓库地址是:https://github.com/你的用户名/Math.git

// 配置git
Mac:Math fanyanbo$ git init
Mac:Math fanyanbo$ git remote add origin https://github.com/你的用户名/Math.git
Mac:Math fanyanbo$ git remote -v
origin  https://github.com/你的用户名/Math.git (fetch)
origin  https://github.com/你的用户名/Math.git (push)

本地安装 husky

Mac:Math fanyanbo$ npm install -D husky --registry https://registry.npm.taobao.org
Mac:Math fanyanbo$ npm view husky version
4.3.6

package.json 文件中配置 husky,commit message 时进行 commitlint 校验

// package.js
  ...
  "husky": {
    "hooks": {
      "commit-msg""commitlint -E HUSKY_GIT_PARAMS"
    }
  },
  ...

4. 提交到远程仓库

使用 git cz 提交, 执行 git cz 后会有引导提示

Mac:Math fanyanbo$ git add .
Mac:Math fanyanbo$ git cz
cz-cli@4.2.2, cz-conventional-changelog@3.3.0

? 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)

// 根据提示填写type,scope,description等,type是必选项
? 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(+)

// 提交到远程仓库
Mac:Math fanyanbo$ git push

在 github Commits 中能看到 feat(all): init project 这条提交记录,提交格式规范为 type(scope): a short description

或者用 git commit 提交, 分别用不规范格式和规范格式示范

Mac:Math fanyanbo$ git add .
Mac:Math fanyanbo$ git commit -m "这不是规范的commit message"
husky > commit-msg (node v14.15.0)
⧗   input: 这不是规范的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)

填写不规范的 message 格式时,会抛出上述报错,阻止本次提交,校验规则在 commitlint.config.js 中配置

Mac:Math fanyanbogit commit -m "feat(all): 这是规范的commit message"
husky > commit-msg (node v14.15.0)
[master 1996ac2] feat(all): 这是规范的commit message
 1 file changed, 1 insertion(+)
Mac:Math fanyanbogit push

填写规范的 message 格式时,顺利完成提交,在 github Commits 中能看到 feat(all): 这是规范的commit message 这次提交记录

  1. 安装 standard-version

standard-version 可以更新版本号,另外自动生成Changle log

Mac:Math fanyanbo$ npm install -D standard-version --registry https://registry.npm.taobao.org

// 在package.json scripts 配置 "version""standard-version -r patch"
Mac:Math fanyanbo$ npm run version

> math@1.0.0 version /Users/fanyanbo/juejin/Math
> standard-version -r patch

✔ bumping version in package.json from 1.0.0 to 1.0.1
✔ bumping version in package-lock.json from 1.0.0 to 1.0.1
✔ created CHANGELOG.md
✔ outputting changes to CHANGELOG.md
✔ committing package-lock.json and package.json and CHANGELOG.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

如上所示,执行 npm run version 后,修订号 增加 1,由 v1.0.0 变成了 v1.0.1, 当然也可以方便改变主版本号和副版本号,想进一步学习,请戳这里

另外,根目录下自动生成了 CHANGLELOG.md 文件,里面记录了之前commit message的提交记录

当然生成 Changle log,也可以用 conventional-changelog-cli 工具,本文不做具体介绍,有兴趣可自行了解,请戳这里

总结

内容比较泛,不深入,大家取其精华 去其糟粕就好。