Skip to content

Commit 953422e

Browse files
committed
✨ refactor(app): 统一内外命令的格式,并支持挂载询问模式
1 parent 7f84bc6 commit 953422e

File tree

11 files changed

+271
-279
lines changed

11 files changed

+271
-279
lines changed

src/command/eslint-fix.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/command/eslint-fix/index.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import type { CAC } from "cac";
2+
3+
import { ACTIVATION, eslintGlob } from "@/config";
4+
import { execCommand } from "@/helper";
5+
import { loggerError, loggerInfo, printError, printInfo } from "@/logger";
6+
import { CommandsOptions } from "@/types";
7+
8+
export const eslintFix = async (paths: string[]) => {
9+
if (ACTIVATION) {
10+
loggerInfo(`eslintFix 参数信息: \n ${paths}`);
11+
}
12+
13+
try {
14+
await execCommand("npx", ["eslint", "--fix", ...paths], {
15+
stdio: "inherit",
16+
});
17+
printInfo("代码已通过 eslint 校验");
18+
} catch (error) {
19+
printError(`代码未通过 eslint 校验`);
20+
loggerError(error);
21+
process.exit(1);
22+
}
23+
};
24+
25+
export default function eslintFixInstaller(config: CommandsOptions) {
26+
const { fix } = config;
27+
return {
28+
name: "fix",
29+
describe: "运行 eslint 静态扫描和修复代码中存在的问题",
30+
command: "fix",
31+
setup: (cli: CAC) => {
32+
cli
33+
.command("fix", "运行 eslint 静态扫描和修复代码中存在的问题")
34+
.option("-p, --pattern <pattern>", "设置匹配规则")
35+
.action(async (options) => {
36+
let paths = fix?.paths || eslintGlob;
37+
const { pattern } = options;
38+
if (pattern) {
39+
paths = typeof pattern === "string" ? [pattern] : pattern;
40+
}
41+
await eslintFix(paths);
42+
});
43+
},
44+
};
45+
}

src/command/git-commit.ts renamed to src/command/git-commit/index.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ import type { CAC } from "cac";
33
import enquirer from "enquirer";
44

55
import { ACTIVATION, gitCommitScopes, gitCommitTypes } from "@/config";
6-
import { execCommand, loadConfigModule, loggerInfo } from "@/helper";
7-
import { GitCommitOptions } from "@/types";
6+
import { execCommand } from "@/helper";
7+
import { loggerInfo } from "@/logger";
8+
import {
9+
CommandsOptions,
10+
CommitScope,
11+
CommitType,
12+
GitCommitOptions,
13+
} from "@/types";
814

