-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.js
More file actions
149 lines (128 loc) · 5.16 KB
/
install.js
File metadata and controls
149 lines (128 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env node
// CodeDungeon Installer
// Sets up hooks, statusLine, and spinner in Claude Code settings
const fs = require('fs');
const path = require('path');
const os = require('os');
const GAME_DIR = __dirname;
const CLAUDE_DIR = path.join(os.homedir(), '.claude');
const SETTINGS_FILE = path.join(CLAUDE_DIR, 'settings.json');
const BACKUP_FILE = path.join(GAME_DIR, 'settings-backup.json');
// Determine paths for bash context (Git Bash on Windows)
function toBashPath(winPath) {
// Convert D:\foo\bar to /d/foo/bar
return winPath.replace(/\\/g, '/').replace(/^([A-Za-z]):/, (_, letter) => '/' + letter.toLowerCase());
}
function install() {
console.log('=== CodeDungeon 安装 ===\n');
// Read current settings
let settings;
try {
settings = JSON.parse(fs.readFileSync(SETTINGS_FILE, 'utf8'));
} catch {
console.error('无法读取 settings.json:', SETTINGS_FILE);
process.exit(1);
}
// Backup current settings
fs.writeFileSync(BACKUP_FILE, JSON.stringify(settings, null, 2));
console.log('✅ 已备份当前 settings.json →', BACKUP_FILE);
const bashGameDir = toBashPath(GAME_DIR);
const nodePath = 'node'; // Assume node is on PATH
// 1. Configure hooks
if (!settings.hooks) settings.hooks = {};
if (!settings.hooks.PostToolUse) settings.hooks.PostToolUse = [];
// Remove any existing CodeDungeon hooks
settings.hooks.PostToolUse = settings.hooks.PostToolUse.filter(h => {
const hookCmd = h.hooks && h.hooks[0] && h.hooks[0].command;
return !hookCmd || !hookCmd.includes('CodeDungeon');
});
// Add game hook
settings.hooks.PostToolUse.push({
matcher: 'Edit|Write|Bash|Read|Glob|Grep',
hooks: [{
type: 'command',
command: `${nodePath} "${bashGameDir}/engine.js" tick`,
timeout: 5
}]
});
console.log('✅ 已配置 PostToolUse hook');
// 2. Configure StatusLine
// Save original statusLine for restoration (only if not already a CodeDungeon config)
if (settings.statusLine && !(settings.statusLine.command || '').includes('CodeDungeon')) {
fs.writeFileSync(path.join(GAME_DIR, 'original-statusline.json'), JSON.stringify(settings.statusLine));
}
settings.statusLine = {
type: 'command',
command: `${nodePath} "${bashGameDir}/hud.js"`
};
console.log('✅ 已配置 StatusLine → hud.js');
// 3. Configure spinner
const spinnerData = JSON.parse(fs.readFileSync(path.join(GAME_DIR, 'data', 'spinner.json'), 'utf8'));
// Save original spinner (only if not already dungeon-themed)
if (settings.spinnerVerbs && !(settings.spinnerVerbs.verbs || []).includes('小心翼翼前进中')) {
fs.writeFileSync(path.join(GAME_DIR, 'original-spinner.json'), JSON.stringify(settings.spinnerVerbs));
}
settings.spinnerVerbs = {
mode: 'replace',
verbs: spinnerData.explore
};
console.log('✅ 已配置地牢主题 Spinner');
// Save settings
fs.writeFileSync(SETTINGS_FILE, JSON.stringify(settings, null, 2));
console.log('\n✅ 安装完成!请重启 Claude Code 使 hooks 和 spinner 生效。');
console.log('\n游戏命令:');
console.log(` ! node "${bashGameDir}/engine.js" action status 查看状态`);
console.log(` ! node "${bashGameDir}/engine.js" action fight 战斗`);
console.log(` ! node "${bashGameDir}/engine.js" action bag 背包`);
console.log('\n或创建快捷方式(推荐):');
console.log(` 将以下行加入 ~/.bashrc:`);
console.log(` alias g='node "${bashGameDir}/engine.js" action'`);
console.log(' 然后就可以用 !g fight / !g status 等命令了');
}
function uninstall() {
console.log('=== CodeDungeon 卸载 ===\n');
let settings;
try {
settings = JSON.parse(fs.readFileSync(SETTINGS_FILE, 'utf8'));
} catch {
console.error('无法读取 settings.json');
process.exit(1);
}
// 1. Remove hooks
if (settings.hooks && settings.hooks.PostToolUse) {
settings.hooks.PostToolUse = settings.hooks.PostToolUse.filter(h => {
const hookCmd = h.hooks && h.hooks[0] && h.hooks[0].command;
return !hookCmd || !hookCmd.includes('CodeDungeon');
});
if (settings.hooks.PostToolUse.length === 0) delete settings.hooks.PostToolUse;
if (Object.keys(settings.hooks).length === 0) delete settings.hooks;
}
console.log('✅ 已移除 hooks');
// 2. Restore StatusLine
try {
const original = JSON.parse(fs.readFileSync(path.join(GAME_DIR, 'original-statusline.json'), 'utf8'));
settings.statusLine = original;
console.log('✅ 已恢复原始 StatusLine');
} catch {
delete settings.statusLine;
console.log('⚠️ 未找到原始 StatusLine 备份,已清除');
}
// 3. Restore spinner
try {
const original = JSON.parse(fs.readFileSync(path.join(GAME_DIR, 'original-spinner.json'), 'utf8'));
settings.spinnerVerbs = original;
console.log('✅ 已恢复原始 Spinner');
} catch {
delete settings.spinnerVerbs;
console.log('⚠️ 未找到原始 Spinner 备份,已清除');
}
fs.writeFileSync(SETTINGS_FILE, JSON.stringify(settings, null, 2));
console.log('\n✅ 卸载完成!请重启 Claude Code 使更改生效。');
}
// Main
const cmd = process.argv[2];
if (cmd === 'uninstall') {
uninstall();
} else {
install();
}