Skip to content

Commit cdfd2e4

Browse files
committed
✨ refactor(feature): 重构commit命令
1 parent 2a2490c commit cdfd2e4

File tree

2 files changed

+43
-32
lines changed

2 files changed

+43
-32
lines changed

src/command/git-commit.ts

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,30 @@ import type { CAC } from "cac";
22

33
import enquirer from "enquirer";
44

5-
import { ACTIVATION, gitCommitScopes, gitCommitTypes } from "@/shared/config";
6-
import { CommitScope, CommitType } from "@/shared/types";
75
import { loggerInfo, execCommand } from "@/shared/index";
8-
interface PromptResult {
9-
type: string;
10-
scope: string;
11-
description: string;
12-
}
13-
14-
interface GitCommitOptions {
15-
emoji: boolean;
16-
}
6+
import { ACTIVATION, gitCommitScopes, gitCommitTypes } from "@/shared/config";
7+
import { GitCommitOptions } from "@/shared/types";
178

18-
export const gitCommit = async (
19-
types: Array<CommitType>,
20-
scopes: Array<CommitScope>,
21-
options: GitCommitOptions,
22-
) => {
23-
if (ACTIVATION) {
24-
loggerInfo(
25-
`gitCommit 参数信息: \ntypes ${types} \nscopes${scopes} \noptions${options}`,
26-
);
27-
}
28-
const { emoji: emojiStatus } = options;
29-
const typesChoices = types.map(({ emoji, code, description }) => {
9+
const generateEnquirer = async (): Promise<
10+
Pick<GitCommitOptions, Exclude<keyof GitCommitOptions, "emoji">>
11+
> => {
12+
const typesChoices = gitCommitTypes.map(({ emoji, code, description }) => {
3013
const formatCode = `${code}:`.padEnd(20);
3114
return {
32-
name: emojiStatus ? `${emoji}${code}` : code,
15+
name: `${emoji} ${code}`,
3316
message: `${emoji}${formatCode}${description}`,
3417
};
3518
});
3619

37-
const scopesChoices = scopes.map(({ name, description }) => {
20+
const scopesChoices = gitCommitScopes.map(({ name, description }) => {
3821
const formatName = `${name}:`.padEnd(20);
3922
return {
4023
name,
4124
message: `${formatName.padEnd(20)} ${description}`,
4225
};
4326
});
4427

45-
const result = await enquirer.prompt<PromptResult>([
28+
const result = await enquirer.prompt<GitCommitOptions>([
4629
{
4730
name: "type",
4831
type: "select",
@@ -60,9 +43,23 @@ export const gitCommit = async (
6043
type: "text",
6144
message: "请输入提交描述",
6245
},
46+
{
47+
name: "emoji",
48+
type: "confirm",
49+
message: "要在提交信息中显示内置的 emoji 表情吗?",
50+
},
6351
]);
52+
return {
53+
type: result.emoji ? result.type : result.type.split(" ")[1],
54+
scope: result.scope,
55+
description: result.description,
56+
};
57+
};
6458

65-
const content = `${result.type}(${result.scope}): ${result.description}`;
59+
export const gitCommit = async (content: string) => {
60+
if (ACTIVATION) {
61+
loggerInfo(`gitCommit 参数信息: \n${JSON.stringify(content)}`);
62+
}
6663
await execCommand("git", ["commit", "-m", content], { stdio: "inherit" });
6764
};
6865

@@ -72,12 +69,19 @@ export default function gitCommitInstaller(cli: CAC) {
7269
setup: () => {
7370
cli
7471
.command("commit", "生成 angualr 规范的提交信息")
75-
.option("--no-emoji", "禁用 emoji")
72+
.option("-t, --type <type>", "添加修改类型")
73+
.option("-s, --scope <scope>", "填写修改范围")
74+
.option("-d, --description <description>", "填写修改描述")
7675
.action(async (options) => {
77-
const { emoji } = options;
78-
await gitCommit(gitCommitTypes, gitCommitScopes, {
79-
emoji,
80-
});
76+
const { type, scope, description } = options;
77+
let content = "";
78+
if (!type || !scope || !description) {
79+
const result = await generateEnquirer();
80+
content = `${result.type}(${result.scope}): ${result.description}`;
81+
} else {
82+
content = `${type}(${scope}): ${description}`;
83+
}
84+
await gitCommit(content);
8185
});
8286
},
8387
};

src/shared/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,10 @@ export interface CodeGeniusOptions {
6262
}
6363
>;
6464
}
65+
66+
export interface GitCommitOptions {
67+
emoji: boolean;
68+
type: string;
69+
scope: string;
70+
description: string;
71+
}

0 commit comments

Comments
 (0)