From f6dc9c3ebb05af25d3aa01ce2a51c776daeac7c9 Mon Sep 17 00:00:00 2001 From: a145789 <2091927351@qq.com> Date: Mon, 11 Dec 2023 13:43:21 +0800 Subject: [PATCH 1/2] feat: add custom regex validation and error message display --- README.md | 21 ++++++++++++--------- README.zh-CN.md | 21 ++++++++++++--------- bin/index.js | 6 +++++- package.json | 5 +++-- src/commitLint.ts | 48 ++++++++++++++++++++++++++++++++--------------- 5 files changed, 65 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 4af372d..1b4f827 100644 --- a/README.md +++ b/README.md @@ -42,20 +42,20 @@ pnpm add @varlet/release -D npx vr release # Specify remote name -npx vr release -r +npx vr release -r https://github.com/varletjs/varlet-release # or -npx vr release --remote +npx vr release --remote https://github.com/varletjs/varlet-release # Just generate changelogs npx vr changelog # Specify changelog filename -npx vr changelog -f +npx vr changelog -f changelog.md # or -npx vr changelog --file +npx vr changelog --file changelog.md # Lint commit message -npx vr lint-commit +npx vr lint-commit .git/COMMIT_EDITMSG ``` ### Configuration @@ -75,9 +75,12 @@ npx vr lint-commit #### lint-commit -| Params | Instructions | -| ------------------ | ---------------------------------------------------------------------------------------------------------------------- | -| \ | The path of the temporary file to which the git message is submitted. The git hook commit-msg will pass this parameter | +| Params | Instructions | +| -------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | +| \ | The path of the temporary file to which the git message is submitted. The git hook commit-msg will pass this parameter | +| -cmr --commitMessageRe \ | Validate the regular of whether the commit message passes | +| -em --errorMessage \ | Validation failed to display error messages | +| -wm --warningMessage \ | Validation failed to display warning messages | ### Custom Handle @@ -117,7 +120,7 @@ interface ChangelogCommandOptions { releaseCount?: number } function changelog({ releaseCount, file }?: ChangelogCommandOptions): Promise -function commitLint(gitMessagePath: string): void +function commitLint(commitMessagePath: string): void ``` ## License diff --git a/README.zh-CN.md b/README.zh-CN.md index eb0abe5..7c23662 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -42,20 +42,20 @@ pnpm add @varlet/release -D npx vr release # 指定远程仓库名称 -npx vr release -r +npx vr release -r https://github.com/varletjs/varlet-release # or -npx vr release --remote +npx vr release --remote https://github.com/varletjs/varlet-release # 仅生成变更日志 npx vr changelog # 指定变更日志文件名 -npx vr changelog -f +npx vr changelog -f changelog.md # or -npx vr changelog --file +npx vr changelog --file changelog.md # 检测 commit message -npx vr lint-commit +npx vr lint-commit .git/COMMIT_EDITMSG ``` ### 配置 @@ -75,9 +75,12 @@ npx vr lint-commit #### lint-commit -| 参数 | 说明 | -| ------------------ | --------------------------------------------------------------------------- | -| \ | 提交 `git message` 的临时文件路径。`git` 钩子 `commit-msg` 会传递这个参数。 | +| 参数 | 说明 | +| -------------------------------- | --------------------------------------------------------------------------- | +| \ | 提交 `git message` 的临时文件路径。`git` 钩子 `commit-msg` 会传递这个参数。 | +| -cmr --commitMessageRe \ | 验证 `commit message` 是否通过的正则 | +| -em --errorMessage \ | 验证失败展示的错误信息 | +| -wm --warningMessage \ | 验证失败展示的提示信息 | ### 自定义处理 @@ -117,7 +120,7 @@ interface ChangelogCommandOptions { releaseCount?: number } function changelog({ releaseCount, file }?: ChangelogCommandOptions): Promise -function commitLint(gitMessagePath: string): void +function commitLint(commitMessagePath: string): void ``` ## License diff --git a/bin/index.js b/bin/index.js index cd6e921..4e9ec8f 100644 --- a/bin/index.js +++ b/bin/index.js @@ -18,7 +18,11 @@ program .action(async (options) => changelog(options)) program - .command('commit-lint ') + .command('commit-lint') + .option('-p --commitMessagePath ', 'Git commit message path') + .option('-cmr --commitMessageRe ', 'Validate the regular of whether the commit message passes') + .option('-em --errorMessage ', 'Validation failed to display error messages') + .option('-wm --warningMessage ', 'Validation failed to display warning messages') .description('Lint commit message') .action(async (option) => commitLint(option)) diff --git a/package.json b/package.json index c40b491..37105e9 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ ], "scripts": { "preinstall": "npx only-allow pnpm", + "postinstall": "simple-git-hooks", "dev": "tsup --watch", "build": "tsup", "release": "pnpm build && node bin/index.js release", @@ -41,7 +42,7 @@ }, "simple-git-hooks": { "pre-commit": "pnpm exec lint-staged --allow-empty --concurrent false", - "commit-msg": "pnpm run commit-lint $1" + "commit-msg": "pnpm run commit-lint -p $1" }, "lint-staged": { "*.{ts,tsx,js,vue,less}": "prettier --write", @@ -78,4 +79,4 @@ "picocolors": "^1.0.0", "semver": "^7.5.4" } -} \ No newline at end of file +} diff --git a/src/commitLint.ts b/src/commitLint.ts index e574cec..f495177 100644 --- a/src/commitLint.ts +++ b/src/commitLint.ts @@ -7,20 +7,8 @@ const { readFileSync } = fse export const COMMIT_MESSAGE_RE = /^(revert|fix|feat|docs|perf|test|types|style|build|chore|release|refactor)(\(.+\))?!?: (.|\n)+/ -export function isVersionCommitMessage(message: string) { - return message.startsWith('v') && semver.valid(message.slice(1)) -} - -export function getCommitMessage(gitMessagePath: string) { - return readFileSync(gitMessagePath, 'utf-8').trim() -} - -export function commitLint(gitMessagePath: string) { - const commitMessage = getCommitMessage(gitMessagePath) - - if (!isVersionCommitMessage(commitMessage) && !COMMIT_MESSAGE_RE.test(commitMessage)) { - logger.error(`Commit message invalid`) - logger.warning(`\ +const ERROR_MESSAGE = 'Commit message invalid.' +const WARNING_MESSAGE = `\ The rules for commit messages are as follows Example: @@ -49,7 +37,37 @@ Allowed types: - revert Commit message reference: https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y -参考阮一峰Commit message编写指南: https://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html`) +参考阮一峰Commit message编写指南: https://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html` + +export function isVersionCommitMessage(message: string) { + return message.startsWith('v') && semver.valid(message.slice(1)) +} + +export function getCommitMessage(commitMessagePath: string) { + return readFileSync(commitMessagePath, 'utf-8').trim() +} + +export interface CommitLintCommandOptions { + commitMessagePath: string + commitMessageRe?: string | RegExp + errorMessage?: string + warningMessage?: string +} + +export function commitLint(options: CommitLintCommandOptions) { + const { + commitMessagePath, + commitMessageRe = COMMIT_MESSAGE_RE, + errorMessage = ERROR_MESSAGE, + warningMessage = WARNING_MESSAGE, + } = options + + const commitMessage = getCommitMessage(commitMessagePath) + const isValidCommitMessage = new RegExp(commitMessageRe).test(commitMessage) + + if (!isVersionCommitMessage(commitMessage) && !isValidCommitMessage) { + logger.error(errorMessage) + logger.warning(warningMessage) process.exit(1) } } From 76aab5d185a71d46ce7820b5a50b587ba6c5ae97 Mon Sep 17 00:00:00 2001 From: a145789 <2091927351@qq.com> Date: Tue, 12 Dec 2023 09:57:05 +0800 Subject: [PATCH 2/2] refactor: optimize command parameters --- README.md | 23 +++++++++++++++-------- README.zh-CN.md | 23 +++++++++++++++-------- bin/index.js | 6 +++--- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 1b4f827..b8017ae 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ npx vr changelog -f changelog.md npx vr changelog --file changelog.md # Lint commit message -npx vr lint-commit .git/COMMIT_EDITMSG +npx vr lint-commit -p .git/COMMIT_EDITMSG ``` ### Configuration @@ -75,12 +75,12 @@ npx vr lint-commit .git/COMMIT_EDITMSG #### lint-commit -| Params | Instructions | -| -------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | -| \ | The path of the temporary file to which the git message is submitted. The git hook commit-msg will pass this parameter | -| -cmr --commitMessageRe \ | Validate the regular of whether the commit message passes | -| -em --errorMessage \ | Validation failed to display error messages | -| -wm --warningMessage \ | Validation failed to display warning messages | +| Params | Instructions | +| ------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | +| -p --commitMessagePath \ | The path of the temporary file to which the git message is submitted. The git hook commit-msg will pass this parameter | +| -r --commitMessageRe \ | Validate the regular of whether the commit message passes | +| -e --errorMessage \ | Validation failed to display error messages | +| -w --warningMessage \ | Validation failed to display warning messages | ### Custom Handle @@ -120,7 +120,14 @@ interface ChangelogCommandOptions { releaseCount?: number } function changelog({ releaseCount, file }?: ChangelogCommandOptions): Promise -function commitLint(commitMessagePath: string): void + +interface CommitLintCommandOptions { + commitMessagePath: string + commitMessageRe?: string | RegExp + errorMessage?: string + warningMessage?: string +} +function commitLint(options: CommitLintCommandOptions): void ``` ## License diff --git a/README.zh-CN.md b/README.zh-CN.md index 7c23662..414de8f 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -55,7 +55,7 @@ npx vr changelog -f changelog.md npx vr changelog --file changelog.md # 检测 commit message -npx vr lint-commit .git/COMMIT_EDITMSG +npx vr lint-commit -p .git/COMMIT_EDITMSG ``` ### 配置 @@ -75,12 +75,12 @@ npx vr lint-commit .git/COMMIT_EDITMSG #### lint-commit -| 参数 | 说明 | -| -------------------------------- | --------------------------------------------------------------------------- | -| \ | 提交 `git message` 的临时文件路径。`git` 钩子 `commit-msg` 会传递这个参数。 | -| -cmr --commitMessageRe \ | 验证 `commit message` 是否通过的正则 | -| -em --errorMessage \ | 验证失败展示的错误信息 | -| -wm --warningMessage \ | 验证失败展示的提示信息 | +| 参数 | 说明 | +| ------------------------------- | --------------------------------------------------------------------------- | +| -p --commitMessagePath \ | 提交 `git message` 的临时文件路径。`git` 钩子 `commit-msg` 会传递这个参数。 | +| -r --commitMessageRe \ | 验证 `commit message` 是否通过的正则 | +| -e --errorMessage \ | 验证失败展示的错误信息 | +| -w --warningMessage \ | 验证失败展示的提示信息 | ### 自定义处理 @@ -120,7 +120,14 @@ interface ChangelogCommandOptions { releaseCount?: number } function changelog({ releaseCount, file }?: ChangelogCommandOptions): Promise -function commitLint(commitMessagePath: string): void + +interface CommitLintCommandOptions { + commitMessagePath: string + commitMessageRe?: string | RegExp + errorMessage?: string + warningMessage?: string +} +function commitLint(options: CommitLintCommandOptions): void ``` ## License diff --git a/bin/index.js b/bin/index.js index 4e9ec8f..e7fdf93 100644 --- a/bin/index.js +++ b/bin/index.js @@ -20,9 +20,9 @@ program program .command('commit-lint') .option('-p --commitMessagePath ', 'Git commit message path') - .option('-cmr --commitMessageRe ', 'Validate the regular of whether the commit message passes') - .option('-em --errorMessage ', 'Validation failed to display error messages') - .option('-wm --warningMessage ', 'Validation failed to display warning messages') + .option('-r --commitMessageRe ', 'Validate the regular of whether the commit message passes') + .option('-e --errorMessage ', 'Validation failed to display error messages') + .option('-w --warningMessage ', 'Validation failed to display warning messages') .description('Lint commit message') .action(async (option) => commitLint(option))