Skip to content

Commit 1c1c5dd

Browse files
committed
✨ refactor(feature): 调整commit启动询问方式,进入ajv参数校验
1 parent 1094667 commit 1c1c5dd

File tree

7 files changed

+148
-41
lines changed

7 files changed

+148
-41
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ codeg commit -t fix -s feat -d 修复xx功能的xxBug
4343
| -t, --type \<type\> | 添加修改类型 |
4444
| -s, --scope \<scope\> | 填写修改范围 |
4545
| -d, --description \<description\> | 填写修改描述 |
46+
| -a, --ask | 启用询问模式 |
4647

4748
### 询问模式
4849

4950
```bash
5051
# 启动询问模式
51-
codeg commit
52+
codeg commit --ask
5253
```
5354

5455
```
@@ -419,6 +420,7 @@ codeg format
419420
# 格式化 src 和 components 文件夹下的文件
420421
codeg format -p ./src -p ./components
421422
```
423+
422424
| 选项 | 描述 |
423425
| ------------------------- | ------------ |
424426
| -p, --pattern \<pattern\> | 设置匹配规则 |

package-lock.json

Lines changed: 104 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"unbuild": "^2.0.0-rc.0"
7676
},
7777
"dependencies": {
78+
"ajv": "^8.12.0",
7879
"cac": "^6.7.14",
7980
"enquirer": "^2.4.1",
8081
"eslint": "^8.50.0",

src/cli.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import cac from "cac";
22

3-
import { cmdInstaller, handleError } from "@/helper";
3+
import { handleError, setup } from "@/helper";
44
import { plugins } from "@/index";
55

66
import pkg from "../package.json";
77

88
const setupCli = async () => {
99
const cli = cac("codeg");
1010

11-
await cmdInstaller(cli, plugins);
11+
await setup(cli, plugins);
1212

1313
cli.help();
1414
cli.version(pkg.version);

src/command/git-commit.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
import { performance } from "node:perf_hooks";
2-
1+
import Ajv from "ajv";
32
import type { CAC } from "cac";
43
import enquirer from "enquirer";
54

65
import { ACTIVATION, gitCommitScopes, gitCommitTypes } from "@/config";
76
import { execCommand, loadConfigModule, loggerInfo } from "@/helper";
87
import { GitCommitOptions } from "@/types";
98

9+
const schema = {
10+
type: "object",
11+
properties: {
12+
type: { type: "string" },
13+
scope: { type: "string" },
14+
description: { type: "string" },
15+
},
16+
required: ["type", "scope", "description"],
17+
};
18+
1019
const mergeConfig = async () => {
1120
const config = await loadConfigModule();
1221
const commands = config && config?.commands;
@@ -60,6 +69,7 @@ const generateEnquirer = async (): Promise<
6069
name: "description",
6170
type: "text",
6271
message: "请输入提交描述",
72+
required: true,
6373
},
6474
{
6575
name: "emoji",
@@ -88,6 +98,17 @@ export const gitCommit = async (
8898
})}`,
8999
);
90100
}
101+
102+
const ajv = new Ajv();
103+
const validate = ajv.compile(schema);
104+
const valid = validate({
105+
type,
106+
scope,
107+
description,
108+
});
109+
if (!valid && validate.errors && validate.errors?.length > 0) {
110+
throw new Error(validate.errors[0].message);
111+
}
91112
await execCommand(
92113
"git",
93114
["commit", "-m", `${type}(${scope}): ${description}`],
@@ -104,18 +125,15 @@ export default function gitCommitInstaller(cli: CAC) {
104125
.option("-t, --type <type>", "添加修改类型")
105126
.option("-s, --scope <scope>", "填写修改范围")
106127
.option("-d, --description <description>", "填写修改描述")
128+
.option("-a, --ask", "启用询问模式")
107129
.action(async (options) => {
108-
const start = performance.now();
109-
110-
const { type, scope, description } = options;
111-
if (!type || !scope || !description) {
130+
const { type, scope, description, ask } = options;
131+
if (ask) {
112132
const result = await generateEnquirer();
113133
await gitCommit(result.type, result.scope, result.description);
114134
} else {
115135
await gitCommit(type, scope, description);
116136
}
117-
const getTime = () => `${(performance.now() - start).toFixed(2)}ms`;
118-
loggerInfo(`😁 commit 命令执行结束, 共用时: ${getTime()}`);
119137
});
120138
},
121139
};

src/command/root.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const root = async () => {
2828
choices: commandChoices,
2929
},
3030
]);
31-
await execCommand("codeg", [result.command], { stdio: "inherit" });
31+
await execCommand("codeg", [result.command, "--ask"], { stdio: "inherit" });
3232
};
3333

3434
export default function rootInstaller(cli: CAC) {

0 commit comments

Comments
 (0)