Skip to content

Commit 24156f3

Browse files
committed
✨ refactor(feature): 重构gituser命令,api发生了重大改变
1 parent 27a4bbb commit 24156f3

File tree

5 files changed

+150
-61
lines changed

5 files changed

+150
-61
lines changed

README.md

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ codeg registry
191191

192192
### API 模式
193193

194+
仅对 `npm config set registry xxx` 包装, 无其它配置.
195+
194196
```typescript
195197
import { npmRegistry } from "code-genius";
196198

@@ -199,19 +201,63 @@ import { npmRegistry } from "code-genius";
199201
})();
200202
```
201203

204+
## gituser 命令
205+
206+
设置或校验 `git user` 信息是否规范, 支持命令模式和 **API** 模式;
207+
208+
### 命令模式
209+
210+
```bash
211+
# 在默认规则下设置 email 信息
212+
codeg gituser -e [email protected]
202213

214+
# 在指定规则下设置 email 信息
215+
codeg gituser -e [email protected] --rule-email '^[a-zA-Z0-9._%+-]+@(qq)\.(com)$'
216+
```
217+
218+
```bash
219+
# 在默认规则下校验 user 和 email 信息
220+
codeg gituser
221+
222+
# 在指定规则下校验 user 和 email 信息
223+
codeg gituser --rule-email '^[a-zA-Z0-9._%+-]+@(qq)\.(com)$'
224+
```
225+
226+
| 选项 | 描述 |
227+
| ----------------------- | ------------------------------------ |
228+
| -n, --name \<name\> | 设置 user.name |
229+
| -e, --email \<email\> | 设置 user.email |
230+
| --rule-name \<regexp\> | 设置 user.name 匹配规则(转义字符串) |
231+
| --rule-email \<regexp\> | 设置 user.email 匹配规则(转义字符串) |
232+
233+
- **--rule-name 默认 :** `[\s\S]*`
234+
- **--rule-email 默认 :** `^[a-zA-Z0-9._%+-]+@(163|qq|126|139|sina|sohu|yeah|gmail)\.(com|net)$`
235+
236+
### API 模式
237+
238+
```typescript
239+
import { setGitUserName, setGitUserEmail, checkGitUserInfo } from "code-genius";
240+
241+
(async () => {
242+
await setGitUserName("OSpoon", "[\\s\\S]*");
243+
await setGitUserEmail(
244+
245+
"^[a-zA-Z0-9._%+-]+@(gmail)\\.(com)$"
246+
);
247+
await checkGitUserInfo("[\\s\\S]*", "^[a-zA-Z0-9._%+-]+@(gmail)\\.(com)$");
248+
})();
249+
```
203250

204251
## 命令
205252

206-
| 命令 | 参数 | 默认值 | 功能描述 |
207-
| ---------- | ---------------------------------------------------------------------------------------- | ------- | --------------------------------------------------- |
208-
| fix | --pattern \<pattern\> | './src' | 运行 eslint 静态扫描和修复代码中存在的问题 |
209-
| format | --pattern \<pattern\> | './src' | 运行 prettier 格式化代码风格 |
210-
| create | -- | -- | 运行 npm create 快速创建基础项目 |
211-
| template | -n, --project-name \<project-name\>, -f, --framework \<framework\>, | -- | 快速创建 CodeGenius 基础项目 |
212-
| lighthouse | --url \<url\> | -- | 运行 lighthouse 分析及收集 Web 应用的性能指标 |
213-
| git-user | -n, --name \<name\>,-e, --email \<email\>,--rule-name \<regexp\>,--rule-email \<regexp\> | -- | 设置或校验 git user 信息是否规范 |
214-
| quantity | -p, --path <path> | -- | 运行 cloc 分析并统计代码量 |
253+
| 命令 | 参数 | 默认值 | 功能描述 |
254+
| ---------- | ------------------------------------------------------------------- | ------- | --------------------------------------------- |
255+
| fix | --pattern \<pattern\> | './src' | 运行 eslint 静态扫描和修复代码中存在的问题 |
256+
| format | --pattern \<pattern\> | './src' | 运行 prettier 格式化代码风格 |
257+
| create | -- | -- | 运行 npm create 快速创建基础项目 |
258+
| template | -n, --project-name \<project-name\>, -f, --framework \<framework\>, | -- | 快速创建 CodeGenius 基础项目 |
259+
| lighthouse | --url \<url\> | -- | 运行 lighthouse 分析及收集 Web 应用的性能指标 |
260+
| quantity | -p, --path <path> | -- | 运行 cloc 分析并统计代码量 |
215261

