Skip to content

Commit 8e87f09

Browse files
committed
✨ refactor(feature): 重构clear、registry、impsort询问模块
1 parent 1c1c5dd commit 8e87f09

File tree

7 files changed

+87
-19
lines changed

7 files changed

+87
-19
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ codeg clear -p ./dist -p ./node_modules
142142
| 选项 | 描述 |
143143
| ------------------------- | ------------ |
144144
| -p, --pattern \<pattern\> | 设置匹配规则 |
145+
| -a, --ask | 启用询问模式 |
145146

146147
### 询问模式
147148

@@ -237,6 +238,10 @@ import { npmDepCheck } from "code-genius";
237238
codeg registry -u https://registry.npmjs.org/
238239
```
239240

241+
| 选项 | 描述 |
242+
| --------- | ------------ |
243+
| -a, --ask | 启用询问模式 |
244+
240245
### 询问模式
241246

242247
```bash
@@ -461,6 +466,7 @@ codeg impsort -p ./src -p ./components
461466
| 选项 | 描述 |
462467
| ------------------------- | ------------ |
463468
| -p, --pattern \<pattern\> | 设置匹配规则 |
469+
| -a, --ask | 启用询问模式 |
464470

465471
### 询问模式
466472

src/command/clear.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from "node:path";
22
import { performance } from "node:perf_hooks";
33

4+
import Ajv from "ajv";
45
import type { CAC } from "cac";
56
import enquirer from "enquirer";
67
import fs from "fs-extra";
@@ -9,6 +10,14 @@ import { ACTIVATION, clearGlob } from "@/config";
910
import { execCommand, loggerInfo, printInfo } from "@/helper";
1011
import { ClearOptions, CodeGeniusOptions } from "@/types";
1112

