aihezu 1.0.0 → 1.1.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.
Files changed (3) hide show
  1. package/README.md +69 -13
  2. package/bin/ccclear.js +109 -3
  3. package/package.json +4 -4
package/README.md CHANGED
@@ -1,74 +1,130 @@
1
1
  # aihezu - Claude Code CLI 清理工具
2
2
 
3
- 快速备份和清理 Claude Code CLI 的本地配置和缓存文件。
3
+ 快速备份和清理 Claude Code CLI 的本地配置和缓存文件,同时修改 hosts 文件实现本地代理。
4
4
 
5
5
  ## 功能特性
6
6
 
7
7
  - 🧹 一键清理 Claude Code CLI 的所有本地数据
8
- - 📦 自动备份配置文件(带时间戳)
8
+ - 🌐 自动修改 hosts 文件,将 Anthropic 域名指向本地
9
+ - 📦 自动备份配置文件和 hosts 文件(带时间戳)
9
10
  - ✅ 安全可靠,不会丢失数据
10
11
  - 🚀 支持 npx 直接运行,无需安装
12
+ - 🔄 自动刷新 DNS 缓存
11
13
 
12
14
  ## 清理内容
13
15
 
14
- 该工具会备份并移动以下文件/目录:
16
+ 该工具会执行以下操作:
15
17
 
18
+ ### 1. 修改 hosts 文件
19
+ - 将 `statsig.anthropic.com` 指向 `127.0.0.1`
20
+ - 将 `api.anthropic.com` 指向 `127.0.0.1`
21
+ - 如果已存在这些域名的解析,会先移除旧记录再添加新记录
22
+ - 自动备份 hosts 文件(带时间戳)
23
+ - 自动刷新 DNS 缓存
24
+
25
+ ### 2. 清理 Claude Code 配置
16
26
  - `~/.claude/` - Claude Code 主数据目录(包括缓存、历史、项目等)
17
27
  - `~/.claude.json` - Claude Code 全局配置文件
18
28
 
19
29
  ## 使用方法
20
30
 
21
- ### 方式一:使用 npx(推荐)
31
+ ⚠️ **重要提示**:由于需要修改系统 hosts 文件,必须使用管理员权限运行!
32
+
33
+ ### macOS/Linux
22
34
 
23
- 无需安装,直接运行:
35
+ #### 方式一:使用 npx(推荐)
24
36
 
25
37
  ```bash
26
- npx aihezu ccclear
38
+ sudo npx aihezu ccclear
27
39
  ```
28
40
 
29
- ### 方式二:全局安装
41
+ #### 方式二:全局安装后运行
30
42
 
31
43
  ```bash
32
44
  npm install -g aihezu
33
- ccclear
45
+ sudo ccclear
46
+ ```
47
+
48
+ ### Windows
49
+
50
+ 需要以管理员身份运行命令提示符或 PowerShell:
51
+
52
+ ```bash
53
+ npx aihezu ccclear
34
54
  ```
35
55
 
36
56
  ## 运行效果
37
57
 
38
58
  ```
39
59
  🧹 Claude Code CLI 清理工具
60
+ 🌐 Powered by https://aihezu.dev
61
+
62
+ === 步骤 1: 修改 hosts 文件 ===
63
+
64
+ 🔧 开始修改 hosts 文件...
65
+ 📦 已备份 hosts 文件到 /etc/hosts.backup-20250113120000
66
+ ✅ hosts 文件修改成功!
67
+ 已添加/更新以下域名解析:
68
+ - statsig.anthropic.com -> 127.0.0.1
69
+ - api.anthropic.com -> 127.0.0.1
70
+ 🔄 DNS 缓存已刷新
40
71
 
41
- 📦 备份 ~/.claude .claude-20250112150030
42
- 📦 备份 ~/.claude.json 到 .claude.json-20250112150030
72
+ === 步骤 2: 清理 Claude Code 配置 ===
73
+
74
+ 📦 备份 ~/.claude 到 .claude-20250113120000
75
+ 📦 备份 ~/.claude.json 到 .claude.json-20250113120000
43
76
 
44
77
  ✅ Claude Code CLI 配置和缓存已清理完成!
45
78
  💡 下次启动 Claude Code 时将重新初始化
46
79
  📁 备份文件保存在 /Users/yourname/ 目录下
80
+
81
+ === 全部完成 ===
82
+ 更多服务请访问 AI 合租官网:https://aihezu.dev
47
83
  ```
48
84
 
49
85
  ## 恢复备份
50
86
 
87
+ ### 恢复 Claude Code 配置
88
+
51
89
  如需恢复之前的配置,只需将备份文件重命名:
52
90
 
