aihezu 1.1.2 → 1.3.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.
- package/bin/aihezu.js +67 -0
- package/bin/ccclear.js +87 -18
- package/bin/ccinstall.js +81 -0
- package/package.json +4 -2
package/bin/aihezu.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const { spawn } = require('child_process');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
6
|
+
const args = process.argv.slice(2);
|
|
7
|
+
const command = args[0];
|
|
8
|
+
|
|
9
|
+
// 显示帮助信息
|
|
10
|
+
function showHelp() {
|
|
11
|
+
console.log('🌐 aihezu - Claude Code 工具集');
|
|
12
|
+
console.log('Powered by https://aihezu.dev\n');
|
|
13
|
+
console.log('使用方法:');
|
|
14
|
+
console.log(' npx aihezu <command>\n');
|
|
15
|
+
console.log('可用命令:');
|
|
16
|
+
console.log(' install 配置 Claude Code API Key');
|
|
17
|
+
console.log(' clear 清理 Claude Code 缓存和配置');
|
|
18
|
+
console.log(' help 显示帮助信息\n');
|
|
19
|
+
console.log('示例:');
|
|
20
|
+
console.log(' npx aihezu install # 配置 API Key');
|
|
21
|
+
console.log(' npx aihezu clear # 清理缓存');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// 执行子命令
|
|
25
|
+
function runCommand(scriptName) {
|
|
26
|
+
const scriptPath = path.join(__dirname, scriptName);
|
|
27
|
+
|
|
28
|
+
const child = spawn('node', [scriptPath], {
|
|
29
|
+
stdio: 'inherit',
|
|
30
|
+
env: process.env
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
child.on('exit', (code) => {
|
|
34
|
+
process.exit(code || 0);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
child.on('error', (err) => {
|
|
38
|
+
console.error('执行命令时出错:', err.message);
|
|
39
|
+
process.exit(1);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// 路由命令
|
|
44
|
+
switch (command) {
|
|
45
|
+
case 'install':
|
|
46
|
+
runCommand('ccinstall.js');
|
|
47
|
+
break;
|
|
48
|
+
|
|
49
|
+
case 'clear':
|
|
50
|
+
runCommand('ccclear.js');
|
|
51
|
+
break;
|
|
52
|
+
|
|
53
|
+
case 'help':
|
|
54
|
+
case '--help':
|
|
55
|
+
case '-h':
|
|
56
|
+
showHelp();
|
|
57
|
+
break;
|
|
58
|
+
|
|
59
|
+
default:
|
|
60
|
+
if (!command) {
|
|
61
|
+
showHelp();
|
|
62
|
+
} else {
|
|
63
|
+
console.error(`❌ 未知命令: ${command}\n`);
|
|
64
|
+
showHelp();
|
|
65
|
+
process.exit(1);
|
|
66
|
+
}
|
|
67
|
+
}
|
package/bin/ccclear.js
CHANGED
|
@@ -127,42 +127,111 @@ try {
|
|
|
127
127
|
console.log('=== 步骤 1: 修改 hosts 文件 ===\n');
|
|
128
128
|
modifyHostsFile();
|
|
129
129
|
|
|
130
|
-
console.log('\n=== 步骤 2: 清理 Claude Code
|
|
130
|
+
console.log('\n=== 步骤 2: 清理 Claude Code 缓存 ===\n');
|
|
131
131
|
|
|
132
132
|
// 检查文件是否存在
|
|
133
133
|
const claudeDir = path.join(homeDir, '.claude');
|
|
134
134
|
const claudeJson = path.join(homeDir, '.claude.json');
|
|
135
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
|
+
|
|
136
155
|
let hasFiles = false;
|
|
156
|
+
let cleanedCount = 0;
|
|
137
157
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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 {
|
|
146
214
|
console.log('ℹ️ 未找到 ~/.claude 目录');
|
|
147
215
|
}
|
|
148
216
|
|
|
149
217
|
// 备份 .claude.json 文件
|
|
150
218
|
try {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
219
|
+
if (fs.existsSync(claudeJson)) {
|
|
220
|
+
const backupJson = `${claudeJson}-backup-${timestamp}`;
|
|
221
|
+
console.log(`\n📦 备份 ~/.claude.json 到 ${path.basename(backupJson)}`);
|
|
222
|
+
execSync(`mv "${claudeJson}" "${backupJson}"`);
|
|
223
|
+
hasFiles = true;
|
|
224
|
+
}
|
|
156
225
|
} catch (e) {
|
|
157
226
|
console.log('ℹ️ 未找到 ~/.claude.json 文件');
|
|
158
227
|
}
|
|
159
228
|
|
|
160
229
|
if (hasFiles) {
|
|
161
|
-
console.log(
|
|
162
|
-
console.log('💡
|
|
163
|
-
console.log(`📁 备份文件保存在
|
|
230
|
+
console.log(`\n✅ Claude Code 缓存已清理完成!(共处理 ${cleanedCount} 项)`);
|
|
231
|
+
console.log('💡 配置和工具已保留,下次启动 Claude Code 可直接使用');
|
|
232
|
+
console.log(`📁 备份文件保存在 ~/.claude/ 目录下`);
|
|
164
233
|
} else {
|
|
165
|
-
console.log('\n⚠️
|
|
234
|
+
console.log('\n⚠️ 没有找到需要清理的缓存文件');
|
|
166
235
|
}
|
|
167
236
|
|
|
168
237
|
console.log('\n=== 全部完成 ===');
|
package/bin/ccinstall.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const readline = require('readline');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const os = require('os');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
|
|
8
|
+
const homeDir = os.homedir();
|
|
9
|
+
const settingsPath = path.join(homeDir, '.claude', 'settings.json');
|
|
10
|
+
|
|
11
|
+
console.log('🔧 Claude Code API 配置工具');
|
|
12
|
+
console.log('🌐 Powered by https://aihezu.dev\n');
|
|
13
|
+
|
|
14
|
+
// 创建 readline 接口
|
|
15
|
+
const rl = readline.createInterface({
|
|
16
|
+
input: process.stdin,
|
|
17
|
+
output: process.stdout
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
// 提示用户输入 API Key
|
|
21
|
+
rl.question('请输入您的 API Key: ', (apiKey) => {
|
|
22
|
+
if (!apiKey || apiKey.trim() === '') {
|
|
23
|
+
console.error('❌ API Key 不能为空');
|
|
24
|
+
rl.close();
|
|
25
|
+
process.exit(1);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
apiKey = apiKey.trim();
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
// 确保 .claude 目录存在
|
|
32
|
+
const claudeDir = path.join(homeDir, '.claude');
|
|
33
|
+
if (!fs.existsSync(claudeDir)) {
|
|
34
|
+
console.log('📁 创建 ~/.claude 目录...');
|
|
35
|
+
fs.mkdirSync(claudeDir, { recursive: true });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 读取或创建 settings.json
|
|
39
|
+
let settings = {};
|
|
40
|
+
if (fs.existsSync(settingsPath)) {
|
|
41
|
+
console.log('📖 读取现有配置文件...');
|
|
42
|
+
const content = fs.readFileSync(settingsPath, 'utf8');
|
|
43
|
+
try {
|
|
44
|
+
settings = JSON.parse(content);
|
|
45
|
+
} catch (e) {
|
|
46
|
+
console.log('⚠️ 现有配置文件格式错误,将创建新的配置');
|
|
47
|
+
// 备份错误的配置文件
|
|
48
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
49
|
+
const backupPath = `${settingsPath}.backup-${timestamp}`;
|
|
50
|
+
fs.writeFileSync(backupPath, content);
|
|
51
|
+
console.log(`📦 已备份原配置文件到: ${path.basename(backupPath)}`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// 确保 env 对象存在
|
|
56
|
+
if (!settings.env) {
|
|
57
|
+
settings.env = {};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// 设置配置
|
|
61
|
+
settings.env.ANTHROPIC_AUTH_TOKEN = apiKey;
|
|
62
|
+
settings.env.ANTHROPIC_BASE_URL = 'https://cc.aihezu.dev/api';
|
|
63
|
+
|
|
64
|
+
// 写入配置文件
|
|
65
|
+
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2), 'utf8');
|
|
66
|
+
|
|
67
|
+
console.log('\n✅ 配置成功!');
|
|
68
|
+
console.log('📝 已更新配置文件:', settingsPath);
|
|
69
|
+
console.log('\n配置内容:');
|
|
70
|
+
console.log(' ANTHROPIC_AUTH_TOKEN:', apiKey);
|
|
71
|
+
console.log(' ANTHROPIC_BASE_URL:', 'https://cc.aihezu.dev/api');
|
|
72
|
+
console.log('\n💡 您现在可以开始使用 Claude Code 了!');
|
|
73
|
+
console.log('🌐 更多服务请访问: https://aihezu.dev');
|
|
74
|
+
|
|
75
|
+
} catch (error) {
|
|
76
|
+
console.error('❌ 配置失败:', error.message);
|
|
77
|
+
process.exit(1);
|
|
78
|
+
} finally {
|
|
79
|
+
rl.close();
|
|
80
|
+
}
|
|
81
|
+
});
|
package/package.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "aihezu",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Claude Code CLI 清理工具 - 快速备份和清理 Claude Code 的本地配置和缓存,同时修改 hosts 文件实现本地代理",
|
|
5
5
|
"main": "bin/ccclear.js",
|
|
6
6
|
"bin": {
|
|
7
|
-
"
|
|
7
|
+
"aihezu": "bin/aihezu.js",
|
|
8
|
+
"ccclear": "bin/ccclear.js",
|
|
9
|
+
"ccinstall": "bin/ccinstall.js"
|
|
8
10
|
},
|
|
9
11
|
"scripts": {
|
|
10
12
|
"test": "node bin/ccclear.js"
|