@agile-team/robot-cli 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/bin/index.js +431 -296
- package/lib/utils.js +240 -244
- package/package.json +1 -1
package/bin/index.js
CHANGED
|
@@ -1,341 +1,476 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import inquirer from 'inquirer';
|
|
7
|
-
import { createProject } from '../lib/create.js';
|
|
8
|
-
import { clearCache, getCacheInfo, formatSize } from '../lib/cache.js';
|
|
9
|
-
import { getAllTemplates, searchTemplates, getRecommendedTemplates } from '../lib/templates.js';
|
|
10
|
-
import { checkNetworkConnection } from '../lib/utils.js';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
import { dirname, join, resolve } from 'path';
|
|
5
|
+
import { existsSync } from 'fs';
|
|
11
6
|
|
|
12
|
-
|
|
7
|
+
// 获取当前文件的目录
|
|
8
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
9
|
+
const __dirname = dirname(__filename);
|
|
13
10
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
'
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
11
|
+
/**
|
|
12
|
+
* 智能路径解析 - 兼容不同包管理器的安装路径
|
|
13
|
+
*/
|
|
14
|
+
function resolveLibPath() {
|
|
15
|
+
// 可能的 lib 目录路径
|
|
16
|
+
const possiblePaths = [
|
|
17
|
+
// 1. 标准相对路径 (开发环境 + 大多数情况)
|
|
18
|
+
join(__dirname, '..', 'lib'),
|
|
19
|
+
|
|
20
|
+
// 2. 同级目录 (某些链接情况)
|
|
21
|
+
join(__dirname, 'lib'),
|
|
22
|
+
|
|
23
|
+
// 3. 向上查找 (深度嵌套情况)
|
|
24
|
+
join(__dirname, '..', '..', 'lib'),
|
|
25
|
+
|
|
26
|
+
// 4. 从 node_modules 查找 (npm/yarn)
|
|
27
|
+
join(__dirname, '..', 'node_modules', '@agile-team', 'robot-cli', 'lib'),
|
|
28
|
+
|
|
29
|
+
// 5. 全局安装的各种可能路径
|
|
30
|
+
resolve(__dirname, '..', 'lib'),
|
|
31
|
+
resolve(__dirname, '../../lib'),
|
|
32
|
+
|
|
33
|
+
// 6. bun 特殊路径处理
|
|
34
|
+
join(__dirname, '..', '..', '@agile-team', 'robot-cli', 'lib'),
|
|
25
35
|
];
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
' 🤖 Robot 项目脚手架工具 v1.0.0\n' +
|
|
32
|
-
' ',
|
|
33
|
-
{
|
|
34
|
-
padding: { top: 1, bottom: 1, left: 2, right: 2 },
|
|
35
|
-
borderStyle: 'round',
|
|
36
|
-
borderColor: 'cyan',
|
|
37
|
-
backgroundColor: 'blackBright'
|
|
36
|
+
|
|
37
|
+
// 查找第一个存在的路径
|
|
38
|
+
for (const libPath of possiblePaths) {
|
|
39
|
+
if (existsSync(libPath)) {
|
|
40
|
+
return libPath;
|
|
38
41
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 如果都找不到,抛出详细错误
|
|
45
|
+
throw new Error(`
|
|
46
|
+
无法找到 lib 目录,已尝试以下路径:
|
|
47
|
+
${possiblePaths.map(p => ` - ${p}`).join('\n')}
|
|
48
|
+
|
|
49
|
+
当前执行路径: ${__dirname}
|
|
50
|
+
工作目录: ${process.cwd()}
|
|
51
|
+
|
|
52
|
+
可能的解决方案:
|
|
53
|
+
1. 重新安装: npm uninstall -g @agile-team/robot-cli && npm install -g @agile-team/robot-cli
|
|
54
|
+
2. 使用 npx: npx @agile-team/robot-cli
|
|
55
|
+
3. 检查包完整性: npm list -g @agile-team/robot-cli
|
|
56
|
+
`);
|
|
44
57
|
}
|
|
45
58
|
|
|
46
|
-
//
|
|
47
|
-
async function
|
|
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
|
-
|
|
59
|
+
// 动态导入所需模块
|
|
60
|
+
async function loadModules() {
|
|
61
|
+
try {
|
|
62
|
+
const libPath = resolveLibPath();
|
|
63
|
+
|
|
64
|
+
// 动态导入所有需要的模块
|
|
65
|
+
const [
|
|
66
|
+
{ Command },
|
|
67
|
+
chalk,
|
|
68
|
+
boxen,
|
|
69
|
+
inquirer,
|
|
70
|
+
{ createProject },
|
|
71
|
+
{ clearCache, getCacheInfo, formatSize },
|
|
72
|
+
{ getAllTemplates, searchTemplates, getRecommendedTemplates },
|
|
73
|
+
{ checkNetworkConnection }
|
|
74
|
+
] = await Promise.all([
|
|
75
|
+
import('commander'),
|
|
76
|
+
import('chalk'),
|
|
77
|
+
import('boxen'),
|
|
78
|
+
import('inquirer'),
|
|
79
|
+
import(join(libPath, 'create.js')),
|
|
80
|
+
import(join(libPath, 'cache.js')),
|
|
81
|
+
import(join(libPath, 'templates.js')),
|
|
82
|
+
import(join(libPath, 'utils.js'))
|
|
83
|
+
]);
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
Command,
|
|
87
|
+
chalk: chalk.default,
|
|
88
|
+
boxen: boxen.default,
|
|
89
|
+
inquirer: inquirer.default,
|
|
90
|
+
createProject,
|
|
91
|
+
clearCache,
|
|
92
|
+
getCacheInfo,
|
|
93
|
+
formatSize,
|
|
94
|
+
getAllTemplates,
|
|
95
|
+
searchTemplates,
|
|
96
|
+
getRecommendedTemplates,
|
|
97
|
+
checkNetworkConnection
|
|
98
|
+
};
|
|
99
|
+
} catch (error) {
|
|
100
|
+
console.error(`
|
|
101
|
+
❌ 模块加载失败: ${error.message}
|
|
102
|
+
|
|
103
|
+
🔧 诊断信息:
|
|
104
|
+
当前文件: ${__filename}
|
|
105
|
+
执行目录: ${__dirname}
|
|
106
|
+
工作目录: ${process.cwd()}
|
|
107
|
+
Node版本: ${process.version}
|
|
108
|
+
|
|
109
|
+
💡 解决方案:
|
|
110
|
+
1. 完全重装: npm uninstall -g @agile-team/robot-cli && npm install -g @agile-team/robot-cli
|
|
111
|
+
2. 使用npx: npx @agile-team/robot-cli
|
|
112
|
+
3. 联系支持: https://github.com/ChenyCHENYU/robot-cli/issues
|
|
113
|
+
`);
|
|
114
|
+
process.exit(1);
|
|
115
|
+
}
|
|
100
116
|
}
|
|
101
117
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
118
|
+
// 主程序
|
|
119
|
+
async function main() {
|
|
120
|
+
try {
|
|
121
|
+
const modules = await loadModules();
|
|
122
|
+
const {
|
|
123
|
+
Command,
|
|
124
|
+
chalk,
|
|
125
|
+
boxen,
|
|
126
|
+
inquirer,
|
|
127
|
+
createProject,
|
|
128
|
+
clearCache,
|
|
129
|
+
getCacheInfo,
|
|
130
|
+
formatSize,
|
|
131
|
+
getAllTemplates,
|
|
132
|
+
searchTemplates,
|
|
133
|
+
getRecommendedTemplates,
|
|
134
|
+
checkNetworkConnection
|
|
135
|
+
} = modules;
|
|
109
136
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
.option('--no-cache', '强制重新下载模板')
|
|
116
|
-
.option('--skip-install', '跳过依赖安装')
|
|
117
|
-
.action(async (projectName, options) => {
|
|
118
|
-
try {
|
|
119
|
-
// 检查网络连接
|
|
120
|
-
if (!options.cache) {
|
|
121
|
-
console.log(chalk.blue('🌐 检查网络连接...'));
|
|
122
|
-
const hasNetwork = await checkNetworkConnection();
|
|
123
|
-
if (!hasNetwork) {
|
|
124
|
-
console.log(chalk.red('❌ 网络连接失败,无法下载模板'));
|
|
125
|
-
console.log(chalk.yellow('💡 请检查网络连接后重试'));
|
|
126
|
-
process.exit(1);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
137
|
+
const program = new Command();
|
|
138
|
+
|
|
139
|
+
// 现代化欢迎信息
|
|
140
|
+
function showWelcome() {
|
|
141
|
+
console.clear();
|
|
129
142
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
143
|
+
const logoLines = [
|
|
144
|
+
' ██████╗ ██████╗ ██████╗ ██████╗ ████████╗',
|
|
145
|
+
' ██╔══██╗██╔═══██╗██╔══██╗██╔═══██╗╚══██╔══╝',
|
|
146
|
+
' ██████╔╝██║ ██║██████╔╝██║ ██║ ██║ ',
|
|
147
|
+
' ██╔══██╗██║ ██║██╔══██╗██║ ██║ ██║ ',
|
|
148
|
+
' ██║ ██║╚██████╔╝██████╔╝╚██████╔╝ ██║ ',
|
|
149
|
+
' ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═╝ '
|
|
150
|
+
];
|
|
134
151
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
152
|
+
const logo = logoLines.map(line => chalk.cyan(line)).join('\n');
|
|
153
|
+
|
|
154
|
+
const titleBox = boxen(
|
|
155
|
+
logo + '\n\n' +
|
|
156
|
+
' 🤖 Robot 项目脚手架工具 v1.0.3\n' +
|
|
157
|
+
' 兼容 npm/yarn/pnpm/bun',
|
|
158
|
+
{
|
|
159
|
+
padding: { top: 1, bottom: 1, left: 2, right: 2 },
|
|
160
|
+
borderStyle: 'round',
|
|
161
|
+
borderColor: 'cyan',
|
|
162
|
+
backgroundColor: 'blackBright'
|
|
163
|
+
}
|
|
164
|
+
);
|
|
143
165
|
|
|
144
166
|
console.log();
|
|
145
|
-
console.log(
|
|
146
|
-
console.log(chalk.dim(' robot --help'));
|
|
147
|
-
console.log(chalk.dim(' 联系团队技术支持'));
|
|
167
|
+
console.log(titleBox);
|
|
148
168
|
console.log();
|
|
149
|
-
process.exit(1);
|
|
150
169
|
}
|
|
151
|
-
});
|
|
152
170
|
|
|
153
|
-
//
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
.option('-c, --category <category>', '按分类筛选')
|
|
160
|
-
.action(async (options) => {
|
|
161
|
-
try {
|
|
162
|
-
let templates;
|
|
163
|
-
let title;
|
|
171
|
+
// 显示主菜单
|
|
172
|
+
async function showMainMenu() {
|
|
173
|
+
const title = chalk.white.bold('🚀 快速开始');
|
|
174
|
+
|
|
175
|
+
console.log(' ' + title);
|
|
176
|
+
console.log();
|
|
164
177
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
templates = getAllTemplates();
|
|
170
|
-
title = '📋 所有可用模板';
|
|
171
|
-
}
|
|
178
|
+
// 获取统计信息
|
|
179
|
+
const allTemplates = getAllTemplates();
|
|
180
|
+
const templateCount = Object.keys(allTemplates).length;
|
|
181
|
+
const cacheInfo = await getCacheInfo();
|
|
172
182
|
|
|
183
|
+
console.log(chalk.dim(` 📦 可用模板: ${templateCount} 个`));
|
|
184
|
+
console.log(chalk.dim(` 💾 缓存模板: ${cacheInfo.templates.length} 个 (${formatSize(cacheInfo.size)})`));
|
|
173
185
|
console.log();
|
|
174
|
-
console.log(chalk.blue(title));
|
|
175
|
-
console.log(chalk.dim(`共 ${Object.keys(templates).length} 个模板\n`));
|
|
176
186
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
187
|
+
const commands = [
|
|
188
|
+
{
|
|
189
|
+
cmd: 'robot create',
|
|
190
|
+
desc: '交互式创建项目',
|
|
191
|
+
color: 'cyan'
|
|
192
|
+
},
|
|
193
|
+
{
|
|
194
|
+
cmd: 'robot create <name>',
|
|
195
|
+
desc: '快速创建项目',
|
|
196
|
+
color: 'green'
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
cmd: 'robot list',
|
|
200
|
+
desc: '查看所有可用模板',
|
|
201
|
+
color: 'blue'
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
cmd: 'robot search <keyword>',
|
|
205
|
+
desc: '搜索模板',
|
|
206
|
+
color: 'magenta'
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
cmd: 'robot cache',
|
|
210
|
+
desc: '缓存管理',
|
|
211
|
+
color: 'yellow'
|
|
184
212
|
}
|
|
185
|
-
|
|
186
|
-
});
|
|
213
|
+
];
|
|
187
214
|
|
|
188
|
-
|
|
189
|
-
console.log(chalk
|
|
190
|
-
templates.forEach(template => {
|
|
191
|
-
console.log(` ${chalk.green('●')} ${chalk.bold(template.name)}`);
|
|
192
|
-
console.log(` ${chalk.dim(template.description)}`);
|
|
193
|
-
console.log(` ${chalk.dim('功能: ' + template.features.join(', '))}`);
|
|
194
|
-
console.log(` ${chalk.dim('使用: robot create my-app --template ' + template.key)}`);
|
|
195
|
-
console.log();
|
|
196
|
-
});
|
|
215
|
+
commands.forEach(({ cmd, desc, color }) => {
|
|
216
|
+
console.log(' ' + chalk[color](cmd.padEnd(24)) + chalk.dim(desc));
|
|
197
217
|
});
|
|
198
218
|
|
|
199
|
-
} catch (error) {
|
|
200
|
-
console.log(chalk.red('❌ 获取模板列表失败:'), error.message);
|
|
201
|
-
}
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
// 搜索模板
|
|
205
|
-
program
|
|
206
|
-
.command('search <keyword>')
|
|
207
|
-
.description('搜索模板')
|
|
208
|
-
.action(async (keyword) => {
|
|
209
|
-
try {
|
|
210
|
-
const results = searchTemplates(keyword);
|
|
211
|
-
|
|
212
219
|
console.log();
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
console.log(chalk.dim(' • 使用 robot list 查看所有模板'));
|
|
219
|
-
console.log(chalk.dim(' • 使用 robot list --recommended 查看推荐模板'));
|
|
220
|
-
} else {
|
|
221
|
-
console.log(chalk.green(`🔍 找到 ${Object.keys(results).length} 个匹配的模板:`));
|
|
222
|
-
console.log();
|
|
223
|
-
|
|
224
|
-
Object.entries(results).forEach(([key, template]) => {
|
|
225
|
-
console.log(`${chalk.green('●')} ${chalk.bold(template.name)}`);
|
|
226
|
-
console.log(` ${chalk.dim(template.description)}`);
|
|
227
|
-
console.log(` ${chalk.dim('功能: ' + template.features.join(', '))}`);
|
|
228
|
-
console.log(` ${chalk.cyan('robot create my-app --template ' + key)}`);
|
|
229
|
-
console.log();
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
} catch (error) {
|
|
233
|
-
console.log(chalk.red('❌ 搜索失败:'), error.message);
|
|
220
|
+
console.log(chalk.dim(' 示例:'));
|
|
221
|
+
console.log(chalk.dim(' robot create my-vue-admin'));
|
|
222
|
+
console.log(chalk.dim(' robot search vue'));
|
|
223
|
+
console.log(chalk.dim(' robot create my-app --template robot-admin'));
|
|
224
|
+
console.log();
|
|
234
225
|
}
|
|
235
|
-
});
|
|
236
226
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
227
|
+
program
|
|
228
|
+
.name('robot')
|
|
229
|
+
.description('🤖 Robot 项目脚手架工具 - @agile-team/robot-cli')
|
|
230
|
+
.version('1.0.3')
|
|
231
|
+
.hook('preAction', () => {
|
|
232
|
+
showWelcome();
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
// 创建项目命令
|
|
236
|
+
program
|
|
237
|
+
.command('create [project-name]')
|
|
238
|
+
.description('创建新项目')
|
|
239
|
+
.option('-t, --template <template>', '指定模板类型')
|
|
240
|
+
.option('--no-cache', '强制重新下载模板')
|
|
241
|
+
.option('--skip-install', '跳过依赖安装')
|
|
242
|
+
.action(async (projectName, options) => {
|
|
243
|
+
try {
|
|
244
|
+
// 检查网络连接
|
|
245
|
+
if (!options.cache) {
|
|
246
|
+
console.log(chalk.blue('🌐 检查网络连接...'));
|
|
247
|
+
const hasNetwork = await checkNetworkConnection();
|
|
248
|
+
if (!hasNetwork) {
|
|
249
|
+
console.log(chalk.red('❌ 网络连接失败,无法下载模板'));
|
|
250
|
+
console.log(chalk.yellow('💡 请检查网络连接后重试'));
|
|
251
|
+
process.exit(1);
|
|
252
|
+
}
|
|
252
253
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
await clearCache();
|
|
254
|
+
|
|
255
|
+
await createProject(projectName, options);
|
|
256
|
+
} catch (error) {
|
|
257
257
|
console.log();
|
|
258
|
-
console.log(chalk.
|
|
259
|
-
|
|
260
|
-
|
|
258
|
+
console.log(chalk.red('✗'), chalk.red.bold('创建失败'));
|
|
259
|
+
|
|
260
|
+
// 根据错误类型提供不同的建议
|
|
261
|
+
if (error.message.includes('网络')) {
|
|
262
|
+
console.log(' ' + chalk.dim('网络相关问题,请检查网络连接'));
|
|
263
|
+
} else if (error.message.includes('权限')) {
|
|
264
|
+
console.log(' ' + chalk.dim('权限问题,请检查文件夹权限'));
|
|
265
|
+
} else {
|
|
266
|
+
console.log(' ' + chalk.dim(error.message));
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
console.log();
|
|
270
|
+
console.log(chalk.blue('💡 获取帮助:'));
|
|
271
|
+
console.log(chalk.dim(' robot --help'));
|
|
272
|
+
console.log(chalk.dim(' https://github.com/ChenyCHENYU/robot-cli/issues'));
|
|
273
|
+
console.log();
|
|
274
|
+
process.exit(1);
|
|
261
275
|
}
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
// 列出所有模板
|
|
279
|
+
program
|
|
280
|
+
.command('list')
|
|
281
|
+
.alias('ls')
|
|
282
|
+
.description('列出所有可用模板')
|
|
283
|
+
.option('-r, --recommended', '只显示推荐模板')
|
|
284
|
+
.option('-c, --category <category>', '按分类筛选')
|
|
285
|
+
.action(async (options) => {
|
|
286
|
+
try {
|
|
287
|
+
let templates;
|
|
288
|
+
let title;
|
|
289
|
+
|
|
290
|
+
if (options.recommended) {
|
|
291
|
+
templates = getRecommendedTemplates();
|
|
292
|
+
title = '🎯 推荐模板';
|
|
293
|
+
} else {
|
|
294
|
+
templates = getAllTemplates();
|
|
295
|
+
title = '📋 所有可用模板';
|
|
296
|
+
}
|
|
297
|
+
|
|
276
298
|
console.log();
|
|
277
|
-
console.log(chalk.blue(
|
|
299
|
+
console.log(chalk.blue(title));
|
|
300
|
+
console.log(chalk.dim(`共 ${Object.keys(templates).length} 个模板\n`));
|
|
278
301
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
302
|
+
// 按分类显示
|
|
303
|
+
const categories = {};
|
|
304
|
+
Object.entries(templates).forEach(([key, template]) => {
|
|
305
|
+
// 简单分类逻辑,根据模板名称前缀
|
|
306
|
+
const category = key.split('-')[0];
|
|
307
|
+
if (!categories[category]) {
|
|
308
|
+
categories[category] = [];
|
|
309
|
+
}
|
|
310
|
+
categories[category].push({ key, ...template });
|
|
283
311
|
});
|
|
312
|
+
|
|
313
|
+
Object.entries(categories).forEach(([category, templates]) => {
|
|
314
|
+
console.log(chalk.cyan(`${category.toUpperCase()} 相关:`));
|
|
315
|
+
templates.forEach(template => {
|
|
316
|
+
console.log(` ${chalk.green('●')} ${chalk.bold(template.name)}`);
|
|
317
|
+
console.log(` ${chalk.dim(template.description)}`);
|
|
318
|
+
console.log(` ${chalk.dim('功能: ' + template.features.join(', '))}`);
|
|
319
|
+
console.log(` ${chalk.dim('使用: robot create my-app --template ' + template.key)}`);
|
|
320
|
+
console.log();
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
} catch (error) {
|
|
325
|
+
console.log(chalk.red('❌ 获取模板列表失败:'), error.message);
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
// 搜索模板
|
|
330
|
+
program
|
|
331
|
+
.command('search <keyword>')
|
|
332
|
+
.description('搜索模板')
|
|
333
|
+
.action(async (keyword) => {
|
|
334
|
+
try {
|
|
335
|
+
const results = searchTemplates(keyword);
|
|
336
|
+
|
|
337
|
+
console.log();
|
|
338
|
+
if (Object.keys(results).length === 0) {
|
|
339
|
+
console.log(chalk.yellow('🔍 没有找到匹配的模板'));
|
|
340
|
+
console.log();
|
|
341
|
+
console.log(chalk.blue('💡 建议:'));
|
|
342
|
+
console.log(chalk.dim(' • 尝试其他关键词'));
|
|
343
|
+
console.log(chalk.dim(' • 使用 robot list 查看所有模板'));
|
|
344
|
+
console.log(chalk.dim(' • 使用 robot list --recommended 查看推荐模板'));
|
|
345
|
+
} else {
|
|
346
|
+
console.log(chalk.green(`🔍 找到 ${Object.keys(results).length} 个匹配的模板:`));
|
|
347
|
+
console.log();
|
|
348
|
+
|
|
349
|
+
Object.entries(results).forEach(([key, template]) => {
|
|
350
|
+
console.log(`${chalk.green('●')} ${chalk.bold(template.name)}`);
|
|
351
|
+
console.log(` ${chalk.dim(template.description)}`);
|
|
352
|
+
console.log(` ${chalk.dim('功能: ' + template.features.join(', '))}`);
|
|
353
|
+
console.log(` ${chalk.cyan('robot create my-app --template ' + key)}`);
|
|
354
|
+
console.log();
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
} catch (error) {
|
|
358
|
+
console.log(chalk.red('❌ 搜索失败:'), error.message);
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
// 缓存管理
|
|
363
|
+
program
|
|
364
|
+
.command('cache')
|
|
365
|
+
.description('缓存管理')
|
|
366
|
+
.option('-c, --clear', '清除所有缓存')
|
|
367
|
+
.option('-i, --info', '显示缓存信息')
|
|
368
|
+
.action(async (options) => {
|
|
369
|
+
try {
|
|
370
|
+
if (options.clear) {
|
|
371
|
+
const { confirmed } = await inquirer.prompt([
|
|
372
|
+
{
|
|
373
|
+
type: 'confirm',
|
|
374
|
+
name: 'confirmed',
|
|
375
|
+
message: '确认清除所有模板缓存?',
|
|
376
|
+
default: false
|
|
377
|
+
}
|
|
378
|
+
]);
|
|
379
|
+
|
|
380
|
+
if (confirmed) {
|
|
381
|
+
await clearCache();
|
|
382
|
+
console.log();
|
|
383
|
+
console.log(chalk.green('✓'), chalk.green.bold('缓存清除成功'));
|
|
384
|
+
} else {
|
|
385
|
+
console.log(chalk.yellow('❌ 取消清除'));
|
|
386
|
+
}
|
|
387
|
+
} else {
|
|
388
|
+
// 显示缓存信息
|
|
389
|
+
const cacheInfo = await getCacheInfo();
|
|
390
|
+
|
|
391
|
+
console.log();
|
|
392
|
+
console.log(chalk.blue('💾 缓存信息:'));
|
|
393
|
+
console.log();
|
|
394
|
+
|
|
395
|
+
if (!cacheInfo.exists || cacheInfo.templates.length === 0) {
|
|
396
|
+
console.log(chalk.dim(' 暂无缓存模板'));
|
|
397
|
+
} else {
|
|
398
|
+
console.log(` 缓存目录: ${chalk.dim(cacheInfo.path)}`);
|
|
399
|
+
console.log(` 模板数量: ${chalk.cyan(cacheInfo.templates.length)} 个`);
|
|
400
|
+
console.log(` 总大小: ${chalk.cyan(formatSize(cacheInfo.size))}`);
|
|
401
|
+
console.log();
|
|
402
|
+
console.log(chalk.blue(' 缓存的模板:'));
|
|
403
|
+
|
|
404
|
+
cacheInfo.templates.forEach(template => {
|
|
405
|
+
const modifiedTime = template.modifiedTime.toLocaleDateString();
|
|
406
|
+
console.log(` ${chalk.green('●')} ${template.name}`);
|
|
407
|
+
console.log(` 大小: ${formatSize(template.size)} 更新: ${modifiedTime}`);
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
console.log();
|
|
412
|
+
console.log(chalk.dim(' 使用 robot cache --clear 清除缓存'));
|
|
413
|
+
}
|
|
414
|
+
} catch (error) {
|
|
415
|
+
console.log(chalk.red('❌ 缓存操作失败:'), error.message);
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
// 清除缓存命令 (向后兼容)
|
|
420
|
+
program
|
|
421
|
+
.command('clear-cache')
|
|
422
|
+
.description('清除模板缓存')
|
|
423
|
+
.action(async () => {
|
|
424
|
+
try {
|
|
425
|
+
await clearCache();
|
|
426
|
+
console.log();
|
|
427
|
+
console.log(chalk.green('✓'), chalk.green.bold('缓存清除成功'));
|
|
428
|
+
console.log();
|
|
429
|
+
} catch (error) {
|
|
430
|
+
console.log();
|
|
431
|
+
console.log(chalk.red('✗'), chalk.red.bold('清除缓存失败'));
|
|
432
|
+
console.log(' ' + chalk.dim(error.message));
|
|
433
|
+
console.log();
|
|
284
434
|
}
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
// 如果没有参数,显示主菜单
|
|
438
|
+
if (process.argv.length === 2) {
|
|
439
|
+
showWelcome();
|
|
440
|
+
await showMainMenu();
|
|
441
|
+
process.exit(0);
|
|
291
442
|
}
|
|
292
|
-
});
|
|
293
443
|
|
|
294
|
-
//
|
|
295
|
-
|
|
296
|
-
.command('clear-cache')
|
|
297
|
-
.description('清除模板缓存')
|
|
298
|
-
.action(async () => {
|
|
299
|
-
try {
|
|
300
|
-
await clearCache();
|
|
301
|
-
console.log();
|
|
302
|
-
console.log(chalk.green('✓'), chalk.green.bold('缓存清除成功'));
|
|
444
|
+
// 全局错误处理
|
|
445
|
+
process.on('uncaughtException', (error) => {
|
|
303
446
|
console.log();
|
|
304
|
-
|
|
447
|
+
console.log(chalk.red('💥 程序发生未预期的错误:'));
|
|
448
|
+
console.log(chalk.dim(error.message));
|
|
305
449
|
console.log();
|
|
306
|
-
console.log(chalk.
|
|
307
|
-
console.log(
|
|
450
|
+
console.log(chalk.blue('💡 建议:'));
|
|
451
|
+
console.log(chalk.dim(' • 重启终端重试'));
|
|
452
|
+
console.log(chalk.dim(' • 检查网络连接'));
|
|
453
|
+
console.log(chalk.dim(' • 重新安装: npm install -g @agile-team/robot-cli'));
|
|
454
|
+
console.log(chalk.dim(' • 联系技术支持: https://github.com/ChenyCHENYU/robot-cli/issues'));
|
|
308
455
|
console.log();
|
|
309
|
-
|
|
310
|
-
|
|
456
|
+
process.exit(1);
|
|
457
|
+
});
|
|
311
458
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
459
|
+
process.on('unhandledRejection', (error) => {
|
|
460
|
+
console.log();
|
|
461
|
+
console.log(chalk.red('💥 程序发生未处理的异步错误:'));
|
|
462
|
+
console.log(chalk.dim(error.message));
|
|
463
|
+
console.log();
|
|
464
|
+
process.exit(1);
|
|
465
|
+
});
|
|
318
466
|
|
|
319
|
-
|
|
320
|
-
process.on('uncaughtException', (error) => {
|
|
321
|
-
console.log();
|
|
322
|
-
console.log(chalk.red('💥 程序发生未预期的错误:'));
|
|
323
|
-
console.log(chalk.dim(error.message));
|
|
324
|
-
console.log();
|
|
325
|
-
console.log(chalk.blue('💡 建议:'));
|
|
326
|
-
console.log(chalk.dim(' • 重启终端重试'));
|
|
327
|
-
console.log(chalk.dim(' • 检查网络连接'));
|
|
328
|
-
console.log(chalk.dim(' • 联系技术支持'));
|
|
329
|
-
console.log();
|
|
330
|
-
process.exit(1);
|
|
331
|
-
});
|
|
467
|
+
program.parse();
|
|
332
468
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
process.exit(1);
|
|
339
|
-
});
|
|
469
|
+
} catch (error) {
|
|
470
|
+
console.error('启动失败:', error.message);
|
|
471
|
+
process.exit(1);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
340
474
|
|
|
341
|
-
|
|
475
|
+
// 启动程序
|
|
476
|
+
main();
|