@agile-team/robot-cli 1.0.3 → 1.0.5

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