Skip to content

Commit

Permalink
feat: add custom regex validation and error message display
Browse files Browse the repository at this point in the history
  • Loading branch information
a145789 committed Dec 11, 2023
1 parent 30e3ffb commit f6dc9c3
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 36 deletions.
21 changes: 12 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ pnpm add @varlet/release -D
npx vr release

# Specify remote name
npx vr release -r <remote>
npx vr release -r https://github.com/varletjs/varlet-release
# or
npx vr release --remote <remote>
npx vr release --remote https://github.com/varletjs/varlet-release

# Just generate changelogs
npx vr changelog

# Specify changelog filename
npx vr changelog -f <filename>
npx vr changelog -f changelog.md
# or
npx vr changelog --file <filename>
npx vr changelog --file changelog.md

# Lint commit message
npx vr lint-commit <gitMessagePath>
npx vr lint-commit .git/COMMIT_EDITMSG
```

### Configuration
Expand All @@ -75,9 +75,12 @@ npx vr lint-commit <gitMessagePath>

#### lint-commit

| Params | Instructions |
| ------------------ | ---------------------------------------------------------------------------------------------------------------------- |
| \<gitMessagePath\> | The path of the temporary file to which the git message is submitted. The git hook commit-msg will pass this parameter |
| Params | Instructions |
| -------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| \<commitMessagePath\> | The path of the temporary file to which the git message is submitted. The git hook commit-msg will pass this parameter |
| -cmr --commitMessageRe \<reg\> | Validate the regular of whether the commit message passes |
| -em --errorMessage \<message\> | Validation failed to display error messages |
| -wm --warningMessage \<message\> | Validation failed to display warning messages |

### Custom Handle

Expand Down Expand Up @@ -117,7 +120,7 @@ interface ChangelogCommandOptions {
releaseCount?: number
}
function changelog({ releaseCount, file }?: ChangelogCommandOptions): Promise<void>
function commitLint(gitMessagePath: string): void
function commitLint(commitMessagePath: string): void
```

## License
Expand Down
21 changes: 12 additions & 9 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ pnpm add @varlet/release -D
npx vr release

# 指定远程仓库名称
npx vr release -r <remote>
npx vr release -r https://github.com/varletjs/varlet-release
# or
npx vr release --remote <remote>
npx vr release --remote https://github.com/varletjs/varlet-release

# 仅生成变更日志
npx vr changelog

# 指定变更日志文件名
npx vr changelog -f <filename>
npx vr changelog -f changelog.md
# or
npx vr changelog --file <filename>
npx vr changelog --file changelog.md

# 检测 commit message
npx vr lint-commit <gitMessagePath>
npx vr lint-commit .git/COMMIT_EDITMSG
```

### 配置
Expand All @@ -75,9 +75,12 @@ npx vr lint-commit <gitMessagePath>

#### lint-commit

| 参数 | 说明 |
| ------------------ | --------------------------------------------------------------------------- |
| \<gitMessagePath\> | 提交 `git message` 的临时文件路径。`git` 钩子 `commit-msg` 会传递这个参数。 |
| 参数 | 说明 |
| -------------------------------- | --------------------------------------------------------------------------- |
| \<commitMessagePath\> | 提交 `git message` 的临时文件路径。`git` 钩子 `commit-msg` 会传递这个参数。 |
| -cmr --commitMessageRe \<reg\> | 验证 `commit message` 是否通过的正则 |
| -em --errorMessage \<message\> | 验证失败展示的错误信息 |
| -wm --warningMessage \<message\> | 验证失败展示的提示信息 |

### 自定义处理

Expand Down Expand Up @@ -117,7 +120,7 @@ interface ChangelogCommandOptions {
releaseCount?: number
}
function changelog({ releaseCount, file }?: ChangelogCommandOptions): Promise<void>
function commitLint(gitMessagePath: string): void
function commitLint(commitMessagePath: string): void
```

## License
Expand Down
6 changes: 5 additions & 1 deletion bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ program
.action(async (options) => changelog(options))

program
.command('commit-lint <gitMessagePath>')
.command('commit-lint')
.option('-p --commitMessagePath <commitMessagePath>', 'Git commit message path')
.option('-cmr --commitMessageRe <reg>', 'Validate the regular of whether the commit message passes')
.option('-em --errorMessage <message>', 'Validation failed to display error messages')
.option('-wm --warningMessage <message>', 'Validation failed to display warning messages')
.description('Lint commit message')
.action(async (option) => commitLint(option))

Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -78,4 +79,4 @@
"picocolors": "^1.0.0",
"semver": "^7.5.4"
}
}
}
48 changes: 33 additions & 15 deletions src/commitLint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
}
}

0 comments on commit f6dc9c3

Please sign in to comment.