@xubill/xx-cli 2.0.1 → 2.0.2

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/lib/core/index.js CHANGED
@@ -229,6 +229,7 @@ class Core {
229
229
  `警告: 插件 ${pluginName} 的命令别名与其他插件冲突,已跳过此插件的命令注册`,
230
230
  );
231
231
  } else {
232
+ console.log(error)
232
233
  console.error(
233
234
  `错误: 插件 ${pluginName} 注册命令失败: ${error.message}`,
234
235
  );
@@ -150,7 +150,7 @@ class PluginManager {
150
150
  */
151
151
  toCamelCase(str) {
152
152
  if (!str) {
153
- return 'Plugin';
153
+ return "Plugin";
154
154
  }
155
155
  return str
156
156
  .replace(/-([a-zA-Z])/g, (g) => g[1].toUpperCase())
@@ -166,14 +166,14 @@ class PluginManager {
166
166
  generatePluginTemplate(name, format) {
167
167
  // 参数检查
168
168
  if (!name) {
169
- throw new Error('插件名称不能为空');
169
+ throw new Error("插件名称不能为空");
170
170
  }
171
-
171
+
172
172
  // 生成插件命令名称(去除后缀,转小写,连字符转驼峰)
173
173
  const commandName = name
174
174
  .replace(".js", "")
175
175
  .toLowerCase()
176
- .replace(/-(\w)/g, (g) => g[1].toUpperCase());
176
+ .replace(/-([a-zA-Z])/g, (g) => g[1].toUpperCase());
177
177
 
178
178
  // 生成驼峰命名的类名
179
179
  const className = this.toCamelCase(name);
@@ -302,7 +302,9 @@ module.exports = {
302
302
  }
303
303
  });
304
304
  } catch (error) {
305
- helper.showInfo("提示:无法自动打开 vscode,请手动打开插件文件进行编辑");
305
+ helper.showInfo(
306
+ "提示:无法自动打开 vscode,请手动打开插件文件进行编辑",
307
+ );
306
308
  }
307
309
  } catch (error) {
308
310
  helper.showError(`创建插件模板失败: ${error.message || error}`);
@@ -400,7 +402,10 @@ module.exports = {
400
402
  .get(url, (response) => {
401
403
  if (response.statusCode === 200) {
402
404
  // 获取文件大小
403
- const fileSize = parseInt(response.headers["content-length"], 10);
405
+ const fileSize = parseInt(
406
+ response.headers["content-length"],
407
+ 10,
408
+ );
404
409
  let downloadedSize = 0;
405
410
 
406
411
  // 创建写入流
@@ -468,6 +473,67 @@ module.exports = {
468
473
  helper.showError("添加插件失败:", error.message);
469
474
  }
470
475
  }
476
+
477
+ /**
478
+ * 同步开发插件
479
+ * 将当前目录下的 lib/myplugins 同步到 CLI 配置目录的 plugins
480
+ * @param {Object} helper - 插件帮助器
481
+ */
482
+ async syncDevPlugins(helper) {
483
+ try {
484
+ // 确保用户插件目录存在
485
+ fs.ensureDirSync(this.userPluginsDir);
486
+
487
+ // 开发插件目录路径
488
+ const devPluginsDir = path.join(process.cwd(), "lib/myplugins");
489
+
490
+ // 检查开发插件目录是否存在
491
+ if (!fs.existsSync(devPluginsDir)) {
492
+ helper.showError(`开发插件目录不存在: ${devPluginsDir}`);
493
+ return;
494
+ }
495
+
496
+ // 读取开发插件目录中的文件
497
+ const devPluginFiles = fs
498
+ .readdirSync(devPluginsDir)
499
+ .filter((file) => file.endsWith(".js"));
500
+
501
+ if (devPluginFiles.length === 0) {
502
+ helper.showInfo("开发插件目录中没有 JavaScript 文件");
503
+ return;
504
+ }
505
+
506
+ helper.showInfo(
507
+ `找到 ${devPluginFiles.length} 个开发插件文件,开始同步...`,
508
+ );
509
+
510
+ // 同步每个插件文件
511
+ let syncedCount = 0;
512
+ for (const file of devPluginFiles) {
513
+ const sourcePath = path.join(devPluginsDir, file);
514
+ const targetPath = path.join(this.userPluginsDir, file);
515
+
516
+ // 检查源文件是否存在
517
+ if (fs.existsSync(sourcePath)) {
518
+ // 复制文件
519
+ await fs.copy(sourcePath, targetPath, { overwrite: true });
520
+ helper.showSuccess(`已同步: ${file}`);
521
+ syncedCount++;
522
+ } else {
523
+ helper.showWarning(`跳过不存在的文件: ${file}`);
524
+ }
525
+ }
526
+
527
+ helper.showSuccess(`同步完成,共同步 ${syncedCount} 个插件文件`);
528
+ helper.showInfo(`插件已同步到: ${this.userPluginsDir}`);
529
+ helper.showInfo("\n提示:");
530
+ helper.showInfo("- 插件已自动注册到命令系统");
531
+ helper.showInfo('- 可以使用 "xx plugin list" 命令查看已安装的插件');
532
+ helper.showInfo('- 可以使用 "xx <plugin-command>" 命令使用插件功能');
533
+ } catch (error) {
534
+ helper.showError(`同步开发插件失败: ${error.message}`);
535
+ }
536
+ }
471
537
  }
472
538
 
473
539
  // 创建插件管理实例
@@ -567,21 +633,21 @@ module.exports = {
567
633
  action: async (args, options, helper) => {
568
634
  helper.showInfo(`args 对象: ${JSON.stringify(args)}`);
569
635
  helper.showInfo(`options 对象: ${JSON.stringify(options)}`);
570
-
636
+
571
637
  // 提取插件名称
572
- let pluginName = 'unnamed';
638
+ let pluginName = "unnamed";
573
639
  if (Array.isArray(args)) {
574
640
  // 遍历数组,找到非对象的字符串参数
575
641
  for (const arg of args) {
576
- if (typeof arg === 'string') {
642
+ if (typeof arg === "string") {
577
643
  pluginName = arg;
578
644
  break;
579
645
  }
580
646
  }
581
- } else if (typeof args === 'string') {
647
+ } else if (typeof args === "string") {
582
648
  pluginName = args;
583
649
  }
584
-
650
+
585
651
  helper.showInfo(`提取的插件名称: ${pluginName}`);
586
652
  await pluginManager.createPlugin(pluginName, options, helper);
587
653
  },
@@ -608,7 +674,20 @@ module.exports = {
608
674
  },
609
675
  ],
610
676
  action: (args, options, helper) => {
611
- pluginManager.managePluginConfig(args.plugin, args.key, args.value, helper);
677
+ pluginManager.managePluginConfig(
678
+ args.plugin,
679
+ args.key,
680
+ args.value,
681
+ helper,
682
+ );
683
+ },
684
+ },
685
+ {
686
+ name: "sync",
687
+ alias: "s",
688
+ description: "将当前目录下的 lib/myplugins 同步到 CLI 配置目录的 plugins",
689
+ action: async (args, options, helper) => {
690
+ await pluginManager.syncDevPlugins(helper);
612
691
  },
613
692
  },
614
693
  ],
@@ -618,4 +697,4 @@ module.exports = {
618
697
  description: "显示帮助信息",
619
698
  },
620
699
  ],
621
- };
700
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xubill/xx-cli",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "个人工具集",
5
5
  "main": "lib/core/index.js",
6
6
  "bin": {
File without changes