aihezu 1.4.0 → 1.5.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/bin/ccclear.js CHANGED
@@ -5,23 +5,10 @@ const path = require('path');
5
5
  const os = require('os');
6
6
  const fs = require('fs');
7
7
  const { modifyHostsFile } = require('../lib/hosts');
8
+ const { cleanCache, getLocalTimestamp } = require('../lib/cache');
8
9
 
9
10
  const homeDir = os.homedir();
10
11
 
11
- // 生成本地时间戳,格式:YYYYMMDDHHMMSS
12
- function getLocalTimestamp() {
13
- const now = new Date();
14
- const year = now.getFullYear();
15
- const month = String(now.getMonth() + 1).padStart(2, '0');
16
- const day = String(now.getDate()).padStart(2, '0');
17
- const hours = String(now.getHours()).padStart(2, '0');
18
- const minutes = String(now.getMinutes()).padStart(2, '0');
19
- const seconds = String(now.getSeconds()).padStart(2, '0');
20
- return `${year}${month}${day}${hours}${minutes}${seconds}`;
21
- }
22
-
23
- const timestamp = getLocalTimestamp();
24
-
25
12
  console.log('🧹 Claude Code CLI 清理工具');
26
13
  console.log('🌐 Powered by https://aihezu.dev\n');
27
14
 
