td-web-cli 0.1.28 → 0.1.29
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/dist/index.js +83 -46
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -4,66 +4,103 @@
|
|
|
4
4
|
* 通过交互式选择执行不同模块功能
|
|
5
5
|
*/
|
|
6
6
|
import { Command } from 'commander';
|
|
7
|
+
import fs from 'fs';
|
|
7
8
|
import { select, Separator } from '@inquirer/prompts';
|
|
8
9
|
import { logger, loggerError } from './utils/index.js';
|
|
9
10
|
import { i18n } from './modules/i18n/index.js';
|
|
10
11
|
import { tools } from './modules/tools/index.js';
|
|
12
|
+
// 读取 package.json 中的版本号
|
|
13
|
+
const { version } = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url), 'utf-8'));
|
|
11
14
|
const program = new Command();
|
|
15
|
+
/**
|
|
16
|
+
* 设置基础命令行选项
|
|
17
|
+
*/
|
|
18
|
+
function setupBasicOptions() {
|
|
19
|
+
program
|
|
20
|
+
.version(version, '-v, --version', '显示版本号')
|
|
21
|
+
.description('td-web-cli 命令行工具')
|
|
22
|
+
.usage('[options]')
|
|
23
|
+
.helpOption('-h, --help', '显示帮助信息')
|
|
24
|
+
.addHelpText('after', `
|
|
25
|
+
示例:
|
|
26
|
+
$ td-web-cli # 进入交互式选择模块
|
|
27
|
+
$ td-web-cli -v # 显示版本号
|
|
28
|
+
$ td-web-cli -h # 显示帮助信息
|
|
29
|
+
`.trim());
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 交互式选择模块并执行
|
|
33
|
+
*/
|
|
34
|
+
async function runInteractiveMode() {
|
|
35
|
+
// 定义可用模块选项
|
|
36
|
+
const moduleChoices = [
|
|
37
|
+
{
|
|
38
|
+
name: '国际化',
|
|
39
|
+
value: 'i18n',
|
|
40
|
+
description: '国际化相关功能',
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: '小工具',
|
|
44
|
+
value: 'tools',
|
|
45
|
+
description: '小工具相关功能',
|
|
46
|
+
},
|
|
47
|
+
];
|
|
48
|
+
// 交互式选择模块
|
|
49
|
+
const answer = await select({
|
|
50
|
+
message: '请选择要执行的模块:',
|
|
51
|
+
choices: [
|
|
52
|
+
...moduleChoices,
|
|
53
|
+
new Separator(), // 分割线,便于未来扩展更多模块
|
|
54
|
+
],
|
|
55
|
+
default: 'i18n', // 默认选项
|
|
56
|
+
loop: true, // 选项循环滚动
|
|
57
|
+
});
|
|
58
|
+
// 查找选择模块的名称,方便日志输出
|
|
59
|
+
const selectedModule = moduleChoices.find((item) => item.value === answer);
|
|
60
|
+
if (!selectedModule) {
|
|
61
|
+
logger.warn('未选择有效模块,程序已退出');
|
|
62
|
+
process.exit(0);
|
|
63
|
+
}
|
|
64
|
+
logger.info(`用户选择模块:${selectedModule.name}`);
|
|
65
|
+
// 根据选择执行对应模块
|
|
66
|
+
switch (answer) {
|
|
67
|
+
case 'i18n':
|
|
68
|
+
logger.info(`${selectedModule.name}模块开始执行`);
|
|
69
|
+
await i18n(program);
|
|
70
|
+
logger.info(`${selectedModule.name}模块执行完成`);
|
|
71
|
+
break;
|
|
72
|
+
case 'tools':
|
|
73
|
+
logger.info(`${selectedModule.name}模块开始执行`);
|
|
74
|
+
await tools(program);
|
|
75
|
+
logger.info(`${selectedModule.name}模块执行完成`);
|
|
76
|
+
break;
|
|
77
|
+
default:
|
|
78
|
+
logger.warn(`${selectedModule.name}模块暂未实现,程序已退出`);
|
|
79
|
+
process.exit(0);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
12
82
|
/**
|
|
13
83
|
* 主程序入口函数
|
|
14
|
-
*
|
|
84
|
+
* 解析命令行参数,若未提供任何选项则进入交互式选择
|
|
15
85
|
*/
|
|
16
86
|
async function main() {
|
|
17
87
|
try {
|
|
18
88
|
logger.info('td-web-cli程序启动');
|
|
19
|
-
//
|
|
89
|
+
// 设置基础命令行选项
|
|
90
|
+
setupBasicOptions();
|
|
91
|
+
// 解析命令行参数(如果用户提供了 -v 或 -h,commander 会自动退出)
|
|
20
92
|
program.parse(process.argv);
|
|
21
93
|
logger.info(`命令行参数解析完成:${process.argv.slice(2).join(' ')}`);
|
|
22
|
-
//
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
description: '国际化相关功能',
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
name: '小工具',
|
|
31
|
-
value: 'tools',
|
|
32
|
-
description: '小工具相关功能',
|
|
33
|
-
},
|
|
34
|
-
];
|
|
35
|
-
// 交互式选择模块
|
|
36
|
-
const answer = await select({
|
|
37
|
-
message: '请选择要执行的模块:',
|
|
38
|
-
choices: [
|
|
39
|
-
...moduleChoices,
|
|
40
|
-
new Separator(), // 分割线,便于未来扩展更多模块
|
|
41
|
-
],
|
|
42
|
-
default: 'i18n', // 默认选项
|
|
43
|
-
loop: true, // 选项循环滚动
|
|
44
|
-
});
|
|
45
|
-
// 查找选择模块的名称,方便日志输出
|
|
46
|
-
const selectedModule = moduleChoices.find((item) => item.value === answer);
|
|
47
|
-
if (!selectedModule) {
|
|
48
|
-
logger.warn('未选择有效模块,程序已退出');
|
|
49
|
-
process.exit(0);
|
|
94
|
+
// 如果没有其他参数(如 -v、-h 等),则进入交互模式
|
|
95
|
+
// 注意:program.args 包含未被选项消费的参数,若无额外参数则执行交互
|
|
96
|
+
if (program.args.length === 0) {
|
|
97
|
+
logger.info('未检测到额外参数,进入交互式选择模式');
|
|
98
|
+
await runInteractiveMode();
|
|
50
99
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
logger.info(`${selectedModule.name}模块开始执行`);
|
|
56
|
-
await i18n(program);
|
|
57
|
-
logger.info(`${selectedModule.name}模块执行完成`);
|
|
58
|
-
break;
|
|
59
|
-
case 'tools':
|
|
60
|
-
logger.info(`${selectedModule.name}模块开始执行`);
|
|
61
|
-
await tools(program);
|
|
62
|
-
logger.info(`${selectedModule.name}模块执行完成`);
|
|
63
|
-
break;
|
|
64
|
-
default:
|
|
65
|
-
logger.warn(`${selectedModule.name}模块暂未实现,程序已退出`);
|
|
66
|
-
process.exit(0);
|
|
100
|
+
else {
|
|
101
|
+
// 如果有额外参数,可能用户直接传入了子命令,但当前未实现子命令,提示帮助
|
|
102
|
+
logger.warn(`未知参数:${program.args.join(' ')},请使用 --help 查看用法`, true);
|
|
103
|
+
program.help(); // 显示帮助并退出
|
|
67
104
|
}
|
|
68
105
|
}
|
|
69
106
|
catch (error) {
|