prompts.ts
4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import type { CreateOptions } from "../types";
import fs from "node:fs";
import fsp from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { cancel, confirm, isCancel, select, text } from "@clack/prompts";
export async function runCreateInteractive(): Promise<CreateOptions> {
// 步骤1: 输入项目名
const projectName = await text({
message: "请输入项目名称:",
placeholder: "venus-my-app",
validate(value) {
if (!value || value.trim().length === 0) {
return "项目名称不能为空";
}
if (!/^[a-z0-9-]+$/.test(value)) {
return "项目名称只能包含小写字母、数字和连字符";
}
},
});
if (!projectName) {
cancel("项目名称不能为空");
throw new Error("项目名称不能为空");
}
if (isCancel(projectName)) {
cancel("操作已取消");
throw new Error("操作已取消");
}
// 步骤2: 选择模板类型
const template = await select<string>({
message: "请选择模板类型:",
options: [
{ value: "vue", label: "Vue (PC 管理后台)" },
{ value: "mobile", label: "Mobile (移动端 H5)" },
{ value: "react", label: "React" },
],
});
if (isCancel(template) || typeof template !== "string") {
cancel("操作已取消");
throw new Error("操作已取消");
}
// 步骤3: 输入构建目录
const defaultDirPrefix = (() => {
if (template === "react") return path.join("apps", "frontend", "react");
if (template === "vue") return path.join("apps", "frontend", "vue");
if (template === "mobile") return path.join("apps", "frontend", "mobile");
return "";
})();
const dirInput = await text({
message: "请输入构建目录前缀:",
placeholder: defaultDirPrefix,
initialValue: defaultDirPrefix,
});
if (isCancel(dirInput)) {
cancel("操作已取消");
throw new Error("操作已取消");
}
// 步骤4: 选择是否安装依赖
const shouldInstall = await confirm({
message: "是否立即安装依赖?",
initialValue: true,
});
if (isCancel(shouldInstall)) {
cancel("操作已取消");
throw new Error("操作已取消");
}
// 步骤5: 选择可选功能 - Axios
const hasAxios = await confirm({
message: "是否启用 HTTP 请求库 (Axios)?",
initialValue: false,
});
if (isCancel(hasAxios)) {
cancel("操作已取消");
throw new Error("操作已取消");
}
// 步骤6: 选择可选功能 - Router
const hasRouter = await confirm({
message: "是否启用路由 (Router)?",
initialValue: false,
});
if (isCancel(hasRouter)) {
cancel("操作已取消");
throw new Error("操作已取消");
}
// 步骤7: 选择可选功能 - Store
const hasStore = await confirm({
message: "是否启用状态管理 (Store)?",
initialValue: false,
});
if (isCancel(hasStore)) {
cancel("操作已取消");
throw new Error("操作已取消");
}
// 步骤8: 选择 UI 库
const runtimeDir = path.dirname(fileURLToPath(import.meta.url));
const tplRoot = path.resolve(runtimeDir, "../templates", template);
const uiDir = path.join(tplRoot, "optional", "ui");
let selectedUi: string | undefined;
if (fs.existsSync(uiDir)) {
const uiOptions = await fsp.readdir(uiDir, { withFileTypes: true });
const availableUis = uiOptions.filter((u) => u.isDirectory()).map((u) => ({ value: u.name, label: u.name }));
if (availableUis.length > 0) {
const uiChoice = await select<string>({
message: "请选择 UI 库 (可选):",
options: [ { value: "", label: "不使用 UI 库" }, ...availableUis ],
});
if (isCancel(uiChoice) || typeof uiChoice !== "string") {
cancel("操作已取消");
throw new Error("操作已取消");
}
selectedUi = uiChoice === "" ? undefined : uiChoice;
}
}
// 步骤9: 选择 ESLint
const hasEslint = await confirm({
message: "是否启用 ESLint?",
initialValue: false,
});
if (isCancel(hasEslint)) {
cancel("操作已取消");
throw new Error("操作已取消");
}
// 返回配置
return {
name: String(projectName),
template: String(template),
dir: dirInput ? String(dirInput) : undefined,
noInstall: !shouldInstall,
axios: Boolean(hasAxios),
router: Boolean(hasRouter),
store: Boolean(hasStore),
ui: selectedUi,
eslint: Boolean(hasEslint),
};
}