diff --git a/README.md b/README.md index 4af372d..b8017ae 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 -p .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 | +| ------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | +| -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 @@ -117,7 +120,14 @@ interface ChangelogCommandOptions { releaseCount?: number } function changelog({ releaseCount, file }?: ChangelogCommandOptions): Promise -function commitLint(gitMessagePath: 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 eb0abe5..414de8f 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 -p .git/COMMIT_EDITMSG ``` ### 配置 @@ -75,9 +75,12 @@ npx vr lint-commit #### lint-commit -| 参数 | 说明 | -| ------------------ | --------------------------------------------------------------------------- | -| \ | 提交 `git message` 的临时文件路径。`git` 钩子 `commit-msg` 会传递这个参数。 | +| 参数 | 说明 | +| ------------------------------- | --------------------------------------------------------------------------- | +| -p --commitMessagePath \ | 提交 `git message` 的临时文件路径。`git` 钩子 `commit-msg` 会传递这个参数。 | +| -r --commitMessageRe \ | 验证 `commit message` 是否通过的正则 | +| -e --errorMessage \ | 验证失败展示的错误信息 | +| -w --warningMessage \ | 验证失败展示的提示信息 | ### 自定义处理 @@ -117,7 +120,14 @@ interface ChangelogCommandOptions { releaseCount?: number } function changelog({ releaseCount, file }?: ChangelogCommandOptions): Promise -function commitLint(gitMessagePath: 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 cd6e921..e7fdf93 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('-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)) 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) } }