Skip to content

Commit 69d729a

Browse files
committed
✨refactor(app): 重构命令已codeg启动,并增加交互式启动方式
1 parent 778221b commit 69d729a

File tree

7 files changed

+118
-24
lines changed

7 files changed

+118
-24
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ npm i -g code-genius
2525
| clear | --pattern \<pattern\> | './dist/' | 运行 rimraf 删除不再需要的文件或文件夹 |
2626
| hooks | -- | -- | 新增或修改 simple-git-hooks 配置后需要重新初始化 |
2727
| depcheck | -- | -- | 运行 npm-check 检查过时的、不正确的和未使用的依赖项 |
28-
| run | -- | -- | 列出可以运行的全部脚本 |
2928
| registry | -- | -- | 切换 NPM 镜像地址 |
3029
| fix | --pattern \<pattern\> | './src' | 运行 eslint 静态扫描和修复代码中存在的问题 |
3130
| format | --pattern \<pattern\> | './src' | 运行 prettier 格式化代码风格 |

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"types": "dist/index.d.ts",
3232
"bin": {
3333
"cg": "./bin/codeGenius.mjs",
34+
"codeg": "./bin/codeGenius.mjs",
3435
"code-genius": "./bin/codeGenius.mjs"
3536
},
3637
"files": [

src/cli.ts

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,17 @@
1-
import cac, { CAC } from "cac";
1+
import cac from "cac";
22

33
import { handleError } from "@/shared/index";
44

55
import pkg from "../package.json";
66
import { cmdInstaller } from "./setup";
77

8-
import updateNotifier from "simple-update-notifier";
9-
10-
function welcome(cli: CAC) {
11-
cli.outputHelp();
12-
}
13-
148
export const setupCli = async () => {
15-
const cli = cac("cg");
16-
cli.command("");
9+
const cli = cac("codeg");
1710

1811
cmdInstaller(cli);
1912

2013
cli.help();
2114
cli.version(pkg.version);
22-
cli.command("update", "检测 CodeGenius 版本").action(() => {
23-
updateNotifier({ pkg, alwaysRun: true });
24-
});
25-
26-
cli.on("command:!", () => {
27-
welcome(cli);
28-
});
29-
30-
cli.on("command:*", () => {
31-
welcome(cli);
32-
});
3315

3416
cli.parse(process.argv, { run: false });
3517

src/command/root.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import type { CAC } from "cac";
2+
3+
import enquirer from "enquirer";
4+
import updateNotifier from "simple-update-notifier";
5+
6+
import { execCommand } from "@/shared/index";
7+
import { commands } from "@/shared/config";
8+
import pkg from "../../package.json";
9+
10+
interface PromptResult {
11+
command: string;
12+
}
13+
14+
export const root = async () => {
15+
updateNotifier({ pkg });
16+
const commandChoices = commands.map(({ display, command, description }) => {
17+
const formatCommand = `${display || command}`.padEnd(15);
18+
return {
19+
name: command,
20+
message: `${formatCommand}${description}`,
21+
};
22+
});
23+
const result = await enquirer.prompt<PromptResult>([
24+
{
25+
name: "command",
26+
type: "select",
27+
message: "请选择正确执行的 CodeG 命令",
28+
choices: commandChoices,
29+
},
30+
]);
31+
await execCommand("codeg", [result.command], { stdio: "inherit" });
32+
};
33+
34+
export default function rootInstaller(cli: CAC) {
35+
return {
36+
name: "rootInstaller",
37+
setup: () => {
38+
cli
39+
.command("[root]", "启动 CodeGenius 命令行选项模式 ")
40+
.alias("start")
41+
.alias("dev")
42+
.action(async () => {
43+
await root();
44+
});
45+
},
46+
};
47+
}

src/setup.ts

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

3+
import rootInstaller from "./command/root";
34
import createProjectInstaller from "./command/create-project";
45
import clearInstaller from "./command/clear";
56
import gitCommitInstaller from "./command/git-commit";
67
import gitCommitVerifyInstaller from "./command/git-commit-verify";
78
import gitInitSimpleHooksInstaller from "./command/git-init-hooks";
89
import npmDepCheckInstaller from "./command/npm-dep-check";
9-
import npmRunInstaller from "./command/npm-run";
1010
import npmRegistryInstaller from "./command/npm-registry";
1111
import eslintFixInstaller from "./command/eslint-fix";
1212
import prettierFormatInstaller from "./command/prettier-format";
@@ -16,6 +16,7 @@ import gitUserInstaller from "./command/git-user";
1616
import quantityInstaller from "./command/quantity";
1717

1818
export function cmdInstaller(cli: CAC) {
19+
rootInstaller(cli).setup();
1920
gitCommitInstaller(cli).setup();
2021
gitCommitVerifyInstaller(cli).setup();
2122
gitUserInstaller(cli).setup();
@@ -29,5 +30,4 @@ export function cmdInstaller(cli: CAC) {
2930
templateInstaller(cli).setup();
3031
lighthouseInstaller(cli).setup();
3132
quantityInstaller(cli).setup();
32-
npmRunInstaller(cli).setup();
3333
}

src/shared/config.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,3 +190,68 @@ export const gitUserOptions: GitUserOptions = {
190190
ruleEmail:
191191
"^[a-zA-Z0-9._%+-]+@(163|qq|126|139|sina|sohu|yeah|gmail)\\.(com|net)$",
192192
};
193+
194+
export const commands = [
195+
{
196+
command: "commit",
197+
description: "生成 angualr 规范的提交信息",
198+
},
199+
{
200+
command: "verify",
201+
description: "校验 COMMIT_EDITMSG 中的信息是否符合 Angualr 规范",
202+
},
203+
{
204+
command: "git-user",
205+
description: "设置或校验 git user 信息是否规范",
206+
},
207+
{
208+
command: "hooks",
209+
description: "新增或修改 simple-git-hooks 配置后需要重新初始化",
210+
},
211+
{
212+
command: "registry",
213+
description: "切换 NPM 镜像地址",
214+
},
215+
{
216+
command: "clear",
217+
description: "运行 rimraf 删除不再需要的文件或文件夹",
218+
},
219+
{
220+
command: "depcheck",
221+
description: "运行 depcheck 检查过时的、不正确的和未使用的依赖项",
222+
},
223+
{
224+
command: "fix",
225+
description: "运行 eslint 静态扫描和修复代码中存在的问题",
226+
},
227+
{
228+
command: "format",
229+
description: "运行 prettier 格式化代码风格",
230+
},
231+
{
232+
command: "create",
233+
description: "运行 npm create 快速创建基础项目",
234+
},
235+
{
236+
command: "template",
237+
description: "运行 cg 生成 CodeGenius 内置模板项目",
238+
},
239+
{
240+
command: "lighthouse",
241+
description: "运行 lighthouse 分析及收集 Web 应用的性能指标",
242+
},
243+
{
244+
command: "quantity",
245+
description: "运行 cloc 分析并统计代码量",
246+
},
247+
{
248+
display: "help",
249+
command: "--help",
250+
description: "查看 CodeGenius 终端命令",
251+
},
252+
{
253+
display: "version",
254+
command: "--version",
255+
description: "查看 CodeGenius 版本信息",
256+
},
257+
];

src/shared/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const boxenBorderStyle = {
2121
top: "-",
2222
bottom: "-",
2323
left: "|",
24-
right: "",
24+
right: "|",
2525
},
2626
};
2727

0 commit comments

Comments
 (0)