53
91
  ```bash
54
92
  # 恢复目录
55
- mv ~/.claude-20250112150030 ~/.claude
93
+ mv ~/.claude-20250113120000 ~/.claude
56
94
 
57
95
  # 恢复配置文件
58
- mv ~/.claude.json-20250112150030 ~/.claude.json
96
+ mv ~/.claude.json-20250113120000 ~/.claude.json
97
+ ```
98
+
99
+ ### 恢复 hosts 文件
100
+
101
+ ```bash
102
+ # macOS/Linux
103
+ sudo mv /etc/hosts.backup-20250113120000 /etc/hosts
104
+
105
+ # Windows (以管理员身份运行)
106
+ move C:\Windows\System32\drivers\etc\hosts.backup-20250113120000 C:\Windows\System32\drivers\etc\hosts
59
107
  ```
60
108
 
61
109
  ## 使用场景
62
110
 
63
111
  - Claude Code CLI 出现异常需要重置
112
+ - 需要通过本地代理使用 Claude API
64
113
  - 清理大量累积的缓存和日志
65
114
  - 切换不同的配置环境
66
115
  - 重新初始化 Claude Code CLI
67
116
 
117
+ ## 工作原理
118
+
119
+ 1. **hosts 文件修改**:将 Anthropic 相关域名指向本地 127.0.0.1,配合本地代理使用
120
+ 2. **配置清理**:备份并清理 Claude Code 的配置和缓存
121
+ 3. **安全备份**:所有修改前都会创建带时间戳的备份文件
122
+
68
123
  ## 系统要求
69
124
 
70
125
  - Node.js >= 14.0.0
71
- - macOS / Linux / Windows (with WSL)
126
+ - macOS / Linux / Windows
127
+ - 需要管理员/root 权限(用于修改 hosts 文件)
72
128
 
73
129
  ## 许可证
74
130
 
package/bin/ccclear.js CHANGED
@@ -3,6 +3,7 @@
3
3
  const { execSync } = require('child_process');
4
4
  const path = require('path');
5
5
  const os = require('os');
6
+ const fs = require('fs');
6
7
 
7
8
  const homeDir = os.homedir();
8
9
  const timestamp = new Date().toISOString().replace(/[-:]/g, '').replace(/\..+/, '').replace('T', '');
