@xubill/xx-cli 2.0.0 → 2.0.1

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 CHANGED
@@ -17,17 +17,13 @@ xx <command> [options]
17
17
 
18
18
  ## 版本记录
19
19
 
20
- ### v1.0.0
21
- - 初始化项目,搭建基本框架
22
- - 实现插件系统核心功能
23
- - 添加基础命令(help, version, history)
24
- - 修复原生插件和用户插件区别
25
- - 壳功能移动到原生插件目录
26
-
27
20
  ### v2.0.0
28
21
  - 支持新格式命令注册,兼容旧格式插件
29
22
  - 更新插件创建模板,采用配置对象格式并包含类结构实现
30
23
 
24
+ ### v2.0.1
25
+ - 新增ai插件,解析自然语言为xx命令
26
+
31
27
  ## 框架自带命令
32
28
 
33
29
  xx-cli 框架自带以下核心命令(位于 core 模块中):
@@ -39,6 +35,36 @@ xx-cli 框架自带以下核心命令(位于 core 模块中):
39
35
  | `help` | - | 显示帮助信息 |
40
36
  | `version` | `v` | 显示版本信息 |
41
37
  | `history` | `h` | 查看命令历史记录 |
38
+ | `ai` | - | AI 智能解释命令,将自然语言转换为 xx 命令 |
39
+
40
+ ### AI 插件使用说明
41
+
42
+ **获取 API key**:访问 [白山智算](https://ai.baishan.com/auth/login?referralCode=9CbeQycJJP) 获取 API key
43
+
44
+ **使用方法**:
45
+
46
+ ```bash
47
+ # 快捷添加 API key
48
+ xx ai add <apiKey>
49
+
50
+ # 使用 AI 解释命令
51
+ xx ai <查询内容>
52
+
53
+ # 生成配置文件
54
+ xx ai config
55
+ ```
56
+
57
+ **示例**:
58
+ ```bash
59
+ # 添加 API key
60
+ xx ai add sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
61
+
62
+ # 解释命令
63
+ xx ai 关闭占用3000端口的进程
64
+
65
+ # 命令兜底(输入不存在的命令时自动调用 AI)
66
+ xx npm
67
+ ```
42
68
 
43
69
  ### 插件管理命令
44
70
 
@@ -99,583 +125,57 @@ lib/plugins/
99
125
  - **config-manager.js**:配置管理插件,用于管理所有插件的配置文件。它提供了导出、导入、列出配置等功能。
100
126
  - **history.js**:历史记录插件,用于查看命令历史记录。它提供了查看、清除、搜索历史记录等功能。
101
127
  - **plugin-manager.js**:插件管理插件,用于管理所有插件。它提供了列出、启用、禁用、添加、删除、创建插件等功能。
128
+ - **ai.js**:AI 智能解释插件,用于解释 xx-cli 命令的含义,将自然语言转换为命令。它提供了智能解释、命令兜底等功能。
102
129
 
103
130
  ## 插件开发
104
131
 
105
132
  本部分将详细介绍如何在 xx-cli 中创建、开发和部署插件。
106
133
 
107
- ### 创建插件
134
+ ### 插件格式
108
135
 
109
- #### 使用 plugin create 命令
136
+ xx-cli 支持两种插件格式:
110
137
 
111
- xx-cli 提供了 `plugin create` 命令,可以快速生成插件模板。这是推荐的创建插件的方式。
138
+ 1. **新格式(配置对象格式)**:使用配置对象定义的插件实现(推荐)
139
+ 2. **旧格式(类结构格式)**:继承自 `BasePlugin` 类的插件实现(仅用于向后兼容)
112
140
 
113
- ##### 基本用法
141
+ ### 新格式插件开发(推荐)
142
+
143
+ 新格式插件使用配置对象定义插件信息和命令,结合类结构实现核心功能,是推荐的插件开发方式。
144
+
145
+ #### 创建新格式插件
114
146
 
115
147
  ```bash
116
- # 创建插件到插件目录(默认使用新格式,包含类结构实现)
148
+ # 创建新格式插件(默认)
117
149
  xx plugin create <plugin-name>
118
150
 
119
151
  # 或者使用别名
120
152
  xx p create <plugin-name>
121
-
122
153
  # 或者使用更短的别名
123
154
  xx p cr <plugin-name>
124
155
  ```
125
156
 
126
- ##### 插件模板格式
127
-
128
- 从 v2.0.0 版本开始,`plugin create` 命令生成的插件模板采用统一的格式:
157
+ #### 新格式插件特点
129
158
 
130
159
  - **配置对象格式**:使用配置对象定义插件的基本信息和命令
131
160
  - **包含类结构实现**:核心功能通过类结构实现,提高代码模块化和可维护性
132
161
  - **自动生成命令名称**:根据插件名称自动生成驼峰命名的命令名称
133
162
  - **标准的用户提示**:使用标准化的用户提示和错误处理
134
163
 
135
- 这种格式结合了配置对象的简洁性和类结构的模块化优势,是推荐的插件开发方式。
136
-
137
- ##### 示例
138
-
139
- ```bash
140
- # 创建一个名为 `hello-world` 的插件
141
- xx plugin create hello-world
142
-
143
- # 输出:
144
- # 创建插件模板...
145
- # 插件模板已成功创建到:/Users/xub/.xx-cli/plugins/hello-world.js
146
- # 提示:请编辑插件文件,实现具体功能
147
- # 测试方法:运行 "node bin/cli.js <插件命令>"
148
- # 已自动打开 vscode 编辑插件文件
149
- ```
150
-
151
- #### 手动创建插件
152
-
153
- 如果需要更灵活地控制插件结构,也可以手动创建插件文件。
154
-
155
- 1. 在 `lib/plugins` 目录下创建一个新的 JavaScript 文件,例如 `my-plugin.js`
156
- 2. 实现插件类,包含必要的方法
157
-
158
- ### 插件结构
159
-
160
- #### 插件格式
161
-
162
- xx-cli 支持两种插件格式:
163
-
164
- 1. **旧格式(类结构格式)**:继承自 `BasePlugin` 类的插件实现
165
- 2. **新格式(配置对象格式)**:使用配置对象定义的插件实现(推荐)
166
-
167
- #### 新格式(配置对象格式)插件结构
168
-
169
- 使用 `plugin create` 命令生成的默认插件模板包含以下结构(v2.0.0 版本更新):
170
-
171
- ```javascript
172
- /**
173
- * hello-world 插件
174
- * 用于实现 hello-world 相关功能
175
- *
176
- * 命令说明:
177
- * - helloWorld [options]:hello-world 相关功能
178
- * - 示例:helloWorld
179
- *
180
- * 功能说明:
181
- * - 实现 hello-world 相关功能
182
- *
183
- * 用户体验:
184
- * - 使用标准化的用户提示
185
- * - 提供清晰的错误提示
186
- * - 支持命令行参数
187
- */
188
-
189
- /**
190
- * hello-world 插件类
191
- * 实现 hello-world 相关核心功能
192
- */
193
- class HelloWorldPlugin {
194
- constructor() {
195
- this.pluginName = 'hello-world';
196
- }
197
-
198
- /**
199
- * 执行 hello-world 命令
200
- * @param {Array} args - 命令参数
201
- * @param {Object} options - 命令选项
202
- * @param {Object} helper - 插件帮助器
203
- */
204
- async execute(args, options, helper) {
205
- helper.showInfo('hello-world 命令执行成功!');
206
- if (options && options.verbose) {
207
- helper.showInfo('详细信息:');
208
- helper.showInfo('- 命令名称: helloWorld');
209
- helper.showInfo('- 执行时间: ' + new Date().toLocaleString());
210
- }
211
- }
212
- }
213
-
214
- // 创建插件实例
215
- const pluginInstance = new HelloWorldPlugin();
216
-
217
- module.exports = {
218
- name: "helloWorld",
219
- alias: "h",
220
- description: "hello-world 相关功能",
221
- options: [
222
- {
223
- name: "-v, --verbose",
224
- description: "显示详细信息",
225
- },
226
- {
227
- name: "-h, --help",
228
- description: "显示帮助信息",
229
- },
230
- ],
231
- action: async (args, options, helper) => {
232
- await pluginInstance.execute(args, options, helper);
233
- },
234
- };
235
- ```
236
-
237
- #### 旧格式(类结构格式)插件结构
238
-
239
- 使用 `plugin create --format old` 命令生成的插件模板包含以下结构:
240
-
241
- ```javascript
242
- /**
243
- * hello-world 插件
244
- * 用于实现 hello-world 相关功能
245
- *
246
- * 命令说明:
247
- * - helloWorld [options]:hello-world 相关功能
248
- * - 示例:helloWorld
249
- *
250
- * 功能说明:
251
- * - 实现 hello-world 相关功能
252
- *
253
- * 用户体验:
254
- * - 使用标准化的用户提示
255
- * - 提供清晰的错误提示
256
- * - 支持命令行参数
257
- */
258
-
259
- const BasePlugin = require('../core/base-plugin');
260
-
261
- class HelloWorldPlugin extends BasePlugin {
262
- constructor(options = {}) {
263
- super(options);
264
- this.pluginName = 'hello-world';
265
- }
266
-
267
- /**
268
- * 注册命令
269
- * @param {Object} program - Commander.js 实例
270
- */
271
- registerCommands(program) {
272
- program.command('helloWorld')
273
- .description('hello-world 相关功能')
274
- .option('-v, --verbose', '显示详细信息')
275
- .option('-h, --help', '显示帮助信息')
276
- .action((options) => this.helloWorldCommand(options));
277
- }
278
-
279
- /**
280
- * hello-world 命令
281
- * @param {Object} options - 命令选项
282
- */
283
- helloWorldCommand(options) {
284
- console.log('hello-world 命令执行成功!');
285
- if (options && options.verbose) {
286
- console.log('详细信息:');
287
- console.log('- 命令名称:', 'helloWorld');
288
- console.log('- 执行时间:', new Date().toLocaleString());
289
- }
290
- }
291
- }
292
-
293
- module.exports = HelloWorldPlugin;
294
- ```
295
-
296
- #### 命令注册
297
-
298
- ##### 新格式(配置对象格式)命令注册
299
-
300
- 对于新格式的插件,命令注册通过配置对象的 `options` 和 `action` 属性实现:
301
-
302
- ```javascript
303
- module.exports = {
304
- name: 'hello-world',
305
- alias: 'hw',
306
- description: 'hello-world 相关功能',
307
- options: [
308
- {
309
- name: '-v, --verbose',
310
- description: '显示详细信息'
311
- },
312
- {
313
- name: '-n, --name <name>',
314
- description: '指定名称'
315
- }
316
- ],
317
- action: (args, options, cmd) => {
318
- const helper = createPluginsHelper('hello-world');
319
- helloWorldCommand(args.alias, options, helper);
320
- }
321
- };
322
- ```
323
-
324
- ##### 旧格式(类结构格式)命令注册
325
-
326
- 对于旧格式的插件,命令注册通过实现 `registerCommands` 方法实现:
327
-
328
- ```javascript
329
- registerCommands(program) {
330
- program.command('helloWorld [alias]')
331
- .alias('hw')
332
- .description('hello-world 相关功能')
333
- .option('-v, --verbose', '显示详细信息')
334
- .option('-n, --name <name>', '指定名称')
335
- .action((alias, options) => this.helloWorldCommand(alias, options));
336
- }
337
- ```
338
-
339
- #### 命令选项
340
-
341
- 可以为命令添加各种选项,以增强命令的灵活性。
342
-
343
- ##### 常用选项类型
344
-
345
- - **布尔选项**:不需要参数的选项
346
- ```javascript
347
- .option('-v, --verbose', '显示详细信息')
348
- ```
349
-
350
- - **值选项**:需要参数的选项
351
- ```javascript
352
- .option('-n, --name <name>', '指定名称')
353
- ```
354
-
355
- - **数组选项**:可以接收多个值的选项
356
- ```javascript
357
- .option('-a, --array <items...>', '指定数组参数')
358
- ```
164
+ #### 详细文档
359
165
 
360
- - **带默认值的选项**:
361
- ```javascript
362
- .option('-p, --port <port>', '指定端口', '3000')
363
- ```
166
+ 完整的新格式插件开发指南请参考:[新格式插件开发教程](./plugin-development-new.md)
364
167
 
365
- - **带验证器的选项**:
366
- ```javascript
367
- .option('-p, --port <port>', '指定端口', parseInt)
368
- ```
168
+ ### 旧格式插件开发(仅用于向后兼容)
369
169
 
370
- ### 插件配置
170
+ 旧格式插件需要继承 `BasePlugin` 类,主要用于向后兼容已有的插件。
371
171
 
372
- 插件可以使用配置文件来存储和管理配置信息。
373
-
374
- #### 配置文件结构
375
-
376
- 配置文件通常存储在用户主目录的 `.xx` 文件夹中,例如:
377
-
378
- ```
379
- ~/.xx/
380
- └── hello-world/
381
- └── config.json
382
- ```
383
-
384
- #### 读取配置
385
-
386
- ```javascript
387
- const fs = require('fs');
388
- const path = require('path');
389
-
390
- // 获取用户主目录
391
- const homeDir = process.env.HOME || process.env.USERPROFILE;
392
- const xxDir = path.join(homeDir, '.xx-cli');
393
- const pluginConfigDir = path.join(xxDir, this.pluginName);
394
- const configFile = path.join(pluginConfigDir, 'config.json');
395
-
396
- // 读取配置
397
- let config = {};
398
- if (fs.existsSync(configFile)) {
399
- config = JSON.parse(fs.readFileSync(configFile, 'utf8'));
400
- }
401
- ```
402
-
403
- #### 写入配置
404
-
405
- ```javascript
406
- const fs = require('fs');
407
- const path = require('path');
408
-
409
- // 确保配置目录存在
410
- fs.ensureDirSync(pluginConfigDir);
411
-
412
- // 写入配置
413
- fs.writeFileSync(configFile, JSON.stringify(config, null, 2));
414
- ```
415
-
416
- ### 插件工具
417
-
418
- #### 新格式(配置对象格式)插件工具
419
-
420
- 对于新格式的插件,使用 `plugins-helper` 提供的功能来替代 `BasePlugin` 类的方法:
421
-
422
- ##### 导入 plugins-helper
423
-
424
- ```javascript
425
- const { createPluginsHelper } = require('../utils/plugins-helper');
426
- ```
427
-
428
- ##### 核心功能
429
-
430
- `plugins-helper` 提供了以下核心功能:
431
-
432
- ##### 1. 用户界面
433
-
434
- - **`helper.showInfo(text)`**:显示信息
435
- - **`helper.showSuccess(text)`**:显示成功信息
436
- - **`helper.showWarning(text)`**:显示警告信息
437
- - **`helper.showError(text)`**:显示错误信息
438
- - **`helper.startLoading(text)`**:显示加载状态
439
- - **`helper.stopLoading(text)`**:停止加载状态
440
-
441
- ##### 2. 配置管理
442
-
443
- 可以使用 `fs-extra` 和 `path` 模块来实现配置管理:
444
-
445
- ```javascript
446
- const fs = require('fs-extra');
447
- const path = require('path');
448
-
449
- // 确保配置目录存在
450
- function ensureConfigDir(pluginName) {
451
- const homeDir = process.env.HOME || process.env.USERPROFILE;
452
- const xxDir = path.join(homeDir, '.xx-cli');
453
- const pluginConfigDir = path.join(xxDir, pluginName);
454
- fs.ensureDirSync(pluginConfigDir);
455
- return pluginConfigDir;
456
- }
457
-
458
- // 加载配置
459
- function loadConfig(pluginName) {
460
- const configDir = ensureConfigDir(pluginName);
461
- const configFile = path.join(configDir, 'config.json');
462
- if (fs.existsSync(configFile)) {
463
- return JSON.parse(fs.readFileSync(configFile, 'utf8'));
464
- }
465
- return {};
466
- }
467
-
468
- // 保存配置
469
- function saveConfig(pluginName, config) {
470
- const configDir = ensureConfigDir(pluginName);
471
- const configFile = path.join(configDir, 'config.json');
472
- fs.writeFileSync(configFile, JSON.stringify(config, null, 2));
473
- }
474
- ```
475
-
476
- ##### 使用示例
477
-
478
- ```javascript
479
- const { createPluginsHelper } = require('../utils/plugins-helper');
480
-
481
- function myCommand(options, helper) {
482
- try {
483
- helper.showInfo('执行操作...');
484
-
485
- // 执行操作
486
- setTimeout(() => {
487
- helper.showSuccess('操作成功');
488
-
489
- if (options && options.verbose) {
490
- helper.showInfo('详细信息:');
491
- helper.showInfo('- 执行时间:', new Date().toLocaleString());
492
- }
493
- }, 1000);
494
- } catch (error) {
495
- helper.showError(`执行命令失败: ${error.message}`);
496
- }
497
- }
498
-
499
- module.exports = {
500
- name: 'my-plugin',
501
- description: 'My plugin command',
502
- options: [
503
- {
504
- name: '-v, --verbose',
505
- description: '显示详细信息'
506
- }
507
- ],
508
- action: (args, options, cmd) => {
509
- const helper = createPluginsHelper('my-plugin');
510
- myCommand(options, helper);
511
- }
512
- };
513
- ```
514
-
515
- #### 旧格式(类结构格式)基础插件类
516
-
517
- 对于旧格式的插件,使用 `BasePlugin` 基类:
518
-
519
- ##### 基础插件类路径
520
-
521
- `BasePlugin` 类位于 `/Users/xub/xx-cli/lib/core/base-plugin.js`,插件可以通过以下方式导入:
522
-
523
- ```javascript
524
- const BasePlugin = require('../core/base-plugin');
525
- ```
526
-
527
- ##### 核心功能
528
-
529
- `BasePlugin` 类提供了以下核心功能:
530
-
531
- ##### 1. 命令注册
532
-
533
- - **`registerCommands(program)`**:注册命令到 Commander.js 实例,子类必须实现此方法
534
-
535
- ##### 2. 插件初始化
536
-
537
- - **`init()`**:插件初始化逻辑,子类可以覆盖
538
- - **`getInfo()`**:获取插件信息
539
-
540
- ##### 3. 用户界面
541
-
542
- - **`startLoading(text)`**:显示加载状态
543
- - **`stopLoading(text)`**:停止加载状态
544
- - **`showError(text)`**:显示错误信息
545
- - **`startProgressBar(text, total)`**:启动进度条
546
- - **`updateProgressBar(current, text)`**:更新进度条
547
- - **`stopProgressBar(text)`**:停止进度条
548
- - **`showSuccess(text)`**:显示成功信息
549
- - **`showWarning(text)`**:显示警告信息
550
- - **`showInfo(text)`**:显示信息
551
-
552
- ##### 4. 配置管理
553
-
554
- - **`loadConfig(configName, cliOptions)`**:加载配置
555
- - **`saveConfig(config, configName, isGlobal)`**:保存配置
556
-
557
- ##### 5. 错误处理
558
-
559
- - **`handleError(error, message)`**:处理错误
560
-
561
- ##### 6. 异步操作
562
-
563
- - **`executeAsync(fn, loadingText, successText, errorText)`**:执行异步操作并显示加载状态
564
-
565
- ##### 7. 钩子系统
566
-
567
- - **`registerHook(event, callback)`**:注册钩子
568
- - **`triggerHook(event, data)`**:触发钩子
569
- - **`hasHook(event)`**:检查是否注册了指定钩子
570
-
571
- ##### 8. 剪贴板操作
572
-
573
- - **`copyToClipboard(text, successMessage)`**:复制文本到剪贴板
574
-
575
- ##### 使用示例
576
-
577
- ```javascript
578
- const BasePlugin = require('../core/base-plugin');
579
-
580
- class MyPlugin extends BasePlugin {
581
- constructor(options = {}) {
582
- super(options);
583
- this.pluginName = 'my-plugin';
584
- }
585
-
586
- registerCommands(program) {
587
- program.command('mycommand')
588
- .description('My plugin command')
589
- .action(() => this.myCommand());
590
- }
591
-
592
- async myCommand() {
593
- try {
594
- await this.executeAsync(
595
- async () => {
596
- // 执行异步操作
597
- await new Promise(resolve => setTimeout(resolve, 1000));
598
- return '操作结果';
599
- },
600
- '执行操作...',
601
- '操作成功',
602
- '操作失败'
603
- );
604
- } catch (error) {
605
- this.handleError(error, '执行命令失败');
606
- }
607
- }
608
- }
609
-
610
- module.exports = MyPlugin;
611
- ```
612
-
613
- ### 插件生命周期
614
-
615
- 插件的生命周期包括:
616
-
617
- 1. **初始化**:插件被加载时
618
- 2. **命令注册**:调用 `registerCommands` 方法
619
- 3. **命令执行**:用户运行插件命令时
620
- 4. **清理**:命令执行完成后
621
-
622
- ### 测试插件
623
-
624
- #### 本地测试
625
-
626
- 1. 创建插件:
627
- ```bash
628
- xx plugin create <plugin-name>
629
- ```
630
-
631
- 2. 编辑插件文件,实现具体功能
632
-
633
- 3. 运行插件命令:
634
- ```bash
635
- # 直接运行
636
- node bin/cli.js <plugin-command>
637
-
638
- # 或者使用 xx 命令(如果已全局安装)
639
- xx <plugin-command>
640
- ```
641
-
642
- #### 测试命令选项
172
+ #### 创建旧格式插件
643
173
 
644
174
  ```bash
645
- # 测试布尔选项
646
- xx <plugin-command> --verbose
647
-
648
- # 测试值选项
649
- xx <plugin-command> --name John
650
-
651
- # 测试组合选项
652
- xx <plugin-command> --verbose --name John
175
+ # 创建旧格式插件
176
+ xx plugin create <plugin-name> --format old
653
177
  ```
654
178
 
655
- ### 部署插件
656
-
657
- #### 发布到插件目录
658
-
659
- 1. 确保插件文件位于 `lib/plugins` 目录
660
- 2. 运行 `xx plugin list` 命令,确认插件已被识别
661
- 3. 插件会自动加载并注册到命令系统中
662
-
663
- #### 分享插件
664
-
665
- 如果要与他人分享插件,可以:
666
-
667
- 1. 将插件文件发送给他人
668
- 2. 他人将插件文件放入其 `lib/plugins` 目录
669
- 3. 运行 `xx plugin list` 命令,确认插件已被识别
670
-
671
- #### 从外部添加插件
672
-
673
- 可以使用 `plugin add` 命令从 URL 或本地文件添加插件:
674
-
675
- ```bash
676
- # 从 URL 添加插件
677
- xx plugin add https://example.com/my-plugin.js
179
+ #### 详细文档
678
180
 
679
- # 从本地文件添加插件
680
- xx plugin add /path/to/my-plugin.js
681
- ```
181
+ 完整的旧格式插件开发指南请参考:[旧格式插件开发教程](./plugin-development-old.md)