Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add custom regex validation and error message display #2

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 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 -p .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 |
| ------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| -p --commitMessagePath \<path\> | The path of the temporary file to which the git message is submitted. The git hook commit-msg will pass this parameter |
| -r --commitMessageRe \<reg\> | Validate the regular of whether the commit message passes |
| -e --errorMessage \<message\> | Validation failed to display error messages |
| -w --warningMessage \<message\> | Validation failed to display warning messages |

### Custom Handle

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

interface CommitLintCommandOptions {
commitMessagePath: string
commitMessageRe?: string | RegExp
errorMessage?: string
warningMessage?: string
}
function commitLint(options: CommitLintCommandOptions): void
```

## License
Expand Down
28 changes: 19 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 -p .git/COMMIT_EDITMSG
```

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

#### lint-commit

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

### 自定义处理

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

interface CommitLintCommandOptions {
commitMessagePath: string
commitMessageRe?: string | RegExp
errorMessage?: string
warningMessage?: string
}
function commitLint(options: CommitLintCommandOptions): 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('-r --commitMessageRe <reg>', 'Validate the regular of whether the commit message passes')
.option('-e --errorMessage <message>', 'Validation failed to display error messages')
.option('-w --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)
}
}