13+
const schema = {
14+
type: "object",
15+
properties: {
16+
paths: { type: "array" },
17+
},
18+
required: ["paths"],
19+
};
20+
1221
const mergeConfig = async (config: CodeGeniusOptions) => {
1322
const commands = config && config?.commands;
1423
if (commands && commands.clear) {
@@ -61,6 +70,16 @@ export const clear = async (paths: string[]) => {
6170
if (ACTIVATION) {
6271
loggerInfo(`clear 参数信息: \n ${JSON.stringify(paths)}`);
6372
}
73+
74+
const ajv = new Ajv();
75+
const validate = ajv.compile(schema);
76+
const valid = validate({
77+
paths,
78+
});
79+
if (!valid && validate.errors && validate.errors?.length > 0) {
80+
throw new Error(validate.errors[0].message);
81+
}
82+
6483
await execCommand("npx", ["rimraf", "--glob", ...paths], {
6584
stdio: "inherit",
6685
});
@@ -74,17 +93,15 @@ export default function clearInstaller(cli: CAC, config: CodeGeniusOptions) {
7493
cli
7594
.command("clear", "运行 rimraf 删除不再需要的文件或文件夹")
7695
.option("-p, --pattern <pattern>", "设置匹配规则")
96+
.option("-a, --ask", "启用询问模式")
7797
.action(async (options) => {
78-
const { pattern } = options;
98+
const { pattern, ask } = options;
7999
let paths = [];
80-
if (!pattern) {
100+
if (ask) {
81101
const result = await generateEnquirer(config);
82102
paths = result.files;
83103
} else {
84-
paths =
85-
typeof options.pattern === "string"
86-
? [options.pattern]
87-
: options.pattern;
104+
paths = typeof pattern === "string" ? [pattern] : pattern;
88105
}
89106
const start = performance.now();
90107
await clear(paths);

src/command/eslint-import-sort.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import path from "node:path";
2+
import { performance } from "node:perf_hooks";
23

4+
import Ajv from "ajv";
35
import type { CAC } from "cac";
46
import enquirer from "enquirer";
57
import { ESLint } from "eslint";
@@ -11,6 +13,14 @@ import { ImpSortOptions } from "@/types";
1113

1214
import { CodeGeniusOptions } from "./../types";
1315

16+
const schema = {
17+
type: "object",
18+
properties: {
19+
paths: { type: "array" },
20+
},
21+
required: ["paths"],
22+
};
23+
1424
const mergeConfig = async (config: CodeGeniusOptions) => {
1525
const commands = config && config?.commands;
1626
if (commands && commands.impsort) {
@@ -63,6 +73,15 @@ export const impSort = async (paths: string[]) => {
6373
loggerInfo(`impSort 参数信息: \n ${JSON.stringify(paths)}`);
6474
}
6575

76+
const ajv = new Ajv();
77+
const validate = ajv.compile(schema);
78+
const valid = validate({
79+
paths,
80+
});
81+
if (!valid && validate.errors && validate.errors?.length > 0) {
82+
throw new Error(validate.errors[0].message);
83+
}
84+
6685
const eslint = new ESLint({
6786
fix: true,
6887
overrideConfig: {
@@ -92,19 +111,20 @@ export default function impSortInstaller(cli: CAC, config: CodeGeniusOptions) {
92111
cli
93112
.command("impsort", "运行 eslint 对模块导入进行分组&按字母排序")
94113
.option("-p, --pattern <pattern>", "设置匹配规则")
114+
.option("-a, --ask", "启用询问模式")
95115
.action(async (options) => {
96-
const { pattern } = options;
116+
const { pattern, ask } = options;
97117
let paths = [];
98-
if (!pattern) {
118+
if (ask) {
99119
const result = await generateEnquirer(config);
100120
paths = result.files;
101121
} else {
102-
paths =
103-
typeof options.pattern === "string"
104-
? [options.pattern]
105-
: options.pattern;
122+
paths = typeof pattern === "string" ? [pattern] : pattern;
106123
}
124+
const start = performance.now();
107125
await impSort(paths);
126+
const getTime = () => `${(performance.now() - start).toFixed(2)}ms`;
127+
loggerInfo(`😁 impsort 命令执行结束, 共用时: ${getTime()}`);
108128
});
109129
},
110130
};

src/command/npm-registry.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
import Ajv from "ajv";
12
import type { CAC } from "cac";
23
import enquirer from "enquirer";
34

45
import { ACTIVATION, npmRegisters } from "@/config";
56
import { execCommand, loggerInfo, printInfo, printWarring } from "@/helper";
67
import { RegistryOptions } from "@/types";
78

9+
const schema = {
10+
type: "object",
11+
properties: {
12+
registry: { type: "string" },
13+
},
14+
required: ["registry"],
15+
};
16+
817
const printCurrentRegistry = async (isBefore = true) => {
918
printInfo(`${isBefore ? "当前" : "最新"} NPM 镜像地址(全局):`);
1019
await execCommand("npm", ["config", "get", "registry", "-g"], {
@@ -46,6 +55,16 @@ export const npmRegistry = async (registry: string) => {
4655
if (ACTIVATION) {
4756
loggerInfo(`npmRegistry 参数信息: \n ${JSON.stringify(registry)}`);
4857
}
58+
59+
const ajv = new Ajv();
60+
const validate = ajv.compile(schema);
61+
const valid = validate({
62+
registry,
63+
});
64+
if (!valid && validate.errors && validate.errors?.length > 0) {
65+
throw new Error(validate.errors[0].message);
66+
}
67+
4968
await execCommand("npm", ["config", "set", "registry", registry], {
5069
stdio: "inherit",
5170
});
@@ -59,10 +78,11 @@ export default function npmRegistryInstaller(cli: CAC) {
5978
cli
6079
.command("registry", "切换 NPM 镜像地址")
6180
.option("-u, --url <url>", "镜像地址")
81+
.option("-a, --ask", "启用询问模式")
6282
.action(async (options) => {
63-
const { url } = options;
83+
const { url, ask } = options;
6484
let registryUrl = "";
65-
if (!url) {
85+
if (ask) {
6686
const result = await generateEnquirer();
6787
registryUrl = result.url;
6888
} else {

src/command/prettier-format.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { performance } from "node:perf_hooks";
2+
13
import type { CAC } from "cac";
24

35
import { ACTIVATION, formatGlob } from "@/config";
@@ -41,13 +43,16 @@ export default function prettierFormatInstaller(
4143
.action(async (options) => {
4244
const { paths } = await mergeConfig(config);
4345
const { pattern } = options;
46+
const start = performance.now();
4447
if (pattern) {
4548
await prettierFormat(
4649
typeof pattern === "string" ? [pattern] : pattern,
4750
);
4851
} else {
4952
await prettierFormat(paths);
5053
}
54+
const getTime = () => `${(performance.now() - start).toFixed(2)}ms`;
55+
loggerInfo(`😁 format 命令执行结束, 共用时: ${getTime()}`);
5156
});
5257
},
5358
};

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, "--ask"], { stdio: "inherit" });
31+
await execCommand("codeg", result.command.split(" "), { stdio: "inherit" });
3232
};
3333

3434
export default function rootInstaller(cli: CAC) {

src/config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export const gitUserOptions: GitUserOptions = {
201201

202202
export const commands = [
203203
{
204-
command: "commit",
204+
command: "commit --ask",
205205
description: "生成 angualr 规范的提交信息",
206206
},
207207
{
@@ -217,11 +217,11 @@ export const commands = [
217217
description: "新增或修改 simple-git-hooks 配置后需要重新初始化",
218218
},
219219
{
220-
command: "registry",
220+
command: "registry --ask",
221221
description: "切换 NPM 镜像地址",
222222
},
223223
{
224-
command: "clear",
224+
command: "clear --ask",
225225
description: "运行 rimraf 删除不再需要的文件或文件夹",
226226
},
227227
{
@@ -233,7 +233,7 @@ export const commands = [
233233
description: "运行 eslint 静态扫描和修复代码中存在的问题",
234234
},
235235
{
236-
command: "impsort",
236+
command: "impsort --ask",
237237
description: "运行 eslint 对模块导入进行分组&按字母排序",
238238
},
239239
{

0 commit comments

Comments
 (0)