-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmanage_hooks.mjs
More file actions
428 lines (383 loc) · 12.7 KB
/
manage_hooks.mjs
File metadata and controls
428 lines (383 loc) · 12.7 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env node
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
const CLAUDE_EVENTS = [
{ configName: 'SessionStart', arg: 'session_start', matcher: false },
{ configName: 'SessionEnd', arg: 'session_end', matcher: false },
{ configName: 'UserPromptSubmit', arg: 'user_prompt_submit', matcher: false },
{ configName: 'PreToolUse', arg: 'pre_tool_use', matcher: true },
{ configName: 'PostToolUse', arg: 'post_tool_use', matcher: true },
{ configName: 'SubagentStart', arg: 'subagent_start', matcher: true },
{ configName: 'SubagentStop', arg: 'subagent_stop', matcher: true },
{ configName: 'Stop', arg: 'stop', matcher: false },
];
const CURSOR_EVENTS = [
{ configName: 'sessionStart', arg: 'session_start' },
{ configName: 'sessionEnd', arg: 'session_end' },
{ configName: 'beforeSubmitPrompt', arg: 'user_prompt_submit' },
{ configName: 'preToolUse', arg: 'pre_tool_use' },
{ configName: 'postToolUse', arg: 'post_tool_use' },
{ configName: 'subagentStart', arg: 'subagent_start' },
{ configName: 'subagentStop', arg: 'subagent_stop' },
{ configName: 'stop', arg: 'stop' },
];
function parseArgs(argv) {
const args = argv.slice(2);
const command = args.find((value) => !value.startsWith('--')) || 'status';
const getFlag = (name, fallback) => {
const prefixed = `--${name}=`;
const entry = args.find((value) => value.startsWith(prefixed));
return entry ? entry.slice(prefixed.length) : fallback;
};
const targetRaw = getFlag('target', 'claude').trim().toLowerCase();
const target = targetRaw === 'cursor' ? 'cursor' : 'claude';
const defaultSettingsPath =
target === 'cursor'
? path.join(os.homedir(), '.cursor', 'hooks.json')
: path.join(os.homedir(), '.claude', 'settings.json');
return {
command,
target,
repoRoot: path.resolve(getFlag('repo-root', process.cwd())),
settingsPath: path.resolve(getFlag('settings-path', defaultSettingsPath)),
};
}
function ensureSettings(settingsPath, target) {
if (!fs.existsSync(settingsPath)) {
fs.mkdirSync(path.dirname(settingsPath), { recursive: true });
const initial = target === 'cursor' ? { version: 1, hooks: {} } : { hooks: {} };
fs.writeFileSync(settingsPath, `${JSON.stringify(initial, null, 2)}\n`, 'utf8');
return initial;
}
const raw = fs.readFileSync(settingsPath, 'utf8');
const parsed = raw.trim() ? JSON.parse(raw) : {};
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
throw new Error(`Invalid JSON object in ${settingsPath}`);
}
if (!parsed.hooks || typeof parsed.hooks !== 'object' || Array.isArray(parsed.hooks)) {
parsed.hooks = {};
}
if (target === 'cursor' && typeof parsed.version !== 'number') {
parsed.version = 1;
}
return parsed;
}
function backupSettings(settingsPath) {
const stamp = new Date().toISOString().replace(/[:.]/g, '-');
const backupPath = `${settingsPath}.habbo-agent-platform-backup-${stamp}`;
fs.copyFileSync(settingsPath, backupPath);
return backupPath;
}
function hookCommand(hookRunnerPath, arg) {
return `bash "${hookRunnerPath}" ${arg}`;
}
function isHabboPlatformCommand(command) {
return typeof command === 'string' && command.includes('habbo-agent-platform-hook.sh');
}
function isLegacyHabboCommand(command, arg) {
return (
typeof command === 'string' &&
command.includes('habboAgentHook.ts') &&
command.toLowerCase().includes(arg.toLowerCase())
);
}
function ensureExecutable(filePath) {
const mode = fs.statSync(filePath).mode;
const executableBits = 0o111;
if ((mode & executableBits) === executableBits) {
return;
}
fs.chmodSync(filePath, mode | executableBits);
}
function saveSettings(settingsPath, settings) {
fs.writeFileSync(settingsPath, `${JSON.stringify(settings, null, 2)}\n`, 'utf8');
}
function resolveHooksDir(repoRoot) {
const candidates = [path.resolve(repoRoot, 'hooks'), path.resolve(repoRoot)];
for (const dir of candidates) {
const runner = path.resolve(dir, 'habbo-agent-platform-hook.sh');
if (fs.existsSync(runner)) {
return dir;
}
}
throw new Error(
`Unable to locate habbo-agent-platform-hook.sh from repo root: ${repoRoot}.`
);
}
function findClaudeBlock(blocks, needsMatcher) {
if (!Array.isArray(blocks)) {
return null;
}
if (needsMatcher) {
return blocks.find((block) => block && typeof block === 'object' && block.matcher === '.*') || null;
}
return blocks.find((block) => block && typeof block === 'object') || null;
}
function ensureClaudeHooksArray(block) {
if (!block.hooks || !Array.isArray(block.hooks)) {
block.hooks = [];
}
}
function installClaude(settings, hookRunnerPath) {
let changed = false;
for (const spec of CLAUDE_EVENTS) {
if (!Array.isArray(settings.hooks[spec.configName])) {
settings.hooks[spec.configName] = [];
changed = true;
}
const blocks = settings.hooks[spec.configName];
let block = findClaudeBlock(blocks, spec.matcher);
if (!block) {
block = spec.matcher ? { matcher: '.*', hooks: [] } : { hooks: [] };
blocks.push(block);
changed = true;
}
ensureClaudeHooksArray(block);
const desired = hookCommand(hookRunnerPath, spec.arg);
let hasDesired = false;
for (const hook of block.hooks) {
if (!hook || typeof hook !== 'object') {
continue;
}
if (hook.command === desired) {
hasDesired = true;
}
if (isLegacyHabboCommand(hook.command, spec.arg) || isHabboPlatformCommand(hook.command)) {
if (hook.command !== desired || hook.timeout !== 8 || hook.type !== 'command') {
hook.type = 'command';
hook.command = desired;
hook.timeout = 8;
changed = true;
}
hasDesired = true;
}
}
if (!hasDesired) {
block.hooks.push({
type: 'command',
command: desired,
timeout: 8,
});
changed = true;
}
}
return changed;
}
function uninstallClaude(settings) {
let changed = false;
if (!settings.hooks || typeof settings.hooks !== 'object' || Array.isArray(settings.hooks)) {
return false;
}
for (const spec of CLAUDE_EVENTS) {
const blocks = settings.hooks[spec.configName];
if (!Array.isArray(blocks)) {
continue;
}
for (const block of blocks) {
if (!block || typeof block !== 'object' || !Array.isArray(block.hooks)) {
continue;
}
const before = block.hooks.length;
block.hooks = block.hooks.filter((hook) => {
if (!hook || typeof hook !== 'object') {
return true;
}
const command = hook.command;
if (isHabboPlatformCommand(command)) {
return false;
}
if (isLegacyHabboCommand(command, spec.arg)) {
return false;
}
return true;
});
if (block.hooks.length !== before) {
changed = true;
}
}
}
return changed;
}
function statusClaude(settings) {
const byEvent = {};
let installedCount = 0;
for (const spec of CLAUDE_EVENTS) {
const blocks = settings.hooks?.[spec.configName];
let found = false;
if (Array.isArray(blocks)) {
for (const block of blocks) {
if (!block || typeof block !== 'object' || !Array.isArray(block.hooks)) {
continue;
}
for (const hook of block.hooks) {
if (!hook || typeof hook !== 'object') {
continue;
}
if (isHabboPlatformCommand(hook.command) || isLegacyHabboCommand(hook.command, spec.arg)) {
found = true;
}
}
}
}
byEvent[spec.configName] = found;
if (found) {
installedCount += 1;
}
}
return {
installed: installedCount === CLAUDE_EVENTS.length,
installedCount,
total: CLAUDE_EVENTS.length,
byEvent,
};
}
function installCursor(settings, hookRunnerPath) {
let changed = false;
for (const spec of CURSOR_EVENTS) {
if (!Array.isArray(settings.hooks[spec.configName])) {
settings.hooks[spec.configName] = [];
changed = true;
}
const hooks = settings.hooks[spec.configName];
const desired = hookCommand(hookRunnerPath, spec.arg);
let hasDesired = false;
for (const hook of hooks) {
if (!hook || typeof hook !== 'object') {
continue;
}
if (hook.command === desired) {
hasDesired = true;
}
if (isLegacyHabboCommand(hook.command, spec.arg) || isHabboPlatformCommand(hook.command)) {
if (hook.command !== desired || hook.timeout !== 8) {
hook.command = desired;
hook.timeout = 8;
changed = true;
}
hasDesired = true;
}
}
if (!hasDesired) {
hooks.push({
command: desired,
timeout: 8,
});
changed = true;
}
}
return changed;
}
function uninstallCursor(settings) {
let changed = false;
if (!settings.hooks || typeof settings.hooks !== 'object' || Array.isArray(settings.hooks)) {
return false;
}
for (const spec of CURSOR_EVENTS) {
const hooks = settings.hooks[spec.configName];
if (!Array.isArray(hooks)) {
continue;
}
const before = hooks.length;
settings.hooks[spec.configName] = hooks.filter((hook) => {
if (!hook || typeof hook !== 'object') {
return true;
}
const command = hook.command;
if (isHabboPlatformCommand(command)) {
return false;
}
if (isLegacyHabboCommand(command, spec.arg)) {
return false;
}
return true;
});
if (settings.hooks[spec.configName].length !== before) {
changed = true;
}
}
return changed;
}
function statusCursor(settings) {
const byEvent = {};
let installedCount = 0;
for (const spec of CURSOR_EVENTS) {
const hooks = settings.hooks?.[spec.configName];
let found = false;
if (Array.isArray(hooks)) {
for (const hook of hooks) {
if (!hook || typeof hook !== 'object') {
continue;
}
if (isHabboPlatformCommand(hook.command) || isLegacyHabboCommand(hook.command, spec.arg)) {
found = true;
}
}
}
byEvent[spec.configName] = found;
if (found) {
installedCount += 1;
}
}
return {
installed: installedCount === CURSOR_EVENTS.length,
installedCount,
total: CURSOR_EVENTS.length,
byEvent,
};
}
function printStatus(result, settingsPath, hookRunnerPath, target) {
console.log(`target: ${target}`);
console.log(`hooks installed: ${result.installed ? 'yes' : 'no'}`);
console.log(`events configured: ${result.installedCount}/${result.total}`);
console.log(`settings file: ${settingsPath}`);
console.log(`hook runner: ${hookRunnerPath}`);
console.log('');
for (const [eventName, installed] of Object.entries(result.byEvent)) {
console.log(`- ${eventName}: ${installed ? 'ok' : 'missing'}`);
}
}
function main() {
const { command, target, repoRoot, settingsPath } = parseArgs(process.argv);
const scriptDir = resolveHooksDir(repoRoot);
const hookRunnerPath = path.resolve(scriptDir, 'habbo-agent-platform-hook.sh');
const settings = ensureSettings(settingsPath, target);
if (command === 'status') {
const result = target === 'cursor' ? statusCursor(settings) : statusClaude(settings);
printStatus(result, settingsPath, hookRunnerPath, target);
process.exit(0);
}
if (command === 'install') {
ensureExecutable(hookRunnerPath);
const changed = target === 'cursor' ? installCursor(settings, hookRunnerPath) : installClaude(settings, hookRunnerPath);
if (!changed) {
console.log(`No changes needed. ${target} hooks are already installed.`);
process.exit(0);
}
const backupPath = backupSettings(settingsPath);
saveSettings(settingsPath, settings);
console.log(`Installed habbo-agent-platform hooks into ${settingsPath} (${target})`);
console.log(`Backup written to ${backupPath}`);
console.log('Restart your IDE/assistant client to apply hook changes.');
process.exit(0);
}
if (command === 'uninstall') {
const changed = target === 'cursor' ? uninstallCursor(settings) : uninstallClaude(settings);
if (!changed) {
console.log(`No habbo-agent-platform hooks found to remove for target: ${target}.`);
process.exit(0);
}
const backupPath = backupSettings(settingsPath);
saveSettings(settingsPath, settings);
console.log(`Removed habbo-agent-platform hooks from ${settingsPath} (${target})`);
console.log(`Backup written to ${backupPath}`);
console.log('Restart your IDE/assistant client to apply hook changes.');
process.exit(0);
}
console.error(`Unknown command: ${command}`);
console.error(
'Usage: node hooks/manage_hooks.mjs <install|uninstall|status> [--target=claude|cursor] [--repo-root=PATH] [--settings-path=PATH]'
);
process.exit(1);
}
main();