1

VUE 3.0 源码 scripts/verifyCommit.js 文件 对git提交时输入的描述信息进行规范

 2 years ago
source link: https://segmentfault.com/a/1190000041141684
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

VUE 3.0 源码 scripts/verifyCommit.js 文件 对git提交时输入的描述信息进行规范

发布于 12 月 18 日

文件路径:VUE 3.0 源码 scripts/verifyCommit.js

当我们在命令行敲下 git commit -m "描述信息" 提交代码时,此文件会被触发,关键的配置信息还是在 package.json 这个文件中,具体如下:

image.png


知识点-1:如何拿到咱们输入的描述信息?

答案-1:通过 process.env.GIT_PARAMS 读取到git保存描述信息的文件,一般路径如下:.git/COMMIT_EDITMSG


知识点-2:输入描述信息的格式是怎么把控呢?

答案-2:通过 一个看起来很NB但是很容易理解的一个正则表达式实现的,具体如下:

/^(revert: )?(feat|fix|docs|dx|style|refactor|perf|test|workflow|build|ci|chore|types|wip|release)(\(.+\))?: .{1,50}/

大概分为以下3部分:

(1) 开头固定关键字 - 用于标识此次提交的概括信息:

revert|feat|fix|docs|dx|style|refactor|perf|test|workflow|build|ci|chore|types|wip|release

(2)括号+冒号+空格 (括号内一般描述修改模块的名称) --> fix(模块名):_

(3)输入一些描述信息 这句正则感觉意义不是太大 .{1,50}

正则小数点 . : 可以匹配除了换行符(/n)以外的任意一个字符
正则{m,n} : 表达式至少重复m次,最多重复n次,比如:"ba{1,3}"可以匹配 "ba"或"baa"或"baaa"

具体源码如下:

// Invoked on the commit-msg git hook by yorkie.

/** 控制台日志标注样式 */
const chalk = require('chalk')

/**
 * 通过 GIT_PARAMS 读取到保存 git commit 时输入的描述信息的文件目录,一般路径如下:.git/COMMIT_EDITMSG
 */
const msgPath = process.env.GIT_PARAMS

/** 读取.git/COMMIT_EDITMSG文件信息 */
const msg = require('fs')
  .readFileSync(msgPath, 'utf-8')
  .trim()

/**
 * 校验提交信息格式
 * 示例:fix(runtime-core): check if the key is string on undefined property warning (#1731)
 * part1 - 开头关键字:revert|feat|fix|docs|dx|style|refactor|perf|test|workflow|build|ci|chore|types|wip|release
 * part2 - 括号+冒号+空格 (括号内一般描述修改模块的名称)
 * part3 - 输入一些描述信息 .{1,50}
 */
const commitRE = /^(revert: )?(feat|fix|docs|dx|style|refactor|perf|test|workflow|build|ci|chore|types|wip|release)(\(.+\))?: .{1,50}/

if (!commitRE.test(msg)) {
  console.log()
  console.error(
    `  ${chalk.bgRed.white(' ERROR ')} ${chalk.red(
      `invalid commit message format.`
    )}\n\n` +
      chalk.red(
        `  Proper commit message format is required for automated changelog generation. Examples:\n\n`
      ) +
      `    ${chalk.green(`feat(compiler): add 'comments' option`)}\n` +
      `    ${chalk.green(
        `fix(v-model): handle events on blur (close #28)`
      )}\n\n` +
      chalk.red(`  See .github/commit-convention.md for more details.\n`)
  )
  process.exit(1)
}

如果您对 “前端源码” 情有独钟,可以微信扫码关注下面的公众号二维码,内容一直持续更新中
当前 VUE3.0 源码正在解析中,欢迎捧场!

公众号:前端源码解析

欢迎添加我个人微信进行交流。

老罗-个人微信


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK