@adversity/coding-tool-x 2.5.0 → 2.6.0

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.
@@ -5,6 +5,8 @@
5
5
 
6
6
  const fs = require('fs');
7
7
  const path = require('path');
8
+ const os = require('os');
9
+ const AdmZip = require('adm-zip');
8
10
  const permissionTemplatesService = require('./permission-templates-service');
9
11
  const configTemplatesService = require('./config-templates-service');
10
12
  const channelsService = require('./channels');
@@ -13,10 +15,20 @@ const { CommandsService } = require('./commands-service');
13
15
  const { RulesService } = require('./rules-service');
14
16
  const { SkillService } = require('./skill-service');
15
17
 
16
- const CONFIG_VERSION = '1.0.0';
18
+ const CONFIG_VERSION = '1.1.0';
17
19
  const SKILL_FILE_ENCODING = 'base64';
18
20
  const SKILL_IGNORE_DIRS = new Set(['.git']);
19
21
  const SKILL_IGNORE_FILES = new Set(['.DS_Store']);
22
+ const CC_TOOL_DIR = path.join(os.homedir(), '.claude', 'cc-tool');
23
+ const LEGACY_CC_TOOL_DIR = path.join(os.homedir(), '.cc-tool');
24
+ const CLAUDE_SETTINGS_PATH = path.join(os.homedir(), '.claude', 'settings.json');
25
+ const CC_UI_CONFIG_PATH = path.join(CC_TOOL_DIR, 'ui-config.json');
26
+ const CC_TERMINAL_CONFIG_PATH = path.join(CC_TOOL_DIR, 'terminal-config.json');
27
+ const CC_TERMINAL_COMMANDS_PATH = path.join(CC_TOOL_DIR, 'terminal-commands.json');
28
+ const CC_PROMPTS_PATH = path.join(CC_TOOL_DIR, 'prompts.json');
29
+ const CC_SECURITY_PATH = path.join(CC_TOOL_DIR, 'security.json');
30
+ const LEGACY_UI_CONFIG_PATH = path.join(LEGACY_CC_TOOL_DIR, 'ui-config.json');
31
+ const LEGACY_NOTIFY_HOOK_PATH = path.join(LEGACY_CC_TOOL_DIR, 'notify-hook.js');
20
32
 
21
33
  function ensureDir(dirPath) {
22
34
  if (!fs.existsSync(dirPath)) {
@@ -24,6 +36,90 @@ function ensureDir(dirPath) {
24
36
  }
25
37
  }
26
38
 
39
+ function readJsonFileSafe(filePath) {
40
+ if (!filePath || !fs.existsSync(filePath)) return null;
41
+ try {
42
+ const content = fs.readFileSync(filePath, 'utf8');
43
+ return JSON.parse(content);
44
+ } catch (err) {
45
+ return null;
46
+ }
47
+ }
48
+
49
+ function readTextFileSafe(filePath) {
50
+ if (!filePath || !fs.existsSync(filePath)) return null;
51
+ try {
52
+ return fs.readFileSync(filePath, 'utf8');
53
+ } catch (err) {
54
+ return null;
55
+ }
56
+ }
57
+
58
+ function writeJsonFileAbsolute(filePath, data, overwrite, options = {}) {
59
+ if (data === undefined) {
60
+ return 'failed';
61
+ }
62
+ if (fs.existsSync(filePath) && !overwrite) return 'skipped';
63
+ ensureDir(path.dirname(filePath));
64
+ fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
65
+ if (options.mode) {
66
+ try {
67
+ fs.chmodSync(filePath, options.mode);
68
+ } catch (err) {
69
+ // ignore chmod errors
70
+ }
71
+ }
72
+ return 'success';
73
+ }
74
+
75
+ function writeTextFileAbsolute(filePath, content, overwrite, options = {}) {
76
+ if (content === undefined || content === null) {
77
+ return 'failed';
78
+ }
79
+ if (fs.existsSync(filePath) && !overwrite) return 'skipped';
80
+ ensureDir(path.dirname(filePath));
81
+ fs.writeFileSync(filePath, content, 'utf8');
82
+ if (options.mode) {
83
+ try {
84
+ fs.chmodSync(filePath, options.mode);
85
+ } catch (err) {
86
+ // ignore chmod errors
87
+ }
88
+ }
89
+ return 'success';
90
+ }
91
+
92
+ function getConfigFilePath() {
93
+ try {
94
+ const loaderPath = require.resolve('../../config/loader');
95
+ return path.resolve(path.dirname(loaderPath), '../../config.json');
96
+ } catch (err) {
97
+ return null;
98
+ }
99
+ }
100
+
101
+ function buildExportReadme(exportData) {
102
+ const exportedAt = exportData.exportedAt ? new Date(exportData.exportedAt).toLocaleString('zh-CN') : '';
103
+ return `# Coding Tool 配置导出包
104
+
105
+ 导出时间: ${exportedAt}
106
+ 版本: ${exportData.version || CONFIG_VERSION}
107
+
108
+ ## 📦 包含内容
109
+ - 权限模板、配置模板、频道配置、工作区、收藏
110
+ - Agents / Skills / Commands / Rules
111
+ - MCP 服务器配置
112
+ - UI 配置(主题、面板显示、排序等)
113
+ - 终端配置与 CLI 命令配置
114
+ - Prompts 预设
115
+ - 安全配置
116
+ - 高级配置(端口、日志、性能等)
117
+ - Claude Hooks 配置(如通知脚本)
118
+
119
+ > 注意:配置包可能包含 API Key、Webhook 等敏感信息,请妥善保管。
120
+ `;
121
+ }
122
+
27
123
  function resolveSafePath(baseDir, relativePath) {
28
124
  if (!relativePath || typeof relativePath !== 'string') return null;
29
125
  const normalized = path.normalize(relativePath);
@@ -252,6 +348,27 @@ function exportAllConfigs() {
252
348
  }
253
349
  }
254
350
 
351
+ // 获取 UI / 前端配置
352
+ const { loadUIConfig } = require('./ui-config');
353
+ const { loadTerminalConfig } = require('./terminal-config');
354
+ const { loadTerminalCommands } = require('./terminal-commands');
355
+ const { loadConfig } = require('../../config/loader');
356
+
357
+ const uiConfigFile = readJsonFileSafe(CC_UI_CONFIG_PATH);
358
+ const uiConfig = uiConfigFile || loadUIConfig();
359
+ const terminalConfig = loadTerminalConfig();
360
+ const terminalCommands = loadTerminalCommands();
361
+ const prompts = readJsonFileSafe(CC_PROMPTS_PATH);
362
+ const security = readJsonFileSafe(CC_SECURITY_PATH);
363
+ const appConfig = loadConfig();
364
+
365
+ const claudeSettings = readJsonFileSafe(CLAUDE_SETTINGS_PATH);
366
+ const claudeHooks = {
367
+ uiConfig: readJsonFileSafe(LEGACY_UI_CONFIG_PATH),
368
+ notifyScript: readTextFileSafe(LEGACY_NOTIFY_HOOK_PATH),
369
+ stopHook: claudeSettings?.hooks?.Stop || null
370
+ };
371
+
255
372
  const exportData = {
256
373
  version: CONFIG_VERSION,
257
374
  exportedAt: new Date().toISOString(),
@@ -266,7 +383,14 @@ function exportAllConfigs() {
266
383
  commands: commands || [],
267
384
  rules: rules || [],
268
385
  mcpServers: mcpServers || [],
269
- markdownFiles: markdownFiles
386
+ markdownFiles: markdownFiles,
387
+ uiConfig: uiConfig,
388
+ terminalConfig: terminalConfig,
389
+ terminalCommands: terminalCommands,
390
+ prompts: prompts,
391
+ security: security,
392
+ appConfig: appConfig,
393
+ claudeHooks: claudeHooks
270
394
  }
271
395
  };
272
396
 
@@ -283,6 +407,30 @@ function exportAllConfigs() {
283
407
  }
284
408
  }
285
409
 
