关于vue.js:VUE-30-源码-scriptsverifyCommitjs-文件-对git提交时输入的描述信息进行规范

4次阅读

共计 1825 个字符,预计需要花费 5 分钟才能阅读完成。

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

当咱们在命令行敲下 git commit -m “ 形容信息 ” 提交代码时,此文件会被触发,要害的配置信息还是在 package.json 这个文件中,具体如下:


知识点 -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 源码正在解析中,欢送捧场!

欢送 增加我集体微信 进行交换。

正文完
 0