915
const schema = {
1016
type: "object",
@@ -16,26 +22,12 @@ const schema = {
1622
required: ["type", "scope", "description"],
1723
};
1824

19-
const mergeConfig = async () => {
20-
const config = await loadConfigModule();
21-
const commands = config && config?.commands;
22-
if (commands && commands.commit) {
23-
const { gitCommitTypes: types, gitCommitScopes: scopes } = commands.commit;
24-
return {
25-
types: types && types.length > 0 ? types : gitCommitTypes,
26-
scopes: scopes && scopes.length > 0 ? scopes : gitCommitScopes,
27-
};
28-
}
29-
return {
30-
types: gitCommitTypes,
31-
scopes: gitCommitScopes,
32-
};
33-
};
34-
35-
const generateEnquirer = async (): Promise<
25+
const generateEnquirer = async (
26+
types: Array<CommitType>,
27+
scopes: Array<CommitScope>,
28+
): Promise<
3629
Pick<GitCommitOptions, Exclude<keyof GitCommitOptions, "emoji">>
3730
> => {
38-
const { types, scopes } = await mergeConfig();
3931
const typesChoices = types.map(({ emoji, code, description }) => {
4032
const formatCode = `${code}:`.padEnd(20);
4133
return {
@@ -116,10 +108,13 @@ export const gitCommit = async (
116108
);
117109
};
118110

119-
export default function gitCommitInstaller(cli: CAC) {
111+
export default function gitCommitInstaller(config: CommandsOptions) {
112+
const { commit } = config;
120113
return {
121-
name: "gitCommitInstaller",
122-
setup: () => {
114+
name: "commit",
115+
describe: "生成 angualr 规范的提交信息",
116+
command: "commit --ask",
117+
setup: (cli: CAC) => {
123118
cli
124119
.command("commit", "生成 angualr 规范的提交信息")
125120
.option("-t, --type <type>", "添加修改类型")
@@ -128,8 +123,10 @@ export default function gitCommitInstaller(cli: CAC) {
128123
.option("-a, --ask", "启用询问模式")
129124
.action(async (options) => {
130125
const { type, scope, description, ask } = options;
126+
const types = commit?.gitCommitTypes || gitCommitTypes;
127+
const scopes = commit?.gitCommitScopes || gitCommitScopes;
131128
if (ask) {
132-
const result = await generateEnquirer();
129+
const result = await generateEnquirer(types, scopes);
133130
await gitCommit(result.type, result.scope, result.description);
134131
} else {
135132
await gitCommit(type, scope, description);

src/command/root.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import type { CAC } from "cac";
22
import enquirer from "enquirer";
33
import updateNotifier from "simple-update-notifier";
44

5-
import { commands } from "@/config";
65
import { execCommand } from "@/helper";
6+
import { CommandOptions } from "@/types";
77

88
import pkg from "../../package.json";
99

1010
interface PromptResult {
1111
command: string;
1212
}
1313

14-
export const root = async () => {
14+
export const root = async (commands: Array<CommandOptions>) => {
1515
updateNotifier({ pkg });
1616
const commandChoices = commands.map(({ display, command, description }) => {
1717
const formatCommand = `${display || command}`.padEnd(15);
@@ -31,16 +31,16 @@ export const root = async () => {
3131
await execCommand("codeg", result.command.split(" "), { stdio: "inherit" });
3232
};
3333

34-
export default function rootInstaller(cli: CAC) {
34+
export default function rootInstaller(commands: Array<CommandOptions>) {
3535
return {
3636
name: "rootInstaller",
37-
setup: () => {
37+
setup: (cli: CAC) => {
3838
cli
3939
.command("[root]", "启动 CodeGenius 命令行选项模式 ")
4040
.alias("start")
4141
.alias("dev")
4242
.action(async () => {
43-
await root();
43+
await root(commands);
4444
});
4545
},
4646
};

src/command/script.ts renamed to src/command/script/index.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { CAC } from "cac";
44
import enquirer from "enquirer";
55
import fsExtra from "fs-extra";
66

7-
import { generateScripts } from "@/helper";
7+
import { generateScripts } from "@/command/script/utils";
88
import { execCommand } from "@/helper";
99
import { CommandOptions } from "@/types";
1010

@@ -14,10 +14,10 @@ export async function scriptRun() {
1414
);
1515

1616
const scriptChoices = scripts.map((script: CommandOptions) => {
17-
const formatCmd = `${script.cmd}`.padEnd(15);
17+
const formatCmd = `${script.display}`.padEnd(15);
1818
return {
19-
name: script.script,
20-
message: `${formatCmd} ${script.desc}`,
19+
name: script.command,
20+
message: `${formatCmd} ${script.description}`,
2121
};
2222
});
2323

@@ -44,10 +44,12 @@ export async function scriptRun() {
4444
}
4545
}
4646

47-
export default function scriptRunInstaller(cli: CAC) {
47+
export default function scriptRunInstaller() {
4848
return {
49-
name: "scriptRunInstaller",
50-
setup: () => {
49+
name: "script",
50+
describe: "代理运行 package.scripts 脚本",
51+
command: "script",
52+
setup: (cli: CAC) => {
5153
cli
5254
.command("script", "代理运行 package.scripts 脚本")
5355
.action(async () => {

src/command/script/utils.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import fs from "node:fs";
2+
import path from "node:path";
3+
4+
import fsExtra from "fs-extra";
5+
6+
import { printInfo } from "@/logger";
7+
import { CommandOptions } from "@/types";
8+
9+
/**
10+
* 用于转换 scripts 结构
11+
* @param scripts
12+
* @returns
13+
*/
14+
export function genScriptConfig(scripts: { [key: string]: string }) {
15+
return Object.keys(scripts).map((key) => {
16+
return {
17+
command: key,
18+
display: scripts[key],
19+
description: "description the function of this cmd command",
20+
};
21+
});
22+
}
23+
24+
/**
25+
* 用于合并两份 scripts.config.json 数据
26+
* @param pkgScripts
27+
* @param configScripts
28+
* @returns
29+
*/
30+
export function syncScripts(
31+
pkgScripts: Array<CommandOptions>,
32+
configScripts: Array<CommandOptions>,
33+
) {
34+
const mergedScripts = [...configScripts];
35+
36+
for (const pkgScript of pkgScripts) {
37+
const configScript = mergedScripts.find(
38+
(i) => i.command === pkgScript.command,
39+
);
40+
41+
if (configScript) {
42+
if (configScript.display !== pkgScript.display) {
43+
configScript.display = pkgScript.display;
44+
}
45+
} else {
46+
mergedScripts.push(pkgScript);
47+
}
48+
}
49+
50+
const scripts = mergedScripts.filter((configScript) => {
51+
return pkgScripts.find((i) => i.command === configScript.command);
52+
});
53+
54+
return scripts;
55+
}
56+
57+
/**
58+
* 读取 package.json 解析并生成 scripts.config.json 配置文件
59+
*/
60+
export const generateScripts = async () => {
61+
const pkg = await fsExtra.readJSONSync(
62+
path.join(process.cwd(), "package.json"),
63+
);
64+
let configContent = genScriptConfig(pkg.scripts);
65+
const configfile = path.join(process.cwd(), "scripts.config.json");
66+
if (fs.existsSync(configfile)) {
67+
const { scripts } = await fsExtra.readJSONSync(configfile);
68+
configContent = syncScripts(configContent, scripts);
69+
}
70+
await fsExtra.outputFileSync(
71+
configfile,
72+
JSON.stringify(
73+
{
74+
scripts: configContent,
75+
},
76+
null,
77+
2,
78+
),
79+
);
80+
printInfo("代理脚本 scripts.config.json 已完成同步");
81+
};

src/config.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,28 +92,16 @@ export const gitCommitScopes: Array<CommitScope> = [
9292
},
9393
];
9494

95-
export const commands = [
95+
export const defaultCommands = [
9696
{
97-
command: "commit --ask",
98-
description: "生成 angualr 规范的提交信息",
99-
},
100-
{
101-
command: "fix",
102-
description: "运行 eslint 静态扫描和修复代码中存在的问题",
103-
},
104-
{
105-
command: "script",
106-
description: "代理执行 package.scripts 脚本",
97+
display: "version",
98+
command: "--version",
99+
description: "查看版本号",
107100
},
108101
{
109102
display: "help",
110103
command: "--help",
111-
description: "查看 CodeGenius 终端命令",
112-
},
113-
{
114-
display: "version",
115-
command: "--version",
116-
description: "查看 CodeGenius 版本信息",
104+
description: "查看帮助信息",
117105
},
118106
];
119107

0 commit comments

Comments
 (0)