-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
143 lines (127 loc) · 3.14 KB
/
index.ts
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
#!/usr/bin/env node
import chalk from "chalk";
import figlet from "figlet";
import fs from "fs-extra";
import path from "node:path";
import { fileURLToPath } from "node:url";
import ora from "ora";
import prompts from "prompts";
import {
createTemplate,
getSupportedBuildTools,
getSupportedFrameworks,
getSupportedRouters,
getSupportedStores,
TemplateConfig,
} from "./common/index.js";
const spinner = ora({
color: "green",
spinner: "dots",
});
console.info(chalk.green(figlet.textSync("M o r i", { width: 100 })));
// 项目名
const { projectName } = await prompts({
type: "text",
name: "projectName",
message: `Project name : `,
initial: "mori-project",
});
if (!projectName) {
cancel();
}
// 选择构建工具
const { buildTool } = await prompts({
type: "select",
name: "buildTool",
message: `Build Tool : }`,
choices: getSupportedBuildTools(),
});
// 选择框架
const { framework } = await prompts({
type: "select",
name: "framework",
message: `Framework : `,
choices: getSupportedFrameworks(),
});
// 选择状态管理库
const supportedStores = getSupportedStores(framework);
let store = null;
if (supportedStores.length) {
// 是否需要Store
const { needStore } = await prompts({
type: "confirm",
name: "needStore",
message: `Need a store ? `,
});
if (needStore) {
const storeRes = await prompts({
type: "select",
name: "store",
message: `Store : `,
choices: supportedStores,
});
store = storeRes.store;
}
}
// 选择路由库
const supportedRouters = getSupportedRouters(framework);
let router = null;
if (supportedRouters.length) {
const { needRouter } = await prompts({
type: "confirm",
name: "needRouter",
message: `Need a router ?`,
});
if (needRouter) {
const routerRes = await prompts({
type: "select",
name: "router",
message: `Router : `,
choices: supportedRouters,
});
router = routerRes.router;
}
}
const cwd = process.cwd();
const root = path.resolve(cwd, projectName);
const templatesPath = path.resolve(
fileURLToPath(import.meta.url),
"..",
"..",
"templates"
);
try {
spinner.start();
fs.removeSync(root);
fs.ensureDirSync(root);
const config = {
projectName,
buildTool,
framework,
} as TemplateConfig;
if (store) {
config.store = store;
}
if (router) {
config.router = router;
}
const { success, message } = createTemplate(config, root, templatesPath);
if (success) {
spinner.succeed(
chalk.blue("execute: \n") +
chalk.green(
` 🌲 cd ${projectName} \n 🌲 npm install \n 🌲 npm run dev`
)
);
} else {
spinner.fail(chalk.red(message));
}
} catch (err) {
spinner.fail(
chalk.red("🚫 " + (err instanceof Error ? err.message : String(err)))
);
}
function cancel() {
console.info(chalk.blueBright("🛑Cancelled!"));
process.exit(0);
}