@@ -32,105 +19,27 @@ try {
32
19
 
33
20
  console.log('\n=== 步骤 2: 清理 Claude Code 缓存 ===\n');
34
21
 
35
- // 检查文件是否存在
36
- const claudeDir = path.join(homeDir, '.claude');
37
22
  const claudeJson = path.join(homeDir, '.claude.json');
38
-
39
- // 定义需要清理的缓存文件和文件夹
40
- const cacheItems = [
41
- 'history.jsonl', // 历史记录
42
- 'debug', // 调试信息
43
- 'file-history', // 文件历史
44
- 'session-env', // 会话环境
45
- 'shell-snapshots', // Shell 快照
46
- 'statsig', // 统计信息
47
- 'todos' // 待办事项
48
- ];
49
-
50
- // 需要保留的配置和工具(不清理)
51
- // - settings.json (配置文件)
52
- // - commands/ (自定义命令)
53
- // - skills/ (技能)
54
- // - mcp/ (MCP 服务器)
55
- // - projects/ (项目信息)
56
- // - ide/ (IDE 配置)
57
-
58
- let hasFiles = false;
59
- let cleanedCount = 0;
60
-
61
- if (fs.existsSync(claudeDir)) {
62
- console.log('📂 开始清理 ~/.claude 目录下的缓存文件...\n');
63
-
64
- // 遍历并清理缓存项
65
- for (const item of cacheItems) {
66
- const itemPath = path.join(claudeDir, item);
67
-
68
- try {
69
- if (fs.existsSync(itemPath)) {
70
- const stat = fs.statSync(itemPath);
71
- const backupPath = `${itemPath}-backup-${timestamp}`;
72
-
73
- if (stat.isDirectory()) {
74
- console.log(`📦 备份并清理目录: ${item}/`);
75
- execSync(`mv "${itemPath}" "${backupPath}"`);
76
- } else {
77
- console.log(`📦 备份并清理文件: ${item}`);
78
- execSync(`mv "${itemPath}" "${backupPath}"`);
79
- }
80
-
81
- cleanedCount++;
82
- hasFiles = true;
83
- }
84
- } catch (e) {
85
- console.log(`⚠️ 处理 ${item} 时出错: ${e.message}`);
86
- }
87
- }
88
-
89
- // 清理旧的备份文件夹(.claude-* 格式)
90
- try {
91
- const items = fs.readdirSync(claudeDir);
92
- for (const item of items) {
93
- if (item.startsWith('.claude-') || item.startsWith('backup-')) {
94
- const itemPath = path.join(claudeDir, item);
95
- const stat = fs.statSync(itemPath);
96
-
97
- if (stat.isDirectory()) {
98
- console.log(`🗑️ 删除旧备份: ${item}/`);
99
- execSync(`rm -rf "${itemPath}"`);
100
- cleanedCount++;
101
- hasFiles = true;
102
- }
103
- }
104
- }
105
- } catch (e) {
106
- // 忽略错误
107
- }
108
-
109
- console.log('\n✅ 已保留以下配置和工具:');
110
- console.log(' - settings.json (配置文件)');
111
- console.log(' - commands/ (自定义命令)');
112
- console.log(' - skills/ (技能)');
113
- console.log(' - mcp/ (MCP 服务器)');
114
- console.log(' - projects/ (项目信息)');
115
- console.log(' - ide/ (IDE 配置)');
116
- } else {
117
- console.log('ℹ️ 未找到 ~/.claude 目录');
118
- }
23
+ const cleanedCount = cleanCache();
119
24
 
120
25
  // 备份 .claude.json 文件
26
+ let hasClaudeJson = false;
121
27
  try {
122
28
  if (fs.existsSync(claudeJson)) {
29
+ const timestamp = getLocalTimestamp();
123
30
  const backupJson = `${claudeJson}-backup-${timestamp}`;
124
31
  console.log(`\n📦 备份 ~/.claude.json 到 ${path.basename(backupJson)}`);
125
32
  execSync(`mv "${claudeJson}" "${backupJson}"`);
126
- hasFiles = true;
33
+ hasClaudeJson = true;
127
34
  }
128
35
  } catch (e) {
129
36
  console.log('ℹ️ 未找到 ~/.claude.json 文件');
130
37
  }
131
38
 
132
- if (hasFiles) {
133
- console.log(`\n✅ Claude Code 缓存已清理完成!(共处理 ${cleanedCount} 项)`);
39
+ const totalCleaned = cleanedCount + (hasClaudeJson ? 1 : 0);
40
+
41
+ if (totalCleaned > 0) {
42
+ console.log(`\n✅ Claude Code 缓存已清理完成!(共处理 ${totalCleaned} 项)`);
134
43
  console.log('💡 配置和工具已保留,下次启动 Claude Code 可直接使用');
135
44
  console.log(`📁 备份文件保存在 ~/.claude/ 目录下`);
136
45
  } else {
package/bin/ccinstall.js CHANGED
@@ -5,9 +5,11 @@ const path = require('path');
5
5
  const os = require('os');
6
6
  const fs = require('fs');
7
7
  const { modifyHostsFile } = require('../lib/hosts');
8
+ const { cleanCache } = require('../lib/cache');
8
9
 
9
10
  const homeDir = os.homedir();
10
11
  const settingsPath = path.join(homeDir, '.claude', 'settings.json');
12
+ const claudeDir = path.join(homeDir, '.claude');
11
13
 
12
14
  console.log('🔧 Claude Code API 配置工具');
13
15
  console.log('🌐 Powered by https://aihezu.dev\n');
@@ -18,8 +20,25 @@ const rl = readline.createInterface({
18
20
  output: process.stdout
19
21
  });
20
22
 
21
- // 提示用户输入 API Key
22
- rl.question('请输入您的 API Key: ', (apiKey) => {
23
+
24
+ // 询问是否需要清理缓存
25
+ rl.question('您是从其他服务切换过来的吗?(y/n,如果是首次使用请输入 n): ', (answer) => {
26
+ const needClean = answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes';
27
+
28
+ if (needClean) {
29
+ try {
30
+ console.log('\n=== 清理旧缓存 ===');
31
+ const count = cleanCache({ showHeader: false });
32
+ console.log(`\n✅ 缓存清理完成!(共处理 ${count} 项)`);
33
+ console.log('💡 配置文件已保留,即将配置新的 API Key\n');
34
+ } catch (error) {
35
+ console.error('⚠️ 清理缓存时出错:', error.message);
36
+ console.log('继续配置流程...\n');
37
+ }
38
+ }
39
+
40
+ // 提示用户输入 API Key
41
+ rl.question('请输入您的 API Key: ', (apiKey) => {
23
42
  if (!apiKey || apiKey.trim() === '') {
24
43
  console.error('❌ API Key 不能为空');
25
44
  rl.close();
@@ -30,7 +49,6 @@ rl.question('请输入您的 API Key: ', (apiKey) => {
30
49
 
31
50
  try {
32
51
  // 确保 .claude 目录存在
33
- const claudeDir = path.join(homeDir, '.claude');
34
52
  if (!fs.existsSync(claudeDir)) {
35
53
  console.log('📁 创建 ~/.claude 目录...');
36
54
  fs.mkdirSync(claudeDir, { recursive: true });
@@ -65,7 +83,7 @@ rl.question('请输入您的 API Key: ', (apiKey) => {
65
83
  // 写入配置文件
66
84
  fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2), 'utf8');
67
85
 
68
- console.log('\n=== 步骤 1: API 配置 ===\n');
86
+ console.log('\n=== API 配置 ===\n');
69
87
  console.log('✅ 配置成功!');
70
88
  console.log('📝 已更新配置文件:', settingsPath);
71
89
  console.log('\n配置内容:');
@@ -73,7 +91,7 @@ rl.question('请输入您的 API Key: ', (apiKey) => {
73
91
  console.log(' ANTHROPIC_BASE_URL:', 'https://cc.aihezu.dev/api');
74
92
 
75
93
  // 修改 hosts 文件
76
- console.log('\n=== 步骤 2: 修改 hosts 文件 ===\n');
94
+ console.log('\n=== 修改 hosts 文件 ===\n');
77
95
  modifyHostsFile();
78
96
 
79
97
  console.log('\n=== 全部完成 ===');
@@ -86,4 +104,5 @@ rl.question('请输入您的 API Key: ', (apiKey) => {
86
104
  } finally {
87
105
  rl.close();
88
106
  }
89
- });
107
+ });
108
+ });
package/lib/cache.js ADDED
@@ -0,0 +1,113 @@
1
+ const { execSync } = require('child_process');
2
+ const path = require('path');
3
+ const os = require('os');
4
+ const fs = require('fs');
5
+
6
+ const homeDir = os.homedir();
7
+ const claudeDir = path.join(homeDir, '.claude');
8
+
9
+ // 生成本地时间戳,格式:YYYYMMDDHHMMSS
10
+ function getLocalTimestamp() {
11
+ const now = new Date();
12
+ const year = now.getFullYear();
13
+ const month = String(now.getMonth() + 1).padStart(2, '0');
14
+ const day = String(now.getDate()).padStart(2, '0');
15
+ const hours = String(now.getHours()).padStart(2, '0');
16
+ const minutes = String(now.getMinutes()).padStart(2, '0');
17
+ const seconds = String(now.getSeconds()).padStart(2, '0');
18
+ return `${year}${month}${day}${hours}${minutes}${seconds}`;
19
+ }
20
+
21
+ // 清理缓存函数
22
+ function cleanCache(options = {}) {
23
+ const { showHeader = true } = options;
24
+
25
+ if (showHeader) {
26
+ console.log('\n=== 清理缓存 ===\n');
27
+ }
28
+
29
+ const timestamp = getLocalTimestamp();
30
+ const cacheItems = [
31
+ 'history.jsonl', // 历史记录
32
+ 'debug', // 调试信息
33
+ 'file-history', // 文件历史
34
+ 'session-env', // 会话环境
35
+ 'shell-snapshots', // Shell 快照
36
+ 'statsig', // 统计信息
37
+ 'todos' // 待办事项
38
+ ];
39
+
40
+ // 需要保留的配置和工具(不清理)
41
+ // - settings.json (配置文件)
42
+ // - commands/ (自定义命令)
43
+ // - skills/ (技能)
44
+ // - mcp/ (MCP 服务器)
45
+ // - projects/ (项目信息)
46
+ // - ide/ (IDE 配置)
47
+
48
+ let cleanedCount = 0;
49
+
50
+ if (fs.existsSync(claudeDir)) {
51
+ console.log('📂 开始清理 ~/.claude 目录下的缓存文件...\n');
52
+
53
+ // 遍历并清理缓存项
54
+ for (const item of cacheItems) {
55
+ const itemPath = path.join(claudeDir, item);
56
+
57
+ try {
58
+ if (fs.existsSync(itemPath)) {
59
+ const stat = fs.statSync(itemPath);
60
+ const backupPath = `${itemPath}-backup-${timestamp}`;
61
+
62
+ if (stat.isDirectory()) {
63
+ console.log(`📦 备份并清理目录: ${item}/`);
64
+ execSync(`mv "${itemPath}" "${backupPath}"`);
65
+ } else {
66
+ console.log(`📦 备份并清理文件: ${item}`);
67
+ execSync(`mv "${itemPath}" "${backupPath}"`);
68
+ }
69
+
70
+ cleanedCount++;
71
+ }
72
+ } catch (e) {
73
+ console.log(`⚠️ 处理 ${item} 时出错: ${e.message}`);
74
+ }
75
+ }
76
+
77
+ // 清理旧的备份文件夹(.claude-* 格式)
78
+ try {
79
+ const items = fs.readdirSync(claudeDir);
80
+ for (const item of items) {
81
+ if (item.startsWith('.claude-') || item.startsWith('backup-')) {
82
+ const itemPath = path.join(claudeDir, item);
83
+ const stat = fs.statSync(itemPath);
84
+
85
+ if (stat.isDirectory()) {
86
+ console.log(`🗑️ 删除旧备份: ${item}/`);
87
+ execSync(`rm -rf "${itemPath}"`);
88
+ cleanedCount++;
89
+ }
90
+ }
91
+ }
92
+ } catch (e) {
93
+ // 忽略错误
94
+ }
95
+
96
+ console.log('\n✅ 已保留以下配置和工具:');
97
+ console.log(' - settings.json (配置文件)');
98
+ console.log(' - commands/ (自定义命令)');
99
+ console.log(' - skills/ (技能)');
100
+ console.log(' - mcp/ (MCP 服务器)');
101
+ console.log(' - projects/ (项目信息)');
102
+ console.log(' - ide/ (IDE 配置)');
103
+ } else {
104
+ console.log('ℹ️ 未找到 ~/.claude 目录');
105
+ }
106
+
107
+ return cleanedCount;
108
+ }
109
+
110
+ module.exports = {
111
+ cleanCache,
112
+ getLocalTimestamp
113
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aihezu",
3
- "version": "1.4.0",
3
+ "version": "1.5.1",
4
4
  "description": "Claude Code CLI 清理工具 - 快速备份和清理 Claude Code 的本地配置和缓存,同时修改 hosts 文件实现本地代理",
5
5
  "main": "bin/ccclear.js",
6
6
  "bin": {