410
+ /**
411
+ * 导出所有配置为 ZIP
412
+ * @returns {Object} { success, data: Buffer, filename }
413
+ */
414
+ function exportAllConfigsZip() {
415
+ const result = exportAllConfigs();
416
+ if (!result.success) {
417
+ return result;
418
+ }
419
+
420
+ const exportData = result.data;
421
+ const zip = new AdmZip();
422
+ const jsonContent = JSON.stringify(exportData, null, 2);
423
+
424
+ zip.addFile('config.json', Buffer.from(jsonContent, 'utf8'));
425
+ zip.addFile('README.md', Buffer.from(buildExportReadme(exportData), 'utf8'));
426
+
427
+ return {
428
+ success: true,
429
+ data: zip.toBuffer(),
430
+ filename: `ctx-config-${new Date().toISOString().split('T')[0]}.zip`
431
+ };
432
+ }
433
+
286
434
  /**
287
435
  * 导入配置
288
436
  * @param {Object} importData - 导入的配置对象
@@ -302,7 +450,14 @@ function importConfigs(importData, options = {}) {
302
450
  commands: { success: 0, failed: 0, skipped: 0 },
303
451
  rules: { success: 0, failed: 0, skipped: 0 },
304
452
  mcpServers: { success: 0, failed: 0, skipped: 0 },
305
- markdownFiles: { success: 0, failed: 0, skipped: 0 }
453
+ markdownFiles: { success: 0, failed: 0, skipped: 0 },
454
+ uiConfig: { success: 0, failed: 0, skipped: 0 },
455
+ terminalConfig: { success: 0, failed: 0, skipped: 0 },
456
+ terminalCommands: { success: 0, failed: 0, skipped: 0 },
457
+ prompts: { success: 0, failed: 0, skipped: 0 },
458
+ security: { success: 0, failed: 0, skipped: 0 },
459
+ appConfig: { success: 0, failed: 0, skipped: 0 },
460
+ claudeHooks: { success: 0, failed: 0, skipped: 0 }
306
461
  };
307
462
 
308
463
  try {
@@ -322,7 +477,14 @@ function importConfigs(importData, options = {}) {
322
477
  commands = [],
323
478
  rules = [],
324
479
  mcpServers = [],
325
- markdownFiles = {}
480
+ markdownFiles = {},
481
+ uiConfig = null,
482
+ terminalConfig = null,
483
+ terminalCommands = null,
484
+ prompts = null,
485
+ security = null,
486
+ appConfig = null,
487
+ claudeHooks = null
326
488
  } = importData.data;
327
489
 
328
490
  // 导入权限模板
@@ -602,6 +764,162 @@ function importConfigs(importData, options = {}) {
602
764
  }
603
765
  }
604
766
 
767
+ // 导入 UI 配置
768
+ if (uiConfig && typeof uiConfig === 'object') {
769
+ try {
770
+ if (fs.existsSync(CC_UI_CONFIG_PATH) && !overwrite) {
771
+ results.uiConfig.skipped++;
772
+ } else {
773
+ const { saveUIConfig } = require('./ui-config');
774
+ saveUIConfig(uiConfig);
775
+ results.uiConfig.success++;
776
+ }
777
+ } catch (err) {
778
+ console.error('[ConfigImport] 导入 UI 配置失败:', err);
779
+ results.uiConfig.failed++;
780
+ }
781
+ }
782
+
783
+ // 导入终端配置
784
+ if (terminalConfig && typeof terminalConfig === 'object') {
785
+ try {
786
+ if (fs.existsSync(CC_TERMINAL_CONFIG_PATH) && !overwrite) {
787
+ results.terminalConfig.skipped++;
788
+ } else {
789
+ const { saveTerminalConfig } = require('./terminal-config');
790
+ saveTerminalConfig(terminalConfig);
791
+ results.terminalConfig.success++;
792
+ }
793
+ } catch (err) {
794
+ console.error('[ConfigImport] 导入终端配置失败:', err);
795
+ results.terminalConfig.failed++;
796
+ }
797
+ }
798
+
799
+ // 导入终端命令配置
800
+ if (terminalCommands && typeof terminalCommands === 'object') {
801
+ try {
802
+ if (fs.existsSync(CC_TERMINAL_COMMANDS_PATH) && !overwrite) {
803
+ results.terminalCommands.skipped++;
804
+ } else {
805
+ const { saveTerminalCommands } = require('./terminal-commands');
806
+ const ok = saveTerminalCommands(terminalCommands);
807
+ if (ok) {
808
+ results.terminalCommands.success++;
809
+ } else {
810
+ results.terminalCommands.failed++;
811
+ }
812
+ }
813
+ } catch (err) {
814
+ console.error('[ConfigImport] 导入终端命令配置失败:', err);
815
+ results.terminalCommands.failed++;
816
+ }
817
+ }
818
+
819
+ // 导入 Prompts 配置
820
+ if (prompts && typeof prompts === 'object') {
821
+ try {
822
+ const status = writeJsonFileAbsolute(CC_PROMPTS_PATH, prompts, overwrite);
823
+ if (status === 'success') {
824
+ results.prompts.success++;
825
+ } else if (status === 'skipped') {
826
+ results.prompts.skipped++;
827
+ } else {
828
+ results.prompts.failed++;
829
+ }
830
+ } catch (err) {
831
+ console.error('[ConfigImport] 导入 Prompts 失败:', err);
832
+ results.prompts.failed++;
833
+ }
834
+ }
835
+
836
+ // 导入安全配置
837
+ if (security && typeof security === 'object') {
838
+ try {
839
+ const status = writeJsonFileAbsolute(CC_SECURITY_PATH, security, overwrite, { mode: 0o600 });
840
+ if (status === 'success') {
841
+ results.security.success++;
842
+ } else if (status === 'skipped') {
843
+ results.security.skipped++;
844
+ } else {
845
+ results.security.failed++;
846
+ }
847
+ } catch (err) {
848
+ console.error('[ConfigImport] 导入安全配置失败:', err);
849
+ results.security.failed++;
850
+ }
851
+ }
852
+
853
+ // 导入高级配置(config.json)
854
+ if (appConfig && typeof appConfig === 'object') {
855
+ try {
856
+ const configFilePath = getConfigFilePath();
857
+ if (configFilePath && fs.existsSync(configFilePath) && !overwrite) {
858
+ results.appConfig.skipped++;
859
+ } else {
860
+ const { saveConfig } = require('../../config/loader');
861
+ saveConfig(appConfig);
862
+ results.appConfig.success++;
863
+ }
864
+ } catch (err) {
865
+ console.error('[ConfigImport] 导入高级配置失败:', err);
866
+ results.appConfig.failed++;
867
+ }
868
+ }
869
+
870
+ // 导入 Claude Hooks 配置
871
+ if (claudeHooks && typeof claudeHooks === 'object') {
872
+ let didWrite = false;
873
+ let didSkip = false;
874
+ let didFail = false;
875
+
876
+ try {
877
+ if (claudeHooks.uiConfig && typeof claudeHooks.uiConfig === 'object') {
878
+ const status = writeJsonFileAbsolute(LEGACY_UI_CONFIG_PATH, claudeHooks.uiConfig, overwrite);
879
+ if (status === 'success') didWrite = true;
880
+ else if (status === 'skipped') didSkip = true;
881
+ else didFail = true;
882
+ }
883
+
884
+ if (claudeHooks.notifyScript !== undefined && claudeHooks.notifyScript !== null) {
885
+ const status = writeTextFileAbsolute(LEGACY_NOTIFY_HOOK_PATH, claudeHooks.notifyScript, overwrite, { mode: 0o755 });
886
+ if (status === 'success') didWrite = true;
887
+ else if (status === 'skipped') didSkip = true;
888
+ else didFail = true;
889
+ }
890
+
891
+ if (claudeHooks.stopHook !== undefined) {
892
+ if (!overwrite && fs.existsSync(CLAUDE_SETTINGS_PATH)) {
893
+ didSkip = true;
894
+ } else {
895
+ const settings = readJsonFileSafe(CLAUDE_SETTINGS_PATH) || {};
896
+ settings.hooks = settings.hooks || {};
897
+ if (claudeHooks.stopHook) {
898
+ settings.hooks.Stop = claudeHooks.stopHook;
899
+ } else if (settings.hooks.Stop) {
900
+ delete settings.hooks.Stop;
901
+ if (Object.keys(settings.hooks).length === 0) {
902
+ delete settings.hooks;
903
+ }
904
+ }
905
+ writeJsonFileAbsolute(CLAUDE_SETTINGS_PATH, settings, true);
906
+ didWrite = true;
907
+ }
908
+ }
909
+ } catch (err) {
910
+ console.error('[ConfigImport] 导入 Claude Hooks 失败:', err);
911
+ didFail = true;
912
+ }
913
+
914
+ if (didFail) {
915
+ results.claudeHooks.failed++;
916
+ } else if (didWrite) {
917
+ results.claudeHooks.success++;
918
+ } else if (didSkip) {
919
+ results.claudeHooks.skipped++;
920
+ }
921
+ }
922
+
605
923
  return {
606
924
  success: true,
607
925
  results,
@@ -634,7 +952,14 @@ function generateImportSummary(results) {
634
952
  { key: 'commands', label: 'Commands' },
635
953
  { key: 'rules', label: 'Rules' },
636
954
  { key: 'mcpServers', label: 'MCP服务器' },
637
- { key: 'markdownFiles', label: 'Markdown文件' }
955
+ { key: 'markdownFiles', label: 'Markdown文件' },
956
+ { key: 'uiConfig', label: 'UI配置' },
957
+ { key: 'terminalConfig', label: '终端配置' },
958
+ { key: 'terminalCommands', label: '终端命令' },
959
+ { key: 'prompts', label: 'Prompts' },
960
+ { key: 'security', label: '安全配置' },
961
+ { key: 'appConfig', label: '高级配置' },
962
+ { key: 'claudeHooks', label: 'Claude Hooks' }
638
963
  ];
639
964
 
640
965
  for (const { key, label } of types) {
@@ -658,5 +983,6 @@ function generateImportSummary(results) {
658
983
 
659
984
  module.exports = {
660
985
  exportAllConfigs,
986
+ exportAllConfigsZip,
661
987
  importConfigs
662
988
  };