@@ -10,7 +11,111 @@ const timestamp = new Date().toISOString().replace(/[-:]/g, '').replace(/\..+/,
10
11
  console.log('🧹 Claude Code CLI 清理工具');
11
12
  console.log('🌐 Powered by https://aihezu.dev\n');
12
13
 
14
+ // 修改 hosts 文件的函数
15
+ function modifyHostsFile() {
16
+ const isWindows = os.platform() === 'win32';
17
+ const hostsPath = isWindows
18
+ ? 'C:\\Windows\\System32\\drivers\\etc\\hosts'
19
+ : '/etc/hosts';
20
+
21
+ const domains = [
22
+ 'statsig.anthropic.com',
23
+ 'api.anthropic.com'
24
+ ];
25
+
26
+ try {
27
+ console.log('🔧 开始修改 hosts 文件...');
28
+
29
+ // 读取现有 hosts 文件内容
30
+ let hostsContent = '';
31
+ try {
32
+ hostsContent = fs.readFileSync(hostsPath, 'utf8');
33
+ } catch (error) {
34
+ console.error('❌ 无法读取 hosts 文件,请确保以管理员/root权限运行');
35
+ return false;
36
+ }
37
+
38
+ // 备份 hosts 文件
39
+ const hostsBackup = `${hostsPath}.backup-${timestamp}`;
40
+ try {
41
+ fs.writeFileSync(hostsBackup, hostsContent);
42
+ console.log(`📦 已备份 hosts 文件到 ${hostsBackup}`);
43
+ } catch (error) {
44
+ console.error('⚠️ 备份 hosts 文件失败:', error.message);
45
+ }
46
+
47
+ // 移除所有包含目标域名的现有条目(无论指向什么IP)
48
+ const lines = hostsContent.split('\n');
49
+ const filteredLines = lines.filter(line => {
50
+ const trimmed = line.trim();
51
+ // 跳过注释行(但保留其他注释)
52
+ if (trimmed.startsWith('#')) {
53
+ // 如果是之前添加的标记注释,也移除
54
+ if (trimmed.includes('Added by aihezu ccclear tool')) {
55
+ return false;
56
+ }
57
+ return true;
58
+ }
59
+ // 移除任何包含目标域名的行
60
+ return !domains.some(domain => {
61
+ const regex = new RegExp('\\s+' + domain.replace('.', '\\.') + '(\\s|$)', 'i');
62
+ return regex.test(trimmed);
63
+ });
64
+ });
65
+
66
+ // 添加新的条目
67
+ const newEntries = [
68
+ '',
69
+ '# Added by aihezu ccclear tool',
70
+ '127.0.0.1 statsig.anthropic.com',
71
+ '127.0.0.1 api.anthropic.com'
72
+ ];
73
+
74
+ const newHostsContent = filteredLines.join('\n') + '\n' + newEntries.join('\n') + '\n';
75
+
76
+ // 写入 hosts 文件
77
+ try {
78
+ fs.writeFileSync(hostsPath, newHostsContent);
79
+ console.log('✅ hosts 文件修改成功!');
80
+ console.log(' 已添加/更新以下域名解析:');
81
+ console.log(' - statsig.anthropic.com -> 127.0.0.1');
82
+ console.log(' - api.anthropic.com -> 127.0.0.1');
83
+
84
+ // 刷新 DNS 缓存
85
+ try {
86
+ if (isWindows) {
87
+ execSync('ipconfig /flushdns', { stdio: 'ignore' });
88
+ } else if (os.platform() === 'darwin') {
89
+ execSync('sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder', { stdio: 'ignore' });
90
+ } else {
91
+ // Linux
92
+ execSync('sudo systemd-resolve --flush-caches 2>/dev/null || sudo service nscd restart 2>/dev/null || true', { stdio: 'ignore' });
93
+ }
94
+ console.log('🔄 DNS 缓存已刷新');
95
+ } catch (e) {
96
+ console.log('ℹ️ DNS 缓存刷新失败(可能需要手动刷新)');
97
+ }
98
+
99
+ return true;
100
+ } catch (error) {
101
+ console.error('❌ 无法写入 hosts 文件,请确保以管理员/root权限运行');
102
+ console.error(' macOS/Linux: 使用 sudo 运行');
103
+ console.error(' Windows: 以管理员身份运行命令提示符');
104
+ return false;
105
+ }
106
+ } catch (error) {
107
+ console.error('❌ 修改 hosts 文件失败:', error.message);
108
+ return false;
109
+ }
110
+ }
111
+
13
112
  try {
113
+ // 首先修改 hosts 文件
114
+ console.log('=== 步骤 1: 修改 hosts 文件 ===\n');
115
+ modifyHostsFile();
116
+
117
+ console.log('\n=== 步骤 2: 清理 Claude Code 配置 ===\n');
118
+
14
119
  // 检查文件是否存在
15
120
  const claudeDir = path.join(homeDir, '.claude');
16
121
  const claudeJson = path.join(homeDir, '.claude.json');
@@ -43,13 +148,14 @@ try {
43
148
  console.log('\n✅ Claude Code CLI 配置和缓存已清理完成!');
44
149
  console.log('💡 下次启动 Claude Code 时将重新初始化');
45
150
  console.log(`📁 备份文件保存在 ${homeDir}/ 目录下`);
46
- console.log('\n更多服务请访问 AI 合租官网:https://aihezu.dev');
47
151
  } else {
48
152
  console.log('\n⚠️ 没有找到需要清理的文件');
49
- console.log('\n更多服务请访问 AI 合租官网:https://aihezu.dev');
50
153
  }
51
154
 
155
+ console.log('\n=== 全部完成 ===');
156
+ console.log('更多服务请访问 AI 合租官网:https://aihezu.dev');
157
+
52
158
  } catch (error) {
53
- console.error('❌ 清理失败:', error.message);
159
+ console.error('❌ 操作失败:', error.message);
54
160
  process.exit(1);
55
161
  }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "aihezu",
3
- "version": "1.0.0",
4
- "description": "Claude Code CLI 清理工具 - 快速备份和清理 Claude Code 的本地配置和缓存",
3
+ "version": "1.1.1",
4
+ "description": "Claude Code CLI 清理工具 - 快速备份和清理 Claude Code 的本地配置和缓存,同时修改 hosts 文件实现本地代理",
5
5
  "main": "bin/ccclear.js",
6
6
  "bin": {
7
- "ccclear": "./bin/ccclear.js"
7
+ "ccclear": "bin/ccclear.js"
8
8
  },
9
9
  "scripts": {
10
10
  "test": "node bin/ccclear.js"
@@ -25,7 +25,7 @@
25
25
  },
26
26
  "repository": {
27
27
  "type": "git",
28
- "url": "https://github.com/aihezu/npm-ccclear.git"
28
+ "url": "git+https://github.com/aihezu/npm-ccclear.git"
29
29
  },
30
30
  "bugs": {
31
31
  "url": "https://github.com/aihezu/npm-ccclear/issues"