Skip to content

Commit 504844d

Browse files
committed
✨refactor(core): 重构注册方式
1 parent c7a4327 commit 504844d

12 files changed

+205
-130
lines changed

src/cli.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
import cac, { CAC } from "cac";
22

33
import { handleError } from "@/shared/index";
4-
import { commandSet } from "@/setup";
54

65
import { version } from "../package.json";
6+
import { cmdInstaller } from "./setup";
77

88
function welcome(cli: CAC) {
99
cli.outputHelp();
1010
}
1111

1212
export const setupCli = async () => {
1313
const cli = cac("cg");
14+
cli.command("");
1415

15-
Object.keys(commandSet).forEach((key) => {
16-
commandSet[key](cli);
17-
});
16+
cmdInstaller(cli);
1817

1918
cli.help();
2019
cli.version(version);

src/command/clear.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import type { CAC } from "cac";
2+
13
import { execCommand, loggerInfo } from "@/shared/index";
2-
import { ACTIVATION } from "@/shared/config";
4+
import { ACTIVATION, clearGlob } from "@/shared/config";
35

46
export const clear = async (paths: string[]) => {
57
if (ACTIVATION) {
@@ -10,3 +12,23 @@ export const clear = async (paths: string[]) => {
1012
stdio: "inherit",
1113
});
1214
};
15+
16+
export default function clearInstaller(cli: CAC) {
17+
return {
18+
name: "clearInstaller",
19+
setup: () => {
20+
cli
21+
.command("clear", "运行 rimraf 删除不再需要的文件或文件夹")
22+
.option("-p, --pattern <pattern>", "设置配置规则", {
23+
default: [...clearGlob],
24+
})
25+
.action(async (options) => {
26+
const patterns =
27+
typeof options.pattern === "string"
28+
? [options.pattern]
29+
: options.pattern;
30+
await clear(patterns);
31+
});
32+
},
33+
};
34+
}

src/command/create-project.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import type { CAC } from "cac";
2+
13
import enquirer from "enquirer";
24

35
import { execCommand, loggerInfo } from "@/shared/index";
4-
import { ACTIVATION } from "@/shared/config";
6+
import { ACTIVATION, projectSources } from "@/shared/config";
57
import { ProjectSource } from "@/shared/types";
68

79
interface PromptResult {
@@ -35,3 +37,16 @@ export const createProject = async (sources: ProjectSource[]) => {
3537
stdio: "inherit",
3638
});
3739
};
40+
41+
export default function createProjectInstaller(cli: CAC) {
42+
return {
43+
name: "createProjectInstaller",
44+
setup: () => {
45+
cli
46+
.command("create", "运行 npm create 快速创建基础项目")
47+
.action(async () => {
48+
await createProject(projectSources);
49+
});
50+
},
51+
};
52+
}

src/command/eslint-fix.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import type { CAC } from "cac";
2+
13
import { execCommand, loggerInfo } from "@/shared/index";
2-
import { ACTIVATION } from "@/shared/config";
4+
import { ACTIVATION, eslintGlob } from "@/shared/config";
35

46
export const eslintFix = async (paths: string[]) => {
57
if (ACTIVATION) {
@@ -15,3 +17,23 @@ export const eslintFix = async (paths: string[]) => {
1517
},
1618
);
1719
};
20+
21+
export default function eslintFixInstaller(cli: CAC) {
22+
return {
23+
name: "eslintFixInstaller",
24+
setup: () => {
25+
cli
26+
.command("fix", "运行 eslint 静态扫描和修复代码中存在的问题")
27+
.option("-p, --pattern <pattern>", "设置匹配规则", {
28+
default: [...eslintGlob],
29+
})
30+
.action(async (options) => {
31+
const patterns =
32+
typeof options.pattern === "string"
33+
? [options.pattern]
34+
: options.pattern;
35+
await eslintFix(patterns);
36+
});
37+
},
38+
};
39+
}

src/command/git-commit-verify.ts

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

