Skip to content

Commit 7a03e07

Browse files
committed
✨ refactor(feature): 重构clear命令
1 parent 6bdc08f commit 7a03e07

File tree

8 files changed

+157
-12
lines changed

8 files changed

+157
-12
lines changed

README.md

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@ npm i code-genius
1616
npm i -g code-genius
1717
```
1818

19-
## commit 命令介绍
19+
帮助信息
2020

21-
生成符合 Angualr 规范的提交内容,支持命令模式和询问模式;
21+
```bash
22+
code-genius --help
23+
```
24+
25+
## commit 命令
26+
27+
生成 **Angualr** 规范的提交信息, 支持命令模式, 询问模式和 **API** 模式;
2228

2329
### 命令模式
2430

@@ -35,14 +41,91 @@ codeg commit -t fix -s feat -d 修复xx功能的xxBug
3541
### 询问模式
3642

3743
```bash
44+
# 启动询问模式
3845
codeg commit
46+
```
3947

48+
```
49+
# 询问过程
4050
1. 请选择提交类型
4151
2. 请选择提交范
4252
3. 请输入提交描述
4353
4. 要在提交信息中显示内置的 emoji 表情吗?
4454
```
4555

56+
### API 模式
57+
58+
仅对 `git commit -m "xxx"` 包装, 无提交规范生成.
59+
60+
```typescript
61+
import { gitCommit } from "code-genius";
62+
63+
(async () => {
64+
await gitCommit("fix(feat): 修复xx功能的xxBug");
65+
})();
66+
```
67+
68+
## verify 命令
69+
70+
校验 `COMMIT_EDITMSG` 中的信息是否符合 **Angualr** 规范, 支持命令模式和 **API** 模式;
71+
72+
### 命令模式
73+
74+
```bash
75+
codeg verify
76+
```
77+
78+
### API 模式
79+
80+
```typescript
81+
import { gitCommitVerify } from "code-genius";
82+
83+
(async () => {
84+
await gitCommitVerify();
85+
console.log("Git 提交信息校验通过, 正在执行后续逻辑...");
86+
})();
87+
```
88+
89+
## clear 命令
90+
91+
运行 `rimraf` 删除不再需要的文件或文件夹, 支持命令模式, 询问模式和 **API** 模式;
92+
93+
### 命令模式
94+
95+
```bash
96+
# 删除 dist 文件夹
97+
codeg clear -p ./dist
98+
99+
# 删除 dist 和 node_modules 两个文件夹
100+
codeg clear -p ./dist -p ./node_modules
101+
```
102+
103+
| 选项 | 描述 |
104+
| ------------------------- | ------------ |
105+
| -p, --pattern \<pattern\> | 设置匹配规则 |
106+
107+
### 询问模式
108+
109+
```bash
110+
# 启动询问模式
111+
codeg clear
112+
```
113+
114+
```
115+
# 询问过程
116+
1. 请选择需要清理的文件/夹
117+
```
118+
119+
### API 模式
120+
121+
```typescript
122+
import { clear } from "code-genius";
123+
124+
(async () => {
125+
await clear(["./dist"]);
126+
})();
127+
```
128+
46129
## 命令
47130

48131
| 命令 | 参数 | 默认值 | 功能描述 |

api-model/api-clear.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { clear } from "../src/index";
2+
3+
(async () => {
4+
await clear(["./dist"]);
5+
})();

api-model/api-commit.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { gitCommit } from "../src/index";
2+
3+
(async () => {
4+
await gitCommit("fix(feat): 修复xx功能的xxBug");
5+
})();

api-model/api-verify.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { gitCommitVerify } from "../src/index";
2+
3+
(async () => {
4+
await gitCommitVerify();
5+
console.log("Git 提交信息校验通过, 正在执行后续逻辑...");
6+
})();

src/command/clear.ts

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,44 @@
11
import type { CAC } from "cac";
22

3+
import path from "node:path";
4+
5+
import fs from "fs-extra";
6+
import enquirer from "enquirer";
7+
38
import { execCommand, loggerInfo } from "@/shared/index";
49
import { ACTIVATION, clearGlob } from "@/shared/config";
10+
import { ClearOptions } from "@/shared/types";
11+
12+
const generateEnquirer = async (): Promise<ClearOptions> => {
13+
const files = fs
14+
.readdirSync(path.join(process.cwd(), "."))
15+
.filter((v) => !v.startsWith("."))
16+
.map((file) => {
17+
return {
18+
sort: fs.statSync(path.join(process.cwd(), file)).isFile() ? 1 : 0,
19+
file,
20+
};
21+
});
22+
files.sort((v1, v2) => v1.sort - v2.sort);
23+
const fileMultiChoices = files.map((v) => {
24+
return {
25+
name: `./${v.file}`,
26+
message: `${v.file}`,
27+
hint: clearGlob.includes(`./${v.file}`) ? "建议清理" : "",
28+
};
29+
});
30+
const result = await enquirer.prompt<ClearOptions>([
31+
{
32+
name: "files",
33+
type: "multiselect",
34+
message: "请选择需要清理的文件/夹",
35+
choices: fileMultiChoices,
36+
},
37+
]);
38+
return {
39+
files: result.files,
40+
};
41+
};
542

643
export const clear = async (paths: string[]) => {
744
if (ACTIVATION) {
@@ -18,15 +55,20 @@ export default function clearInstaller(cli: CAC) {
1855
setup: () => {
1956
cli
2057
.command("clear", "运行 rimraf 删除不再需要的文件或文件夹")
21-
.option("-p, --pattern <pattern>", "设置配置规则", {
22-
default: [...clearGlob],
23-
})
58+
.option("-p, --pattern <pattern>", "设置匹配规则")
2459
.action(async (options) => {
25-
const patterns =
26-
typeof options.pattern === "string"
27-
? [options.pattern]
28-
: options.pattern;
29-
await clear(patterns);
60+
const { pattern } = options;
61+
let paths = [];
62+
if (!pattern) {
63+
const result = await generateEnquirer();
64+
paths = result.files;
65+
} else {
66+
paths =
67+
typeof options.pattern === "string"
68+
? [options.pattern]
69+
: options.pattern;
70+
}
71+
await clear(paths);
3072
});
3173
},
3274
};

src/shared/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99

1010
import { blue, green, yellow } from "kolorist";
1111

12-
export const clearGlob = ["./dist/"];
12+
export const clearGlob = ["./dist", "./node_modules"];
1313

1414
export const formatGlob = ["./src"];
1515

src/shared/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@ export interface GitCommitOptions {
6969
scope: string;
7070
description: string;
7171
}
72+
73+
export interface ClearOptions {
74+
files: Array<string>;
75+
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
"@/*": ["src/*"]
1818
}
1919
},
20-
"include": ["src/**/*", "scripts/**/*"],
20+
"include": ["src/**/*", "scripts/**/*", "api-module/**/*"],
2121
"exclude": ["node_modules", "dist"]
2222
}

0 commit comments

Comments
 (0)