216262
## 执照
217263

api-model/api-gituser.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {
2+
setGitUserName,
3+
setGitUserEmail,
4+
checkGitUserInfo,
5+
} from "../src/index";
6+
7+
(async () => {
8+
await setGitUserName("OSpoon", "[\\s\\S]*");
9+
await setGitUserEmail(
10+
11+
"^[a-zA-Z0-9._%+-]+@(gmail)\\.(com)$"
12+
);
13+
await checkGitUserInfo("[\\s\\S]*", "^[a-zA-Z0-9._%+-]+@(gmail)\\.(com)$");
14+
})();

scripts/lint-script.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { eslintFix } from "@/command/eslint-fix";
2-
import { gitUser } from "@/command/git-user";
2+
import { checkGitUserInfo } from "@/command/git-user";
33
import { prettierFormat } from "@/command/prettier-format";
44
import { execCommand } from "@/helper";
55

66
async function lint() {
7-
await gitUser({ ruleEmail: "^[a-zA-Z0-9._%+-]+@(gmail)\\.(com)$" });
7+
await checkGitUserInfo("[\\s\\S]*", "^[a-zA-Z0-9._%+-]+@(gmail)\\.(com)$");
88
await prettierFormat(["./src/", "./scripts/"]);
99
await execCommand("git", ["add", "."]);
1010
await eslintFix(["./src/", "./scripts/"]);

src/command/git-user.ts

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,69 +8,80 @@ import {
88
printWarring,
99
} from "@/helper";
1010
import { ACTIVATION, gitUserOptions } from "@/config";
11-
import { GitUserOptions } from "@/types";
1211

13-
async function printCurrentGitUser() {
14-
const name = await execCommand("git", ["config", "user.name"]);
15-
const email = await execCommand("git", ["config", "user.email"]);
16-
printInfo(`\ngit config info:\n user.name: ${name}\n user.email: ${email}`);
12+
export async function setGitUserName(name: string, ruleName: string) {
13+
if (ACTIVATION) {
14+
loggerInfo(
15+
`setGitUserName 参数信息: \n ${JSON.stringify({
16+
name,
17+
ruleName,
18+
})}`,
19+
);
20+
}
21+
const nameRegExp = new RegExp(ruleName!);
22+
if (!nameRegExp.test(name)) {
23+
printWarring(`设置失败(user.name), ${name} 不符合规范`);
24+
} else {
25+
await execCommand("git", ["config", "user.name", name], {
26+
stdio: "inherit",
27+
});
28+
const result = await execCommand("git", ["config", "user.name"]);
29+
printInfo(`更新成功(user.name): ${result}`);
30+
}
1731
}
1832

19-
export const gitUser = async (options: GitUserOptions) => {
33+
export async function setGitUserEmail(email: string, ruleEmail: string) {
2034
if (ACTIVATION) {
21-
loggerInfo(`gitUser 参数信息: \n ${JSON.stringify(options)}`);
35+
loggerInfo(
36+
`setGitUserEmail 参数信息: \n ${JSON.stringify({
37+
email,
38+
ruleEmail,
39+
})}`,
40+
);
2241
}
23-
24-
const { name, email, ruleName, ruleEmail } = options;
25-
const nameRegExp = new RegExp(ruleName!);
2642
const emailRegExp = new RegExp(ruleEmail!, "i");
27-
28-
await printCurrentGitUser();
29-
30-
if (name) {
31-
if (!nameRegExp.test(name)) {
32-
printWarring(`设置失败(user.name), ${name} 不符合规范`);
33-
} else {
34-
await execCommand("git", ["config", "user.name", name], {
35-
stdio: "inherit",
36-
});
37-
const result = await execCommand("git", ["config", "user.name"]);
38-
printInfo(`更新成功(user.name): ${result}`);
39-
}
43+
if (!emailRegExp.test(email)) {
44+
printWarring(`设置失败(user.email), ${email} 不符合规范`);
45+
} else {
46+
await execCommand("git", ["config", "user.email", email], {
47+
stdio: "inherit",
48+
});
49+
const result = await execCommand("git", ["config", "user.email"]);
50+
printInfo(`更新成功(user.email): ${result}`);
4051
}
52+
}
4153

42-
if (email) {
43-
if (!emailRegExp.test(email)) {
44-
printWarring(`设置失败(user.email), ${email} 不符合规范`);
45-
} else {
46-
await execCommand("git", ["config", "user.email", email], {
47-
stdio: "inherit",
48-
});
49-
const result = await execCommand("git", ["config", "user.email"]);
50-
printInfo(`更新成功(user.email): ${result}`);
51-
}
54+
export async function checkGitUserInfo(ruleName: string, ruleEmail: string) {
55+
if (ACTIVATION) {
56+
loggerInfo(
57+
`checkGitUserInfo 参数信息: \n ${JSON.stringify({
58+
ruleName,
59+
ruleEmail,
60+
})}`,
61+
);
5262
}
53-
54-
if (!name && !email) {
55-
const username = await execCommand("git", ["config", "user.name"]);
56-
if (!nameRegExp.test(username)) {
57-
printError(`${username} 不符合 ${ruleName} 规范`);
58-
process.exit(1);
59-
}
60-
const useremail = await execCommand("git", ["config", "user.email"]);
61-
if (!emailRegExp.test(useremail)) {
62-
printError(`${useremail} 不符合 ${ruleEmail} 规范`);
63-
process.exit(1);
64-
}
63+
const nameRegExp = new RegExp(ruleName!);
64+
const emailRegExp = new RegExp(ruleEmail!, "i");
65+
const username = await execCommand("git", ["config", "user.name"]);
66+
if (!nameRegExp.test(username)) {
67+
printError(`${username} 不符合 ${ruleName} 规范`);
68+
process.exit(1);
69+
}
70+
const useremail = await execCommand("git", ["config", "user.email"]);
71+
if (!emailRegExp.test(useremail)) {
72+
printError(`${useremail} 不符合 ${ruleEmail} 规范`);
73+
process.exit(1);
6574
}
66-
};
75+
}
6776

6877
export default function gitUserInstaller(cli: CAC) {
6978
return {
7079
name: "gitUserInstaller",
7180
setup: () => {
7281
cli
73-
.command("git-user", "设置或校验 git user 信息是否规范")
82+
.command("gituser", "设置或校验 git user 信息是否规范")
83+
.alias("git-user")
84+
.alias("gitu")
7485
.option("-n, --name <name>", "设置 user.name")
7586
.option("-e, --email <email>", "设置 user.email")
7687
.option("--rule-name <regexp>", "设置 user.name 匹配规则(转义字符串)", {
@@ -83,7 +94,19 @@ export default function gitUserInstaller(cli: CAC) {
8394
default: gitUserOptions.ruleEmail,
8495
},
8596
)
86-
.action(async (options) => await gitUser(options));
97+
.action(async (options) => {
98+
console.log(options);
99+
const { name, email, ruleName, ruleEmail } = options;
100+
if (!name && !email) {
101+
await checkGitUserInfo(ruleName, ruleEmail);
102+
}
103+
if (name) {
104+
await setGitUserName(name, ruleName);
105+
}
106+
if (email) {
107+
await setGitUserName(email, ruleEmail);
108+
}
109+
});
87110
},
88111
};
89112
}

src/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import prettierFormatInstaller, {
1818
} from "@/command/prettier-format";
1919
import templateInstaller, { template } from "@/command/template";
2020
import lighthouseInstaller, { lighthouse } from "@/command/lighthouse";
21-
import gitUserInstaller, { gitUser } from "@/command/git-user";
21+
import gitUserInstaller, {
22+
setGitUserName,
23+
setGitUserEmail,
24+
checkGitUserInfo,
25+
} from "@/command/git-user";
2226
import quantityInstaller, { quantity } from "@/command/quantity";
2327
import { defineConfig } from "@/helper";
2428

@@ -35,7 +39,9 @@ export {
3539
prettierFormat,
3640
template,
3741
lighthouse,
38-
gitUser,
42+
setGitUserName,
43+
setGitUserEmail,
44+
checkGitUserInfo,
3945
quantity,
4046
};
4147

0 commit comments

Comments
 (0)