Skip to content

Commit 2ec0440

Browse files
committed
✨refactor(feature): 添加 create,为拉取代码准备入口
1 parent afa0367 commit 2ec0440

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

src/command/create-project.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import enquirer from "enquirer";
2+
3+
import { execCommand, loggerInfo } from "@/shared/index";
4+
import { ACTIVATION } from "@/shared/config";
5+
import { ProjectSource } from "@/shared/types";
6+
7+
interface PromptResult {
8+
command: string;
9+
}
10+
11+
export const createProject = async (sources: ProjectSource[]) => {
12+
if (ACTIVATION) {
13+
loggerInfo("createProject 参数信息: \n");
14+
console.table(sources);
15+
}
16+
17+
const sourceChoices = sources.map(({ name, description }) => {
18+
const formatName = `${name}:`.padEnd(15);
19+
return {
20+
name: name,
21+
message: `${formatName}${description}`,
22+
};
23+
});
24+
25+
const result = await enquirer.prompt<PromptResult>([
26+
{
27+
name: "command",
28+
type: "select",
29+
message: "请选择提交类型",
30+
choices: sourceChoices,
31+
},
32+
]);
33+
34+
await execCommand("npm", ["create", result.command], {
35+
stdio: "inherit",
36+
});
37+
};

src/setup.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
formatGlob,
1010
gitCommitScopes,
1111
gitCommitTypes,
12+
projectSources,
1213
} from "./shared/config";
1314

1415
import { CommandSet } from "./shared/types";
@@ -20,6 +21,7 @@ import { clear } from "./command/clear";
2021
import { npmRun } from "./command/npm-run";
2122
import { npmDepCheck } from "./command/npm-dep-check";
2223
import { npmRegistry } from "./command/npm-registry";
24+
import { createProject } from "./command/create-project";
2325

2426
export const commandSet: CommandSet = {
2527
gitCommitCmd: (cli: CAC) => {
@@ -109,4 +111,11 @@ export const commandSet: CommandSet = {
109111
await prettierFormat(patterns);
110112
});
111113
},
114+
createProjectCmd: (cli: CAC) => {
115+
cli
116+
.command("create", "运行 npm create 快速创建基础项目")
117+
.action(async () => {
118+
await createProject(projectSources);
119+
});
120+
},
112121
};

src/shared/config.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { CommitScope, CommitType, KeyValue } from "@/shared/types";
1+
import {
2+
CommitScope,
3+
CommitType,
4+
KeyValue,
5+
ProjectSource,
6+
} from "@/shared/types";
27

38
export const clearGlob = ["./dist/"];
49

@@ -122,3 +127,18 @@ export const npmRegisters: Array<KeyValue> = [
122127
value: "https://skimdb.npmjs.com/registry/",
123128
},
124129
];
130+
131+
export const projectSources: Array<ProjectSource> = [
132+
{
133+
name: "vite@latest",
134+
description: "创建由 Vite 驱动的新的项目",
135+
},
136+
{
137+
name: "vue@latest",
138+
description: "创建由 Vite 驱动的 Vue3 项目",
139+
},
140+
{
141+
name: "vue@legacy",
142+
description: "创建由 Vite 驱动的 Vue2 项目(支持 IE11)",
143+
},
144+
];

src/shared/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ export interface KeyValue {
1919
key: string;
2020
value: string;
2121
}
22+
23+
export interface ProjectSource {
24+
name: string;
25+
description: string;
26+
}

0 commit comments

Comments
 (0)