aihezu 1.3.1 → 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
@@ -4,124 +4,14 @@ const { execSync } = require('child_process');
4
4
  const path = require('path');
5
5
  const os = require('os');
6
6
  const fs = require('fs');
7
+ const { modifyHostsFile } = require('../lib/hosts');
8
+ const { cleanCache, getLocalTimestamp } = require('../lib/cache');
7
9
 
8
10
  const homeDir = os.homedir();
9
11
 
10
- // 生成本地时间戳,格式:YYYYMMDDHHMMSS
11
- function getLocalTimestamp() {
12
- const now = new Date();
13
- const year = now.getFullYear();
14
- const month = String(now.getMonth() + 1).padStart(2, '0');
15
- const day = String(now.getDate()).padStart(2, '0');
16
- const hours = String(now.getHours()).padStart(2, '0');
17
- const minutes = String(now.getMinutes()).padStart(2, '0');
18
- const seconds = String(now.getSeconds()).padStart(2, '0');
19
- return `${year}${month}${day}${hours}${minutes}${seconds}`;
20
- }
21
-
22
- const timestamp = getLocalTimestamp();
23
-
24
12
  console.log('🧹 Claude Code CLI 清理工具');
25
13
  console.log('🌐 Powered by https://aihezu.dev\n');
26
14
 
