更新日志是你在一段时间内对你的项目所做的任何修改的详细记录。更新日志不仅是修复bug和错误的起点,而且在向新的开发者介绍你的项目时,它也是一种宝贵的教育资源。
在本教程中,我们将探讨一种使用git钩子和Node.js自动生成和发布更新日志的方法。我们将使用名为常规提交的特定提交格式和名为Commitizen的工具,创建一个常规提交信息。然后,我们将使用一个名为 [standard-version](https://www.npmjs.com/package/standard-version)的库来自动生成一个更新日志和一个遵循语义版本的新版本。
最后,我们将使我们的更新日志在整个开发团队中共享,以便每个人在项目中遵循相同的惯例。如果你想跟随的话,你可以在这个GitHub仓库中找到最终的代码。
让我们开始吧!
在常规提交中构建提交信息
常规提交规范通过提供创建特定提交历史的规则来改进提交信息。传统的Commits通过创建一个使用语义版本的版本,使生成更新日志变得简单。
根据惯例,提交消息的结构应该如下。
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
我们来研究一下这个结构的细节。
<type> 是一种影响到版本号的提交类型。在语义版本管理中,fix 类型影响 PATCH,feat 类型影响 MINOR。还有其他的类型,然而这些并不影响版本号。
scope 是一个可选的名词,它描述了提交所改变或更新的代码库的部分。例如,在feat(pages) ,pages是范围。
在语义版本管理中,! 与MAJOR相关。当在范围之后使用时,! 表示提交中存在破坏性的修改。
<description> 是对代码所做修改的简要书面解释。例如,如果我们为feat(pages) 写一个描述,它可能看起来像下面这样。feat(pages): add contact page in the side menu 。
body 是一个可选的字段,你可以用它来更详细地描述该提交。body 必须在描述后一行开始。footer 也是一个可选的字段。例如,有一个页脚是BREAKING CHANGE ,它与语义版本管理中的 MAJOR 相关。
提交消息的例子
让我们看看不同提交信息的一些例子。
只有type 和description 的提交信息。
feat: add the charging option for cars
带有type,scope, 和description 的提交消息。
fix(homepage): change title width of title
带有BREAKING CHANGE 的提交消息。
refactor(api): remove the get api from reservations
BREAKING CHANGE: refactor to use the trip api instead of reservations api
创建我们的项目
让我们通过添加必要的工具来开始我们的项目,使我们的更新日志和发布自动化。首先,创建一个command 提示,在这里我们将添加以下代码块。
让我们创建一个基于npm的项目,并使其成为一个git仓库。如果你想自动化一个现有的仓库,你可以跳过这一步。
# create project directory
mkdir changelog
# cd into project
cd changelog
# initialize npm project
npm init -y
# initialize git
git init
上面的代码块将创建一个git仓库和一个v1.0.0的npm包。
在我们的项目中添加standard-version
现在,让我们开始为我们的项目创建版本吧你需要将standard-version npm包安装到你的项目中,如下所示。
npm install --save-dev standard-version
你还需要把它添加到npm脚本中。
...
"scripts": {
"release": "standard-version"
}
...
创建一个版本
创建一个名为new-feature 的假文件,并按如下方式提交。
touch new-feature
git add new-feature
git commit
添加以下git提交信息。
feat(new-feature): add a new-feature to our project
最后,让我们通过运行我们新添加的脚本在我们的项目中创建一个版本。
npm run release
运行上面的命令会在屏幕上显示以下信息。
> changelog@1.0.0 release /home/imsingh/Develop/inder/changelog
> standard-version
✔ bumping version in package.json from 1.0.0 to 1.1.0
✔ bumping version in package-lock.json from 1.0.0 to 1.1.0
✔ created CHANGELOG.md
✔ outputting changes to CHANGELOG.md
✔ committing package-lock.json and package.json and CHANGELOG.md
✔ tagging release v1.1.0
ℹ Run `git push --follow-tags origin master && npm publish` to publish
上面的消息做了以下工作。
- 将SemVer的版本号从
1.0.0增加到1.1.0我们增加了一个功能,因此,MINOR从0更新到1 - 创建一个
CHANGELOG.md文件,向其添加所需内容 - 提交上述修改,创建一个
v1.1.0标签 - 如果需要,打印出推送标签的信息,并将我们的包发布到npm上
CHANGELOG.md
现在,如果你打开CHANGELOG.md ,你会看到下面的代码块,其中包括上面的修改。
# Changelog
All notable changes to this project will be documented in this file. See \[standard-version\](https://github.com/conventional-changelog/standard-version) for commit guidelines.
## 1.1.0 (2021-07-12)
### Features
* **new-feature:** add a new-feature to our project 11c0322
你还会看到standard-release 创建的提交消息,它使用了git log -1 命令来进行发布。
commit #COMMIT_HASH (HEAD -> master, tag: v1.1.0)
Author: #AUTHOR_NAME <#AUTHOR_EMAIL>
Date: #COMMIT_DATE
chore(release): 1.1.0
提交消息的类型是chore ,范围是release ,description 是1.1.0 。
现在,你已经拥有了自动化更新日志和发布所需的一切!但是,手动编写提交信息是很繁琐的。然而,手动写提交是很繁琐的,而且容易出错。让我们引入一些工具来顺利完成这个过程吧
添加Commitizen
你可以用Commitizen来自动生成提交,而不是自己写传统的提交。Commitizen 会在command 提示中向你提问,并根据你的回答生成提交。
按如下步骤安装Commitizen软件包。
npm install --save-dev commitizen
现在,初始化Commitizen以使用传统的更新日志适配器。
npx commitizen init cz-conventional-changelog --save-dev --save-exact
适配器是一种配置,它告诉Commitizen在提示中显示不同种类的提交。目前,有多种适配器可用,但如果你愿意,也可以创建自己的适配器。
现在,为了使用Commitizen,我们要添加一个npm脚本。
...
"scripts": {
"commit": "cz"
}
...
在这一点上,你应该创建一个.gitignore 文件,并忽略node_modules 目录。
使用git add ,将package.json 和package-lock.json 添加到git暂存区。我们将通过运行下面的代码块进行提交。
npm run commit
上面的代码块也会提示你回答后面的指令。
type 显示了你可以从中选择的types 列表。下面的列表来自于我们之前安装的适配器。
? Select the type of change that you're committing:
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-col
ons, etc)
refactor: A code change that neither fixes a bug nor adds a feature
perf: A code change that improves performance
(Move up and down to reveal more choices)
scope ,在下面的代码块中,指的是常规提交的范围。
? What is the scope of this change (e.g. component or file name): (press enter to skip)
对于short description ,写出对常规提交的简要解释。
? Write a short, imperative tense description of the change (max 82 chars):
在longer description ,描述常规提交的body 。
? Provide a longer description of the change: (press enter to skip)
下面代码块中的两个问题产生了一个带有破坏性修改的提交。
? Are there any breaking changes?
? Describe the breaking changes:
在issues related to commit ,你可以参考GitHub、JIRA或其他类似工具中的问题。
? Does this change affect any open issues?
? Add issue references (e.g. "fix #123", "re #123".):
一旦你根据你的需要回答了这些提示,你就会有一个类似于下图的提交。
Author: #AUTHOR_NAME <#AUTHOR_EMAIL>
Date: Mon Jul 12 21:10:17 2021 +0200
feat(some-scope): a short description
a long description
BREAKING CHANGE: it breaks
123
添加commitlint来执行规则
为了确保我们项目的所有开发者都遵循相同的约定,我们将使用git钩子与Husky和commitlint。
安装所需工具
首先,让我们通过运行下面的代码块来安装 commitlint 和 Husky。
# Install commitlint cli and conventional config
npm install --save-dev @commitlint/config-conventional @commitlint/cli
# Install Husky
npm install husky --save-dev
配置commitlint
为了配置commitlint,我们需要创建一个名为commitlint.config.js 的配置文件,并添加以下代码。
module.exports = {extends: ['@commitlint/config-conventional']}
为了在提交前对信息进行润色,我们需要通过运行以下命令来使用Husky的commit-msg 钩。
# Activate hooks
npx husky install
# Add hook
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit "$1"'
你可以将husky install 作为npm准备脚本添加,然而这一步是可选的。husky install 将确保每个使用此 repo 的开发者在使用该项目之前都会安装Husky Hooks。
...
"scripts": {
...
"prepare": "husky install"
}
我们仍然会使用git commit来使我们的提交遵循前面描述的惯例。如果git消息中存在错误,commitlint将引发以下错误。
git commit -m "This is a commit"
⧗ input: This is a commit
✖ subject may not be empty [subject-empty]
✖ type may not be empty [type-empty]
✖ found 2 problems, 0 warnings
ⓘ Get help: \[https://github.com/conventional-changelog/commitlint/#what-is-commitlint\](https://github.com/conventional-changelog/commitlint/#what-is-commitlint)
husky - commit-msg hook exited with code 1 (error)
管理发布的最终工作流程
为了管理你的发布,你可以遵循下面列出的工作流程。
- 创建你的特性并提交它们。如果提交信息没有遵循惯例,commitlint将引发错误
- 在命令行中执行
npm run commit,用Commitizen进行提交 - 运行
npm run release,创建一个更新日志和一个基于语义的版本。
要使用CI/CD创建一个版本,请看语义发布。
总结
在这篇文章中,你学到了如何使用git钩子和Node.js创建一个自动更新日志和一个基于语义的版本发布。我们使用常规提交规范创建了我们的提交信息,然后使用commitizen和standard-release 。接下来,我们使用commitlint和Husky来自动编写我们的提交。
The postAutomatically generate and release a changelog using Node.jsappeared first onLogRocket Blog.