@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.
Files changed (4) hide show
  1. package/README.md +5 -5
  2. package/bin/index.js +431 -296
  3. package/lib/utils.js +240 -244
  4. package/package.json +1 -1
package/bin/index.js CHANGED
@@ -1,341 +1,476 @@
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 } 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
+ // 动态导入所有需要的模块
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
- program
103
- .name('robot')
104
- .description('🤖 Robot 项目脚手架工具 - @cheny/robot-cli')
105
- .version('1.0.0')
106
- .hook('preAction', () => {
107
- showWelcome();
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
- 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
- }
137
+ const program = new Command();
138
+
139
+ // 现代化欢迎信息
140
+ function showWelcome() {
141
+ console.clear();
129
142
 
130
- await createProject(projectName, options);
131
- } catch (error) {
132
- console.log();
133
- console.log(chalk.red(''), chalk.red.bold('创建失败'));
143
+ const logoLines = [
144
+ ' ██████╗ ██████╗ ██████╗ ██████╗ ████████╗',
145
+ ' ██╔══██╗██╔═══██╗██╔══██╗██╔═══██╗╚══██╔══╝',
146
+ ' ██████╔╝██║ ██║██████╔╝██║ ██║ ██║ ',
147
+ ' ██╔══██╗██║ ██║██╔══██╗██║ ██║ ██║ ',
148
+ ' ██║ ██║╚██████╔╝██████╔╝╚██████╔╝ ██║ ',
149
+ ' ╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═╝ '
150
+ ];
134
151
 
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
- }
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(chalk.blue('💡 获取帮助:'));
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
- 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;
171
+ // 显示主菜单
172
+ async function showMainMenu() {
173
+ const title = chalk.white.bold('🚀 快速开始');
174
+
175
+ console.log(' ' + title);
176
+ console.log();
164
177
 
165
- if (options.recommended) {
166
- templates = getRecommendedTemplates();
167
- title = '🎯 推荐模板';
168
- } else {
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
- const categories = {};
179
- Object.entries(templates).forEach(([key, template]) => {
180
- // 简单分类逻辑,根据模板名称前缀
181
- const category = key.split('-')[0];
182
- if (!categories[category]) {
183
- categories[category] = [];
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
- categories[category].push({ key, ...template });
186
- });
213
+ ];
187
214
 
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
- });
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
- 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);
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
- 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
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
- if (confirmed) {
256
- await clearCache();
254
+
255
+ await createProject(projectName, options);
256
+ } catch (error) {
257
257
  console.log();
258
- console.log(chalk.green(''), chalk.green.bold('缓存清除成功'));
259
- } else {
260
- console.log(chalk.yellow('❌ 取消清除'));
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
- } 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))}`);
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
- cacheInfo.templates.forEach(template => {
280
- const modifiedTime = template.modifiedTime.toLocaleDateString();
281
- console.log(` ${chalk.green('●')} ${template.name}`);
282
- console.log(` 大小: ${formatSize(template.size)} 更新: ${modifiedTime}`);
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
- console.log();
287
- console.log(chalk.dim(' 使用 robot cache --clear 清除缓存'));
288
- }
289
- } catch (error) {
290
- console.log(chalk.red('❌ 缓存操作失败:'), error.message);
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
- 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('缓存清除成功'));
444
+ // 全局错误处理
445
+ process.on('uncaughtException', (error) => {
303
446
  console.log();
304
- } catch (error) {
447
+ console.log(chalk.red('💥 程序发生未预期的错误:'));
448
+ console.log(chalk.dim(error.message));
305
449
  console.log();
306
- console.log(chalk.red('✗'), chalk.red.bold('清除缓存失败'));
307
- console.log(' ' + chalk.dim(error.message));
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
- if (process.argv.length === 2) {
314
- showWelcome();
315
- await showMainMenu();
316
- process.exit(0);
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
- 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
- });
469
+ } catch (error) {
470
+ console.error('启动失败:', error.message);
471
+ process.exit(1);
472
+ }
473
+ }
340
474
 
341
- program.parse();
475
+ // 启动程序
476
+ main();