27
- // 修改 hosts 文件的函数
28
- function modifyHostsFile() {
29
- const isWindows = os.platform() === 'win32';
30
- const hostsPath = isWindows
31
- ? 'C:\\Windows\\System32\\drivers\\etc\\hosts'
32
- : '/etc/hosts';
33
-
34
- const domains = [
35
- 'statsig.anthropic.com',
36
- 'api.anthropic.com'
37
- ];
38
-
39
- try {
40
- console.log('🔧 开始修改 hosts 文件...');
41
-
42
- // 读取现有 hosts 文件内容
43
- let hostsContent = '';
44
- try {
45
- hostsContent = fs.readFileSync(hostsPath, 'utf8');
46
- } catch (error) {
47
- console.error('❌ 无法读取 hosts 文件,请确保以管理员/root权限运行');
48
- return false;
49
- }
50
-
51
- // 备份 hosts 文件
52
- const hostsBackup = `${hostsPath}.backup-${timestamp}`;
53
- try {
54
- fs.writeFileSync(hostsBackup, hostsContent);
55
- console.log(`📦 已备份 hosts 文件到 ${hostsBackup}`);
56
- } catch (error) {
57
- console.error('⚠️ 备份 hosts 文件失败:', error.message);
58
- }
59
-
60
- // 移除所有包含目标域名的现有条目(无论指向什么IP)
61
- const lines = hostsContent.split('\n');
62
- const filteredLines = lines.filter(line => {
63
- const trimmed = line.trim();
64
- // 跳过注释行(但保留其他注释)
65
- if (trimmed.startsWith('#')) {
66
- // 如果是之前添加的标记注释,也移除
67
- if (trimmed.includes('Added by aihezu ccclear tool')) {
68
- return false;
69
- }
70
- return true;
71
- }
72
- // 移除任何包含目标域名的行
73
- return !domains.some(domain => {
74
- const regex = new RegExp('\\s+' + domain.replace('.', '\\.') + '(\\s|$)', 'i');
75
- return regex.test(trimmed);
76
- });
77
- });
78
-
79
- // 添加新的条目
80
- const newEntries = [
81
- '',
82
- '# Added by aihezu ccclear tool',
83
- '127.0.0.1 statsig.anthropic.com',
84
- '127.0.0.1 api.anthropic.com'
85
- ];
86
-
87
- const newHostsContent = filteredLines.join('\n') + '\n' + newEntries.join('\n') + '\n';
88
-
89
- // 写入 hosts 文件
90
- try {
91
- fs.writeFileSync(hostsPath, newHostsContent);
92
- console.log('✅ hosts 文件修改成功!');
93
- console.log(' 已添加/更新以下域名解析:');
94
- console.log(' - statsig.anthropic.com -> 127.0.0.1');
95
- console.log(' - api.anthropic.com -> 127.0.0.1');
96
-
97
- // 刷新 DNS 缓存
98
- try {
99
- if (isWindows) {
100
- execSync('ipconfig /flushdns', { stdio: 'ignore' });
101
- } else if (os.platform() === 'darwin') {
102
- execSync('sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder', { stdio: 'ignore' });
103
- } else {
104
- // Linux
105
- execSync('sudo systemd-resolve --flush-caches 2>/dev/null || sudo service nscd restart 2>/dev/null || true', { stdio: 'ignore' });
106
- }
107
- console.log('🔄 DNS 缓存已刷新');
108
- } catch (e) {
109
- console.log('ℹ️ DNS 缓存刷新失败(可能需要手动刷新)');
110
- }
111
-
112
- return true;
113
- } catch (error) {
114
- console.error('❌ 无法写入 hosts 文件,请确保以管理员/root权限运行');
115
- console.error(' macOS/Linux: 使用 sudo 运行');
116
- console.error(' Windows: 以管理员身份运行命令提示符');
117
- return false;
118
- }
119
- } catch (error) {
120
- console.error('❌ 修改 hosts 文件失败:', error.message);
121
- return false;
122
- }
123
- }
124
-
125
15
  try {
126
16
  // 首先修改 hosts 文件
127
17
  console.log('=== 步骤 1: 修改 hosts 文件 ===\n');
@@ -129,105 +19,27 @@ try {
129
19
 
130
20
  console.log('\n=== 步骤 2: 清理 Claude Code 缓存 ===\n');
131
21
 
132
- // 检查文件是否存在
133
- const claudeDir = path.join(homeDir, '.claude');
134
22
  const claudeJson = path.join(homeDir, '.claude.json');
135
-
136
- // 定义需要清理的缓存文件和文件夹
137
- const cacheItems = [
138
- 'history.jsonl', // 历史记录
139
- 'debug', // 调试信息
140
- 'file-history', // 文件历史
141
- 'session-env', // 会话环境
142
- 'shell-snapshots', // Shell 快照
143
- 'statsig', // 统计信息
144
- 'todos' // 待办事项
145
- ];
146
-
147
- // 需要保留的配置和工具(不清理)
148
- // - settings.json (配置文件)
149
- // - commands/ (自定义命令)
150
- // - skills/ (技能)
151
- // - mcp/ (MCP 服务器)
152
- // - projects/ (项目信息)
153
- // - ide/ (IDE 配置)
154
-
155
- let hasFiles = false;
156
- let cleanedCount = 0;
157
-
158
- if (fs.existsSync(claudeDir)) {
159
- console.log('📂 开始清理 ~/.claude 目录下的缓存文件...\n');
160
-
161
- // 遍历并清理缓存项
162
- for (const item of cacheItems) {
163
- const itemPath = path.join(claudeDir, item);
164
-
165
- try {
166
- if (fs.existsSync(itemPath)) {
167
- const stat = fs.statSync(itemPath);
168
- const backupPath = `${itemPath}-backup-${timestamp}`;
169
-
170
- if (stat.isDirectory()) {
171
- console.log(`📦 备份并清理目录: ${item}/`);
172
- execSync(`mv "${itemPath}" "${backupPath}"`);
173
- } else {
174
- console.log(`📦 备份并清理文件: ${item}`);
175
- execSync(`mv "${itemPath}" "${backupPath}"`);
176
- }
177
-
178
- cleanedCount++;
179
- hasFiles = true;
180
- }
181
- } catch (e) {
182
- console.log(`⚠️ 处理 ${item} 时出错: ${e.message}`);
183
- }
184
- }
185
-
186
- // 清理旧的备份文件夹(.claude-* 格式)
187
- try {
188
- const items = fs.readdirSync(claudeDir);
189
- for (const item of items) {
190
- if (item.startsWith('.claude-') || item.startsWith('backup-')) {
191
- const itemPath = path.join(claudeDir, item);
192
- const stat = fs.statSync(itemPath);
193
-
194
- if (stat.isDirectory()) {
195
- console.log(`🗑️ 删除旧备份: ${item}/`);
196
- execSync(`rm -rf "${itemPath}"`);
197
- cleanedCount++;
198
- hasFiles = true;
199
- }
200
- }
201
- }
202
- } catch (e) {
203
- // 忽略错误
204
- }
205
-
206
- console.log('\n✅ 已保留以下配置和工具:');
207
- console.log(' - settings.json (配置文件)');
208
- console.log(' - commands/ (自定义命令)');
209
- console.log(' - skills/ (技能)');
210
- console.log(' - mcp/ (MCP 服务器)');
211
- console.log(' - projects/ (项目信息)');
212
- console.log(' - ide/ (IDE 配置)');
213
- } else {
214
- console.log('ℹ️ 未找到 ~/.claude 目录');
215
- }
23
+ const cleanedCount = cleanCache();
216
24
 
217
25
  // 备份 .claude.json 文件
26
+ let hasClaudeJson = false;
218
27
  try {
219
28
  if (fs.existsSync(claudeJson)) {
29
+ const timestamp = getLocalTimestamp();
220
30
  const backupJson = `${claudeJson}-backup-${timestamp}`;
221
31
  console.log(`\n📦 备份 ~/.claude.json 到 ${path.basename(backupJson)}`);
222
32
  execSync(`mv "${claudeJson}" "${backupJson}"`);
223
- hasFiles = true;
33
+ hasClaudeJson = true;
224
34
  }
225
35
  } catch (e) {
226
36
  console.log('ℹ️ 未找到 ~/.claude.json 文件');
227
37
  }
228
38
 
229
- if (hasFiles) {
230
- 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} 项)`);
231
43
  console.log('💡 配置和工具已保留,下次启动 Claude Code 可直接使用');
232
44
  console.log(`📁 备份文件保存在 ~/.claude/ 目录下`);
233
45
  } else {
package/bin/ccinstall.js CHANGED
@@ -4,9 +4,12 @@ const readline = require('readline');
4
4
  const path = require('path');
5
5
  const os = require('os');
6
6
  const fs = require('fs');
7
+ const { modifyHostsFile } = require('../lib/hosts');
8
+ const { cleanCache } = require('../lib/cache');
7
9
 
8
10
  const homeDir = os.homedir();
9
11
  const settingsPath = path.join(homeDir, '.claude', 'settings.json');
12
+ const claudeDir = path.join(homeDir, '.claude');
10
13
 
11
14
  console.log('🔧 Claude Code API 配置工具');
12
15
  console.log('🌐 Powered by https://aihezu.dev\n');
@@ -17,8 +20,25 @@ const rl = readline.createInterface({
17
20
  output: process.stdout
18
21
  });
19
22
 
20
- // 提示用户输入 API Key
21
- 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) => {
22
42
  if (!apiKey || apiKey.trim() === '') {
23
43
  console.error('❌ API Key 不能为空');
24
44
  rl.close();
@@ -29,7 +49,6 @@ rl.question('请输入您的 API Key: ', (apiKey) => {
29
49
 
30
50
  try {
31
51
  // 确保 .claude 目录存在
32
- const claudeDir = path.join(homeDir, '.claude');
33
52
  if (!fs.existsSync(claudeDir)) {
34
53
  console.log('📁 创建 ~/.claude 目录...');
35
54
  fs.mkdirSync(claudeDir, { recursive: true });
@@ -64,12 +83,19 @@ rl.question('请输入您的 API Key: ', (apiKey) => {
64
83
  // 写入配置文件
65
84
  fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2), 'utf8');
66
85
 
67
- console.log('\n 配置成功!');
86
+ console.log('\n=== API 配置 ===\n');
87
+ console.log('✅ 配置成功!');
68
88
  console.log('📝 已更新配置文件:', settingsPath);
69
89
  console.log('\n配置内容:');
70
90
  console.log(' ANTHROPIC_AUTH_TOKEN:', apiKey);
71
91
  console.log(' ANTHROPIC_BASE_URL:', 'https://cc.aihezu.dev/api');
72
- console.log('\n💡 您现在可以开始使用 Claude Code 了!');
92
+
93
+ // 修改 hosts 文件
94
+ console.log('\n=== 修改 hosts 文件 ===\n');
95
+ modifyHostsFile();
96
+
97
+ console.log('\n=== 全部完成 ===');
98
+ console.log('💡 您现在可以开始使用 Claude Code 了!');
73
99
  console.log('🌐 更多服务请访问: https://aihezu.dev');
74
100
 
75
101
  } catch (error) {
@@ -78,4 +104,5 @@ rl.question('请输入您的 API Key: ', (apiKey) => {
78
104
  } finally {
79
105
  rl.close();
80
106
  }
81
- });
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/lib/hosts.js ADDED
@@ -0,0 +1,118 @@
1
+ const { execSync } = require('child_process');
2
+ const os = require('os');
3
+ const fs = require('fs');
4
+
5
+ // 生成本地时间戳,格式:YYYYMMDDHHMMSS
6
+ function getLocalTimestamp() {
7
+ const now = new Date();
8
+ const year = now.getFullYear();
9
+ const month = String(now.getMonth() + 1).padStart(2, '0');
10
+ const day = String(now.getDate()).padStart(2, '0');
11
+ const hours = String(now.getHours()).padStart(2, '0');
12
+ const minutes = String(now.getMinutes()).padStart(2, '0');
13
+ const seconds = String(now.getSeconds()).padStart(2, '0');
14
+ return `${year}${month}${day}${hours}${minutes}${seconds}`;
15
+ }
16
+
17
+ // 修改 hosts 文件的函数
18
+ function modifyHostsFile() {
19
+ const isWindows = os.platform() === 'win32';
20
+ const hostsPath = isWindows
21
+ ? 'C:\\Windows\\System32\\drivers\\etc\\hosts'
22
+ : '/etc/hosts';
23
+
24
+ const domains = [
25
+ 'statsig.anthropic.com',
26
+ 'api.anthropic.com'
27
+ ];
28
+
29
+ try {
30
+ console.log('🔧 开始修改 hosts 文件...');
31
+
32
+ // 读取现有 hosts 文件内容
33
+ let hostsContent = '';
34
+ try {
35
+ hostsContent = fs.readFileSync(hostsPath, 'utf8');
36
+ } catch (error) {
37
+ console.error('❌ 无法读取 hosts 文件,请确保以管理员/root权限运行');
38
+ return false;
39
+ }
40
+
41
+ // 备份 hosts 文件
42
+ const timestamp = getLocalTimestamp();
43
+ const hostsBackup = `${hostsPath}.backup-${timestamp}`;
44
+ try {
45
+ fs.writeFileSync(hostsBackup, hostsContent);
46
+ console.log(`📦 已备份 hosts 文件到 ${hostsBackup}`);
47
+ } catch (error) {
48
+ console.error('⚠️ 备份 hosts 文件失败:', error.message);
49
+ }
50
+
51
+ // 移除所有包含目标域名的现有条目(无论指向什么IP)
52
+ const lines = hostsContent.split('\n');
53
+ const filteredLines = lines.filter(line => {
54
+ const trimmed = line.trim();
55
+ // 跳过注释行(但保留其他注释)
56
+ if (trimmed.startsWith('#')) {
57
+ // 如果是之前添加的标记注释,也移除
58
+ if (trimmed.includes('Added by aihezu ccclear tool')) {
59
+ return false;
60
+ }
61
+ return true;
62
+ }
63
+ // 移除任何包含目标域名的行
64
+ return !domains.some(domain => {
65
+ const regex = new RegExp('\\s+' + domain.replace('.', '\\.') + '(\\s|$)', 'i');
66
+ return regex.test(trimmed);
67
+ });
68
+ });
69
+
70
+ // 添加新的条目
71
+ const newEntries = [
72
+ '',
73
+ '# Added by aihezu ccclear tool',
74
+ '127.0.0.1 statsig.anthropic.com',
75
+ '127.0.0.1 api.anthropic.com'
76
+ ];
77
+
78
+ const newHostsContent = filteredLines.join('\n') + '\n' + newEntries.join('\n') + '\n';
79
+
80
+ // 写入 hosts 文件
81
+ try {
82
+ fs.writeFileSync(hostsPath, newHostsContent);
83
+ console.log('✅ hosts 文件修改成功!');
84
+ console.log(' 已添加/更新以下域名解析:');
85
+ console.log(' - statsig.anthropic.com -> 127.0.0.1');
86
+ console.log(' - api.anthropic.com -> 127.0.0.1');
87
+
88
+ // 刷新 DNS 缓存
89
+ try {
90
+ if (isWindows) {
91
+ execSync('ipconfig /flushdns', { stdio: 'ignore' });
92
+ } else if (os.platform() === 'darwin') {
93
+ execSync('sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder', { stdio: 'ignore' });
94
+ } else {
95
+ // Linux
96
+ execSync('sudo systemd-resolve --flush-caches 2>/dev/null || sudo service nscd restart 2>/dev/null || true', { stdio: 'ignore' });
97
+ }
98
+ console.log('🔄 DNS 缓存已刷新');
99
+ } catch (e) {
100
+ console.log('ℹ️ DNS 缓存刷新失败(可能需要手动刷新)');
101
+ }
102
+
103
+ return true;
104
+ } catch (error) {
105
+ console.error('❌ 无法写入 hosts 文件,请确保以管理员/root权限运行');
106
+ console.error(' macOS/Linux: 使用 sudo 运行');
107
+ console.error(' Windows: 以管理员身份运行命令提示符');
108
+ return false;
109
+ }
110
+ } catch (error) {
111
+ console.error('❌ 修改 hosts 文件失败:', error.message);
112
+ return false;
113
+ }
114
+ }
115
+
116
+ module.exports = {
117
+ modifyHostsFile
118
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aihezu",
3
- "version": "1.3.1",
3
+ "version": "1.5.1",
4
4
  "description": "Claude Code CLI 清理工具 - 快速备份和清理 Claude Code 的本地配置和缓存,同时修改 hosts 文件实现本地代理",
5
5
  "main": "bin/ccclear.js",
6
6
  "bin": {