Skip to content

Commit

Permalink
feat(commit-lint): 添加commit-lint插件
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Jan 28, 2021
1 parent 45d9dec commit 4832cb3
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 5 deletions.
11 changes: 11 additions & 0 deletions packages/cli-plugin-command-commit-lint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# `cli`

> TODO: description
## Usage

```
const cli = require('cli');
// TODO: DEMONSTRATE API
```
36 changes: 36 additions & 0 deletions packages/cli-plugin-command-commit-lint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "@xus/cli-plugin-command-commit-lint",
"version": "0.0.1",
"description": "xus cli plugin command-commit-lint",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"keywords": [
"cli",
"commit-lint"
],
"author": "guo.xu <xuguo@outlook.it>",
"homepage": "https://github.com/xus-code/bundle-tools/tree/master/packages/cli#readme",
"license": "MIT",
"files": [
"dist"
],
"repository": {
"type": "git",
"url": "git+https://github.com/xus-code/bundle-tools.git"
},
"scripts": {
"dev:cl": "tsc --watch",
"build:cl": "tsc"
},
"bugs": {
"url": "https://github.com/xus-code/bundle-tools/issues"
},
"dependencies": {
"@xus/cli": "^0.0.1",
"chalk": "^4.1.0",
"fs-extra": "^9.1.0"
},
"devDependencies": {
"@types/fs-extra": "^9.0.6"
}
}
4 changes: 4 additions & 0 deletions packages/cli-plugin-command-commit-lint/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const HuskyGitParamsEnv = 'HUSKY_GIT_PARAMS'

export const commitRE = /^(revert: )?(fix|feat|docs|perf|test|types|style|build|chore|refactor|ci|wip|breaking change)(\(.+\))?: .{1,50}/
export const mergeRE = /Merge /
52 changes: 52 additions & 0 deletions packages/cli-plugin-command-commit-lint/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { IPluginAPI } from '@xus/cli'
import { HuskyGitParamsEnv, commitRE, mergeRE } from './constants'
import { readFileSync } from 'fs-extra'
import chalk from 'chalk'