@@ -17,3 +19,16 @@ export const gitCommitVerify = async () => {
1719
process.exit(1);
1820
}
1921
};
22+
23+
export default function gitCommitVerifyInstaller(cli: CAC) {
24+
return {
25+
name: "gitCommitVerifyInstaller",
26+
setup: () => {
27+
cli
28+
.command("verify", "校验 COMMIT_EDITMSG 中的信息是否符合 Angualr 规范")
29+
.action(async () => {
30+
await gitCommitVerify();
31+
});
32+
},
33+
};
34+
}

src/command/git-commit.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import type { CAC } from "cac";
2+
13
import enquirer from "enquirer";
24

3-
import { ACTIVATION } from "@/shared/config";
5+
import { ACTIVATION, gitCommitScopes, gitCommitTypes } from "@/shared/config";
46
import { CommitScope, CommitType } from "@/shared/types";
57
import { loggerInfo, execCommand } from "@/shared/index";
68
interface PromptResult {
@@ -64,3 +66,20 @@ export const gitCommit = async (
6466
const content = `${result.type}(${result.scope}): ${result.description}`;
6567
await execCommand("git", ["commit", "-m", content], { stdio: "inherit" });
6668
};
69+
70+
export default function gitCommitInstaller(cli: CAC) {
71+
return {
72+
name: "gitCommitInstaller",
73+
setup: () => {
74+
cli
75+
.command("commit", "生成 angualr 规范的提交信息")
76+
.option("--no-emoji", "禁用 emoji")
77+
.action(async (options) => {
78+
const { emoji } = options;
79+
await gitCommit(gitCommitTypes, gitCommitScopes, {
80+
emoji,
81+
});
82+
});
83+
},
84+
};
85+
}

src/command/git-init-hooks.ts

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

@@ -20,3 +22,16 @@ export const gitInitSimpleHooks = async (cwd = process.cwd()) => {
2022
});
2123
await execCommand("npx", ["simple-git-hooks"], { stdio: "inherit" });
2224
};
25+
26+
export default function gitInitSimpleHooksInstaller(cli: CAC) {
27+
return {
28+
name: "gitInitSimpleHooksInstaller",
29+
setup: () => {
30+
cli
31+
.command("hooks", "新增或修改 simple-git-hooks 配置后需要重新初始化")
32+
.action(async () => {
33+
await gitInitSimpleHooks();
34+
});
35+
},
36+
};
37+
}

src/command/npm-dep-check.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1+
import type { CAC } from "cac";
2+
13
import { execCommand } from "@/shared/index";
24

35
export const npmDepCheck = async () => {
46
await execCommand("npx", ["npm-check"], {
57
stdio: "inherit",
68
});
79
};
10+
11+
export default function npmDepCheckInstaller(cli: CAC) {
12+
return {
13+
name: "npmDepCheckInstaller",
14+
setup: () => {
15+
cli
16+
.command(
17+
"depcheck",
18+
"运行 npm-check 检查过时的、不正确的和未使用的依赖项",
19+
)
20+
.action(async () => {
21+
await npmDepCheck();
22+
});
23+
},
24+
};
25+
}

src/command/npm-registry.ts

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

35
import { printInfo, printWarring, execCommand } from "@/shared/index";
@@ -44,3 +46,14 @@ export const npmRegistry = async () => {
4446

4547
await printCurrentRegistry();
4648
};
49+
50+
export default function npmRegistryInstaller(cli: CAC) {
51+
return {
52+
name: "npmRegistryInstaller",
53+
setup: () => {
54+
cli.command("registry", "切换 NPM 镜像地址").action(async () => {
55+
await npmRegistry();
56+
});
57+
},
58+
};
59+
}

src/command/npm-run.ts

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

@@ -32,3 +34,14 @@ export const npmRun = async (cwd = process.cwd()) => {
3234
execCommand(cmd, args, { stdio: "inherit" });
3335
}
3436
};
37+
38+
export default function npmRunInstaller(cli: CAC) {
39+
return {
40+
name: "npmRunInstaller",
41+
setup: () => {
42+
cli.command("run", "列出可以运行的全部脚本").action(async () => {
43+
await npmRun();
44+
});
45+
},
46+
};
47+
}

0 commit comments

Comments
 (0)