Skip to content

Commit 923804a

Browse files
committed
🔧 chore(app): 增加配置文件支持
1 parent da831c2 commit 923804a

File tree

6 files changed

+114
-52
lines changed

6 files changed

+114
-52
lines changed

src/command/clear.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,23 @@ import enquirer from "enquirer";
55
import fs from "fs-extra";
66

77
import { ACTIVATION, clearGlob } from "@/config";
8-
import { execCommand, loggerInfo, printInfo } from "@/helper";
8+
import { execCommand, loadConfigModule, loggerInfo, printInfo } from "@/helper";
99
import { ClearOptions } from "@/types";
1010

11+
const mergeConfig = async () => {
12+
const config = await loadConfigModule();
13+
const commands = config && config?.commands;
14+
if (commands && commands.clear) {
15+
const { files } = commands.clear;
16+
return {
17+
paths: files && files.length > 0 ? files : clearGlob,
18+
};
19+
}
20+
return {
21+
paths: clearGlob,
22+
};
23+
};
24+
1125
const generateEnquirer = async (): Promise<ClearOptions> => {
1226
const files = fs
1327
.readdirSync(path.join(process.cwd(), "."))
@@ -19,16 +33,18 @@ const generateEnquirer = async (): Promise<ClearOptions> => {
1933
};
2034
});
2135
files.sort((v1, v2) => v1.sort - v2.sort);
36+
37+
const { paths } = await mergeConfig();
2238
const fileMultiChoices = files.map((v) => {
2339
return {
2440
name: `./${v.file}`,
2541
message: `${v.file}`,
26-
hint: clearGlob.includes(`./${v.file}`) ? "建议清理" : "",
42+
hint: paths.includes(`./${v.file}`) ? "建议清理" : "",
2743
};
2844
});
2945
const result = await enquirer.prompt<ClearOptions>([
3046
{
31-
name: "files",
47+
name: "paths",
3248
type: "multiselect",
3349
message: "请选择需要清理的文件/夹",
3450
choices: fileMultiChoices,

src/command/eslint-fix.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,27 @@ import type { CAC } from "cac";
33
import { ACTIVATION, eslintGlob } from "@/config";
44
import {
55
execCommand,
6+
loadConfigModule,
67
loggerError,
78
loggerInfo,
89
printError,
910
printInfo,
1011
} from "@/helper";
1112

13+
const mergeConfig = async () => {
14+
const config = await loadConfigModule();
15+
const commands = config && config?.commands;
16+
if (commands && commands.fix) {
17+
const { paths } = commands.fix;
18+
return {
19+
paths: paths && paths.length > 0 ? paths : eslintGlob,
20+
};
21+
}
22+
return {
23+
paths: eslintGlob,
24+
};
25+
};
26+
1227
export const eslintFix = async (paths: string[]) => {
1328
if (ACTIVATION) {
1429
loggerInfo(`eslintFix 参数信息: \n ${paths}`);
@@ -36,11 +51,13 @@ export default function eslintFixInstaller(cli: CAC) {
3651
default: [...eslintGlob],
3752
})
3853
.action(async (options) => {
39-
const patterns =
40-
typeof options.pattern === "string"
41-
? [options.pattern]
42-
: options.pattern;
43-
await eslintFix(patterns);
54+
const { paths } = await mergeConfig();
55+
const { pattern } = options;
56+
if (pattern) {
57+
await eslintFix(typeof pattern === "string" ? [pattern] : pattern);
58+
} else {
59+
await eslintFix(paths);
60+
}
4461
});
4562
},
4663
};

src/command/eslint-import-sort.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,23 @@ import { ESLint } from "eslint";
66
import fs from "fs-extra";
77

88
import { ACTIVATION, impSortGlob } from "@/config";
9-
import { loggerInfo, printError, printInfo } from "@/helper";
9+
import { loadConfigModule, loggerInfo, printError, printInfo } from "@/helper";
1010
import { ImpSortOptions } from "@/types";
1111

12+
const mergeConfig = async () => {
13+
const config = await loadConfigModule();
14+
const commands = config && config?.commands;
15+
if (commands && commands.impsort) {
16+
const { paths } = commands.impsort;
17+
return {
18+
paths: paths && paths.length > 0 ? paths : impSortGlob,
19+
};
20+
}
21+
return {
22+
paths: impSortGlob,
23+
};
24+
};
25+
1226
const generateEnquirer = async (): Promise<ImpSortOptions> => {
1327
const files = fs
1428
.readdirSync(path.join(process.cwd(), "."))
@@ -20,11 +34,12 @@ const generateEnquirer = async (): Promise<ImpSortOptions> => {
2034
};
2135
});
2236
files.sort((v1, v2) => v1.sort - v2.sort);
37+
const { paths } = await mergeConfig();
2338
const fileMultiChoices = files.map((v) => {
2439
return {
2540
name: `./${v.file}`,
2641
message: `${v.file}`,
27-
hint: impSortGlob.includes(`./${v.file}`) ? "建议尝试修复" : "",
42+
hint: paths.includes(`./${v.file}`) ? "建议尝试修复" : "",
2843
};
2944
});
3045
const result = await enquirer.prompt<ImpSortOptions>([

src/command/git-user.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,29 @@ import type { CAC } from "cac";
33
import { ACTIVATION, gitUserOptions } from "@/config";
44
import {
55
execCommand,
6+
loadConfigModule,
67
loggerInfo,
78
printError,
89
printInfo,
910
printWarring,
1011
} from "@/helper";
1112

13+
const mergeConfig = async () => {
14+
const config = await loadConfigModule();
15+
const commands = config && config?.commands;
16+
if (commands && commands.gituser) {
17+
const { ruleName, ruleEmail } = commands.gituser;
18+
return {
19+
ruleName: ruleName || gitUserOptions.ruleName,
20+
ruleEmail: ruleEmail || gitUserOptions.ruleEmail,
21+
};
22+
}
23+
return {
24+
ruleName: gitUserOptions.ruleName,
25+
ruleEmail: gitUserOptions.ruleEmail,
26+
};
27+
};
28+
1229
export async function setGitUserName(name: string, ruleName: string) {
1330
if (ACTIVATION) {
1431
loggerInfo(
@@ -97,27 +114,20 @@ export default function gitUserInstaller(cli: CAC) {
97114
.alias("gitu")
98115
.option("-n, --name <name>", "设置 user.name")
99116
.option("-e, --email <email>", "设置 user.email")
100-
.option("--rule-name <regexp>", "设置 user.name 匹配规则(转义字符串)", {
101-
default: gitUserOptions.ruleName,
102-
})
103-
.option(
104-
"--rule-email <regexp>",
105-
"设置 user.email 匹配规则(转义字符串)",
106-
{
107-
default: gitUserOptions.ruleEmail,
108-
},
109-
)
117+
.option("--rule-name <regexp>", "设置 user.name 匹配规则(转义字符串)")
118+
.option("--rule-email <regexp>", "设置 user.email 匹配规则(转义字符串)")
110119
.action(async (options) => {
111-
const { name, email, ruleName, ruleEmail } = options;
120+
const { ruleName, ruleEmail } = await mergeConfig();
121+
const { name, email, ruleName: rName, ruleEmail: rEmail } = options;
112122
if (!name && !email) {
113-
await checkGitUserName(ruleName);
114-
await checkGitUserEmail(ruleEmail);
123+
await checkGitUserName(ruleName || rName);
124+
await checkGitUserEmail(ruleEmail || rEmail);
115125
}
116126
if (name) {
117-
await setGitUserName(name, ruleName);
127+
await setGitUserName(name, ruleName || rName);
118128
}
119129
if (email) {
120-
await setGitUserName(email, ruleEmail);
130+
await setGitUserName(email, ruleEmail || rEmail);
121131
}
122132
});
123133
},

src/command/prettier-format.ts

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

33
import { ACTIVATION, formatGlob } from "@/config";
4-
import { execCommand, loggerInfo } from "@/helper";
4+
import { execCommand, loadConfigModule, loggerInfo } from "@/helper";
5+
6+
const mergeConfig = async () => {
7+
const config = await loadConfigModule();
8+
const commands = config && config?.commands;
9+
if (commands && commands.format) {
10+
const { paths } = commands.format;
11+
return {
12+
paths: paths && paths.length > 0 ? paths : formatGlob,
13+
};
14+
}
15+
return {
16+
paths: formatGlob,
17+
};
18+
};
519

620
export const prettierFormat = async (paths: string[]) => {
721
if (ACTIVATION) {
@@ -19,15 +33,17 @@ export default function prettierFormatInstaller(cli: CAC) {
1933
setup: () => {
2034
cli
2135
.command("format", "运行 prettier 格式化代码风格")
22-
.option("-p, --pattern <pattern>", "设置匹配规则", {
23-
default: [...formatGlob],
24-
})
36+
.option("-p, --pattern <pattern>", "设置匹配规则")
2537
.action(async (options) => {
26-
const patterns =
27-
typeof options.pattern === "string"
28-
? [options.pattern]
29-
: options.pattern;
30-
await prettierFormat(patterns);
38+
const { paths } = await mergeConfig();
39+
const { pattern } = options;
40+
if (pattern) {
41+
await prettierFormat(
42+
typeof pattern === "string" ? [pattern] : pattern,
43+
);
44+
} else {
45+
await prettierFormat(paths);
46+
}
3147
});
3248
},
3349
};

src/types.ts

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ export interface CommitOptions {
6363
export interface VerifyOptions {
6464
// TODO
6565
}
66-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
67-
export interface ClearOptions {
68-
// TODO
69-
}
66+
7067
// eslint-disable-next-line @typescript-eslint/no-empty-interface
7168
export interface HooksOptions {
7269
// TODO
@@ -79,10 +76,7 @@ export interface DepcheckOptions {
7976
export interface RegistryOptions {
8077
// TODO
8178
}
82-
// eslint-disable-next-line @typescript-eslint/no-empty-interface
83-
export interface GituserOptions {
84-
// TODO
85-
}
79+
8680
// eslint-disable-next-line @typescript-eslint/no-empty-interface
8781
export interface TemplateOptions {
8882
// TODO
@@ -113,17 +107,11 @@ export interface CodeGeniusOptions {
113107
>;
114108
commands?: {
115109
commit?: CommitOptions;
116-
verify?: VerifyOptions;
117110
clear?: ClearOptions;
118-
hooks?: HooksOptions;
119-
depcheck?: DepcheckOptions;
120-
registry?: RegistryOptions;
121-
gituser?: GituserOptions;
122-
template?: TemplateOptions;
123-
fix?: FixOptions;
124-
format?: FormatOptions;
125-
impsort?: ImpsortOptions;
126-
script?: ScriptOptions;
111+
gituser?: Pick<GitUserOptions, "ruleName" | "ruleEmail">;
112+
fix?: { paths: string[] };
113+
format?: { paths: string[] };
114+
impsort?: { paths: string[] };
127115
};
128116
}
129117

0 commit comments

Comments
 (0)