Skip to content

Commit da831c2

Browse files
committed
🔧 chore(app): 尝试通过配置文件的方式支持自定义使用
1 parent 592b7b6 commit da831c2

File tree

6 files changed

+124
-9
lines changed

6 files changed

+124
-9
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.release-it.js
22
build.config.ts
3-
./templates/
3+
./templates/
4+
dist

src/command/git-commit.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,38 @@ import type { CAC } from "cac";
22
import enquirer from "enquirer";
33

44
import { ACTIVATION, gitCommitScopes, gitCommitTypes } from "@/config";
5-
import { execCommand, loggerInfo } from "@/helper";
5+
import { execCommand, loadConfigModule, loggerInfo } from "@/helper";
66
import { GitCommitOptions } from "@/types";
77

8+
const mergeConfig = async () => {
9+
const config = await loadConfigModule();
10+
const commands = config && config?.commands;
11+
if (commands && commands.commit) {
12+
const { gitCommitTypes: types, gitCommitScopes: scopes } = commands.commit;
13+
return {
14+
types: types && types.length > 0 ? types : gitCommitTypes,
15+
scopes: scopes && scopes.length > 0 ? scopes : gitCommitScopes,
16+
};
17+
}
18+
return {
19+
types: gitCommitTypes,
20+
scopes: gitCommitScopes,
21+
};
22+
};
23+
824
const generateEnquirer = async (): Promise<
925
Pick<GitCommitOptions, Exclude<keyof GitCommitOptions, "emoji">>
1026
> => {
11-
const typesChoices = gitCommitTypes.map(({ emoji, code, description }) => {
27+
const { types, scopes } = await mergeConfig();
28+
const typesChoices = types.map(({ emoji, code, description }) => {
1229
const formatCode = `${code}:`.padEnd(20);
1330
return {
1431
name: `${emoji} ${code}`,
1532
message: `${emoji}${formatCode}${description}`,
1633
};
1734
});
1835

19-
const scopesChoices = gitCommitScopes.map(({ name, description }) => {
36+
const scopesChoices = scopes.map(({ name, description }) => {
2037
const formatName = `${name}:`.padEnd(20);
2138
return {
2239
name,

src/config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,12 @@ export const commands = [
271271
description: "查看 CodeGenius 版本信息",
272272
},
273273
];
274+
275+
export const DEFAULT_CONFIG_FILES = [
276+
"codeg.config.js",
277+
"codeg.config.mjs",
278+
"codeg.config.ts",
279+
"codeg.config.mts",
280+
"codeg.config.cjs",
281+
"codeg.config.cts",
282+
];

src/helper.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import execa from "execa";
99
import fsExtra from "fs-extra";
1010
import { gray, green, red, yellow } from "kolorist";
1111

12-
import { ACTIVATION, FRAMEWORKS, TEMPLATES } from "@/config";
12+
import {
13+
ACTIVATION,
14+
DEFAULT_CONFIG_FILES,
15+
FRAMEWORKS,
16+
TEMPLATES,
17+
} from "@/config";
1318

1419
import { CodeGeniusOptions, CommandOptions } from "./types";
1520

@@ -294,8 +299,10 @@ export const defineConfig = (config: CodeGeniusOptions): CodeGeniusOptions =>
294299

295300
export async function cmdInstaller(cli: CAC, config: CodeGeniusOptions) {
296301
const { plugins } = config;
297-
for (const plugin of plugins) {
298-
plugin(cli).setup();
302+
if (plugins) {
303+
for (const plugin of plugins) {
304+
plugin(cli).setup();
305+
}
299306
}
300307
}
301308

@@ -358,3 +365,19 @@ export const generateScripts = async () => {
358365
);
359366
printInfo("代理脚本 scripts.config.json 已完成同步");
360367
};
368+
369+
export async function loadConfigModule(): Promise<
370+
CodeGeniusOptions | undefined
371+
> {
372+
let resolvedPath: string | undefined;
373+
for (const filename of DEFAULT_CONFIG_FILES) {
374+
const filePath = path.resolve(process.cwd(), filename);
375+
if (!fs.existsSync(filePath)) continue;
376+
resolvedPath = filePath;
377+
break;
378+
}
379+
380+
if (!resolvedPath) return;
381+
382+
return (await import(resolvedPath)).default;
383+
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ const config = defineConfig({
5050
],
5151
});
5252

53+
export { config, defineConfig };
54+
5355
export {
5456
checkGitUserEmail,
5557
checkGitUserName,
5658
clear,
57-
config,
5859
createProject,
5960
eslintFix,
6061
gitCommit,

src/types.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,77 @@ export interface GitUserOptions {
5454
ruleEmail?: string;
5555
}
5656

57+
export interface CommitOptions {
58+
gitCommitTypes?: Array<CommitType>;
59+
gitCommitScopes?: Array<CommitScope>;
60+
}
61+
62+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
63+
export interface VerifyOptions {
64+
// TODO
65+
}
66+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
67+
export interface ClearOptions {
68+
// TODO
69+
}
70+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
71+
export interface HooksOptions {
72+
// TODO
73+
}
74+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
75+
export interface DepcheckOptions {
76+
// TODO
77+
}
78+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
79+
export interface RegistryOptions {
80+
// TODO
81+
}
82+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
83+
export interface GituserOptions {
84+
// TODO
85+
}
86+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
87+
export interface TemplateOptions {
88+
// TODO
89+
}
90+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
91+
export interface FixOptions {
92+
// TODO
93+
}
94+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
95+
export interface FormatOptions {
96+
// TODO
97+
}
98+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
99+
export interface ImpsortOptions {
100+
// TODO
101+
}
102+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
103+
export interface ScriptOptions {
104+
// TODO
105+
}
106+
57107
export interface CodeGeniusOptions {
58-
plugins: Array<
108+
plugins?: Array<
59109
(cli: CAC) => {
60110
name: string;
61111
setup: () => void;
62112
}
63113
>;
114+
commands?: {
115+
commit?: CommitOptions;
116+
verify?: VerifyOptions;
117+
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;
127+
};
64128
}
65129

66130
export interface GitCommitOptions {

0 commit comments

Comments
 (0)