Skip to content

Commit 2627855

Browse files
committed
🔧chore(feature): 增加 git-user 命令
1 parent e6e9bd8 commit 2627855

5 files changed

Lines changed: 123 additions & 0 deletions

File tree

scripts/lint-script.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { eslintFix } from "@/command/eslint-fix";
2+
import { gitUser } from "@/command/git-user";
23
import { prettierFormat } from "@/command/prettier-format";
34
import { execCommand, printError } from "@/shared";
45

56
async function lint() {
67
try {
8+
await gitUser({ ruleEmail: "^[a-zA-Z0-9._%+-]+@(gmail)\\.(com)$" });
79
await prettierFormat(["./src/", "./scripts/"]);
810
await execCommand("git", ["add", "."], {
911
stdio: "inherit",

src/command/git-user.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import type { CAC } from "cac";
2+
3+
import {
4+
execCommand,
5+
loggerInfo,
6+
printError,
7+
printInfo,
8+
printWarring,
9+
} from "@/shared/index";
10+
import { ACTIVATION, gitUserOptions } from "@/shared/config";
11+
import { GitUserOptions } from "..";
12+
13+
async function printCurrentGitUser(isBefore: boolean = true) {
14+
printInfo(`${isBefore ? "当前" : "最新"}使用的 Git UserName :`);
15+
await execCommand("git", ["config", "user.name"], {
16+
stdio: "inherit",
17+
});
18+
printInfo(`${isBefore ? "当前" : "最新"}使用的 Git UserEmail :`);
19+
await execCommand("git", ["config", "user.email"], {
20+
stdio: "inherit",
21+
});
22+
}
23+
24+
export const gitUser = async (options: GitUserOptions) => {
25+
if (ACTIVATION) {
26+
loggerInfo("gitUser 参数信息: \n");
27+
console.table(options);
28+
}
29+
30+
const { name, email, ruleName, ruleEmail } = options;
31+
const nameRegExp = new RegExp(ruleName);
32+
const emailRegExp = new RegExp(ruleEmail, "i");
33+
34+
await printCurrentGitUser(true);
35+
36+
if (name) {
37+
if (!nameRegExp.test(name)) {
38+
printWarring(`因输入的 ${name} 不符合 name 规则, 未能成功设置 name`);
39+
} else {
40+
await execCommand("git", ["config", "user.name", name], {
41+
stdio: "inherit",
42+
});
43+
printInfo(`最新使用的 Git UserName :`);
44+
await execCommand("git", ["config", "user.name"], {
45+
stdio: "inherit",
46+
});
47+
}
48+
}
49+
50+
if (email) {
51+
if (!emailRegExp.test(email)) {
52+
printWarring(`因输入的 ${email} 不符合 email 规则, 未能成功设置 email`);
53+
} else {
54+
await execCommand("git", ["config", "user.email", email], {
55+
stdio: "inherit",
56+
});
57+
printInfo(`最新使用的 Git UserEmail :`);
58+
await execCommand("git", ["config", "user.email"], {
59+
stdio: "inherit",
60+
});
61+
}
62+
}
63+
64+
if (!name && !email) {
65+
const username = await execCommand("git", ["config", "user.name"]);
66+
if (!nameRegExp.test(username)) {
67+
printError("Git 配置的 user.name 不符合规范, 请更换");
68+
process.exit(1);
69+
}
70+
71+
const useremail = await execCommand("git", ["config", "user.email"]);
72+
if (!emailRegExp.test(useremail)) {
73+
printError("Git 配置的 user.email 不符合规范, 请更换");
74+
process.exit(1);
75+
}
76+
}
77+
};
78+
79+
export default function gitUserInstaller(cli: CAC) {
80+
return {
81+
name: "gitUserInstaller",
82+
setup: () => {
83+
cli
84+
.command("git-user", "设置或校验 git user 信息是否规范")
85+
.option("-n, --name <name>", "设置 user.name")
86+
.option("-e, --email <email>", "设置 user.email")
87+
.option("--rule-name [regexp]", "设置 user.name 匹配规则(转义字符串)", {
88+
default: gitUserOptions.ruleName,
89+
})
90+
.option(
91+
"--rule-email [regexp]",
92+
"设置 user.email 匹配规则(转义字符串)",
93+
{
94+
default: gitUserOptions.ruleEmail,
95+
},
96+
)
97+
.action(async (options) => {
98+
console.log(options);
99+
await gitUser(options);
100+
});
101+
},
102+
};
103+
}

src/setup.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import eslintFixInstaller from "./command/eslint-fix";
1212
import prettierFormatInstaller from "./command/prettier-format";
1313
import templateInstaller from "./command/template";
1414
import lighthouseInstaller from "./command/lighthouse";
15+
import gitUserInstaller from "./command/git-user";
1516

1617
export function cmdInstaller(cli: CAC) {
1718
gitCommitInstaller(cli).setup();
@@ -26,4 +27,5 @@ export function cmdInstaller(cli: CAC) {
2627
createProjectInstaller(cli).setup();
2728
templateInstaller(cli).setup();
2829
lighthouseInstaller(cli).setup();
30+
gitUserInstaller(cli).setup();
2931
}

src/shared/config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
CommitScope,
33
CommitType,
44
Framework,
5+
GitUserOptions,
56
KeyValue,
67
ProjectSource,
78
} from "@/shared/types";
@@ -181,3 +182,11 @@ export const TEMPLATES = FRAMEWORKS.map(
181182
).reduce((a, b) => a.concat(b), []);
182183

183184
export const fileIgnore = ["package.json", "_gitignore"];
185+
186+
export const gitUserOptions: GitUserOptions = {
187+
name: "",
188+
email: "",
189+
ruleName: "[\\s\\S]*",
190+
ruleEmail:
191+
"^[a-zA-Z0-9._%+-]+@(163|qq|126|139|sina|sohu|yeah|gmail)\\.(com|net)$",
192+
};

src/shared/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,10 @@ export interface Framework {
4747
color: ColorFunc;
4848
variants: FrameworkVariant[];
4949
}
50+
51+
export interface GitUserOptions {
52+
name?: string;
53+
email?: string;
54+
ruleName?: string;
55+
ruleEmail?: string;
56+
}

0 commit comments

Comments
 (0)