export default function (api: IPluginAPI): void {
api.registerCommand(
'commit-lint',
{
usage: 'xus commit-lint',
desc: 'help you to validate commit message'
},
() => {
const gitParams = api.EnvManager.getEnv(HuskyGitParamsEnv)
if (gitParams) {
const commitMsg = readFileSync(gitParams, 'utf-8').trim()
if (!commitRE.test(commitMsg) && !mergeRE.test(commitMsg)) {
console.info(`invalid commit message:
"${chalk.red(commitMsg)}".
Proper commit message format is required for automated changelog generation.
Examples:
- fix(Button): incorrect style
- feat(Button): incorrect style
- docs(Button): fix typo
Allowed Types:
- fix (修复bug)
- feat (新特性)
- docs (文档相关)
- perf (性能优化)
- test (测试相关)
- types (ts类型相关)
- build (打包构建相关)
- chore (其他类型)
- ci (CI/CD相关)
- wip (开发中间态)
- refactor (代码重构)
- breaking change (重大变更)
- Merge branch 'foo' into 'bar' (分支合并)
`)
process.exit(1)
}
}
console.log(chalk.red(`no HUSKY_GIT_PARAMS`))
process.exit(1)
}
)
}
7 changes: 7 additions & 0 deletions packages/cli-plugin-command-commit-lint/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"outDir": "./dist"
},
"extends": "../../tsconfig.json",
"include": ["./src/**/*"]
}
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"url": "https://github.com/xus-code/bundle-tools/issues"
},
"dependencies": {
"@xus/cli-plugin-command-commit-lint": "^0.0.1",
"@xus/cli-plugin-command-help": "^0.0.1",
"chalk": "^4.1.0",
"joi": "^17.3.0",
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/src/Cli.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// types
import { Commands, Args, RawArgs, Plugin, PluginApply } from './types'
import { error, loadModule } from './utils'
import { error, loadModule, builtInMap } from './utils'
import ConfigManager, { IConfigManager } from './manager/ConfigManager'
import PathManager, { IPathManager } from './manager/PathManager'
import EnvManager, { IEnvManager } from './manager/EnvManager'
Expand Down Expand Up @@ -52,7 +52,6 @@ class Cli {
}
}
const builtInPlugins = []
const builtInMap = ['@xus/cli-plugin-command-help']
for (const pkgName of builtInMap) {
const plugin = await toPlugin(pkgName)
if (plugin) {
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/types/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export interface Plugin {

export type CommandFn = (args: Args, rawArgs: RawArgs) => any | Promise<any>
export interface CommandOps {
desc: string
usage: string
options: {
desc?: string
usage?: string
options?: {
[key: string]: string
}
}
Expand Down
5 changes: 5 additions & 0 deletions packages/cli/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ export enum EnvEnum {
context = 'XUS_CLI_CONTEXT',
mode = 'XUS_CLI_MODE'
}

export const builtInMap = [
'@xus/cli-plugin-command-help',
'@xus/cli-plugin-command-commit-lint'
]
36 changes: 36 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,13 @@
resolved "https://npm.c2cloud.cn/@sideway/pinpoint/download/@sideway/pinpoint-2.0.0.tgz#cff8ffadc372ad29fd3f78277aeb29e632cc70df"
integrity sha1-z/j/rcNyrSn9P3gneusp5jLMcN8=

"@types/fs-extra@^9.0.6":
version "9.0.6"
resolved "https://npm.c2cloud.cn/@types/fs-extra/download/@types/fs-extra-9.0.6.tgz#488e56b77299899a608b8269719c1d133027a6ab"
integrity sha1-SI5Wt3KZiZpgi4JpcZwdEzAnpqs=
dependencies:
"@types/node" "*"

"@types/glob@^7.1.1":
version "7.1.3"
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.3.tgz#e6ba80f36b7daad2c685acd9266382e68985c183"
Expand Down Expand Up @@ -1316,6 +1323,11 @@ asynckit@^0.4.0:
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha1-x57Zf380y48robyXkLzDZkdLS3k=

at-least-node@^1.0.0:
version "1.0.0"
resolved "https://npm.c2cloud.cn/at-least-node/download/at-least-node-1.0.0.tgz#602cd4b46e844ad4effc92a8011a3c46e0238dc2"
integrity sha1-YCzUtG6EStTv/JKoARo8RuAjjcI=

atob-lite@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/atob-lite/-/atob-lite-2.0.0.tgz#0fef5ad46f1bd7a8502c65727f0367d5ee43d696"
Expand Down Expand Up @@ -2550,6 +2562,16 @@ fs-extra@^8.1.0:
jsonfile "^4.0.0"
universalify "^0.1.0"

fs-extra@^9.1.0:
version "9.1.0"
resolved "https://npm.c2cloud.cn/fs-extra/download/fs-extra-9.1.0.tgz#5954460c764a8da2094ba3554bf839e6b9a7c86d"
integrity sha1-WVRGDHZKjaIJS6NVS/g55rmnyG0=
dependencies:
at-least-node "^1.0.0"
graceful-fs "^4.2.0"
jsonfile "^6.0.1"
universalify "^2.0.0"

fs-minipass@^1.2.5:
version "1.2.7"
resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-1.2.7.tgz#ccff8570841e7fe4265693da88936c55aed7f7c7"
Expand Down Expand Up @@ -3382,6 +3404,15 @@ jsonfile@^4.0.0:
optionalDependencies:
graceful-fs "^4.1.6"

jsonfile@^6.0.1:
version "6.1.0"
resolved "https://npm.c2cloud.cn/jsonfile/download/jsonfile-6.1.0.tgz#bc55b2634793c679ec6403094eb13698a6ec0aae"
integrity sha1-vFWyY0eTxnnsZAMJTrE2mKbsCq4=
dependencies:
universalify "^2.0.0"
optionalDependencies:
graceful-fs "^4.1.6"

jsonparse@^1.2.0:
version "1.3.1"
resolved "https://registry.yarnpkg.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280"
Expand Down Expand Up @@ -5637,6 +5668,11 @@ universalify@^0.1.0:
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==

universalify@^2.0.0:
version "2.0.0"
resolved "https://npm.c2cloud.cn/universalify/download/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717"
integrity sha1-daSYTv7cSwiXXFrrc/Uw0C3yVxc=

unset-value@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/unset-value/-/unset-value-1.0.0.tgz#8376873f7d2335179ffb1e6fc3a8ed0dfc8ab559"
Expand Down

0 comments on commit 4832cb3

Please sign in to comment.