Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/claude-hook-path-independence.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@colony/installers": patch
---

Preserve existing Claude Code hooks while normalizing Colony hooks to absolute Node commands.
59 changes: 47 additions & 12 deletions packages/installers/src/claude-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import type { InstallContext, Installer } from './types.js';
interface ClaudeSettings {
hooks?: Record<
string,
Array<{ matcher?: string; hooks: Array<{ type: string; command: string }> }>
Array<{
matcher?: string;
hooks: Array<{ type: string; command: string; [key: string]: unknown }>;
}>
>;
mcpServers?: Record<string, { command: string; args?: string[]; env?: Record<string, string> }>;
}
Expand All @@ -24,6 +27,42 @@ function settingsFile(): string {
return join(homedir(), '.claude', 'settings.json');
}

function isColonyHookCommand(command: string, hookId: string): boolean {
const normalized = command.replace(/["']/g, ' ').replace(/\s+/g, ' ').trim();
return /\bcolony(?:\.js)?\b/.test(normalized) && normalized.includes(` hook run ${hookId}`);
}

function installColonyHook(
existing: NonNullable<ClaudeSettings['hooks']>[string] | undefined,
command: string,
hookId: string,
): NonNullable<ClaudeSettings['hooks']>[string] {
const filtered = removeColonyHook(existing, hookId);
return [
...filtered,
{
hooks: [
{
type: 'command',
command,
},
],
},
];
}

function removeColonyHook(
existing: NonNullable<ClaudeSettings['hooks']>[string] | undefined,
hookId: string,
): NonNullable<ClaudeSettings['hooks']>[string] {
return (existing ?? [])
.map((entry) => ({
...entry,
hooks: entry.hooks.filter((hook) => !isColonyHookCommand(hook.command, hookId)),
}))
.filter((entry) => entry.hooks.length > 0);
}

export const claudeCode: Installer = {
id: 'claude-code',
label: 'Claude Code',
Expand All @@ -40,16 +79,8 @@ export const claudeCode: Installer = {
const nodeBin = shellQuote(ctx.nodeBin);
const cliPath = shellQuote(ctx.cliPath);
for (const [claudeName, hookId] of HOOK_NAMES) {
hooks[claudeName] = [
{
hooks: [
{
type: 'command',
command: `${nodeBin} ${cliPath} hook run ${hookId} --ide claude-code`,
},
],
},
];
const command = `${nodeBin} ${cliPath} hook run ${hookId} --ide claude-code`;
hooks[claudeName] = installColonyHook(hooks[claudeName], command, hookId);
}
const mcpServers: NonNullable<ClaudeSettings['mcpServers']> = { ...(current.mcpServers ?? {}) };
delete mcpServers.cavemem;
Expand All @@ -67,7 +98,11 @@ export const claudeCode: Installer = {
const path = settingsFile();
const current = readJson<ClaudeSettings>(path, {});
if (current.hooks) {
for (const [claudeName] of HOOK_NAMES) delete current.hooks[claudeName];
for (const [claudeName, hookId] of HOOK_NAMES) {
const remaining = removeColonyHook(current.hooks[claudeName], hookId);
if (remaining.length > 0) current.hooks[claudeName] = remaining;
else delete current.hooks[claudeName];
}
}
if (current.mcpServers) {
delete current.mcpServers.colony;
Expand Down
39 changes: 38 additions & 1 deletion packages/installers/test/installers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ describe('claude-code installer', () => {
await claudeCode.install(ctx); // run twice
const second = JSON.parse(readFileSync(settingsPath, 'utf8')) as typeof first;
expect(Object.keys(second.hooks).sort()).toEqual(Object.keys(first.hooks).sort());
expect(second.hooks.SessionStart).toHaveLength(1);
// No duplicate or stale MCP namespace entries.
expect(Object.keys(second.mcpServers)).toEqual(['colony']);
});
Expand All @@ -96,7 +97,23 @@ describe('claude-code installer', () => {
other: { command: '/other/bin' },
cavemem: { command: '/old/bin', args: ['old-mcp'] },
},
hooks: { CustomEvent: [{ hooks: [{ type: 'command', command: 'noop' }] }] },
hooks: {
CustomEvent: [{ hooks: [{ type: 'command', command: 'noop' }] }],
PostToolUse: [
{
matcher: '*',
hooks: [{ type: 'command', command: 'node /home/me/.claude/hooks/context.js' }],
},
{
hooks: [
{
type: 'command',
command: '/old/bin/colony hook run post-tool-use --ide claude-code',
},
],
},
],
},
}),
);

Expand All @@ -114,6 +131,20 @@ describe('claude-code installer', () => {
});
expect(installed.mcpServers.cavemem).toBeUndefined();
expect(installed.hooks.CustomEvent).toBeDefined();
expect(installed.hooks.PostToolUse).toEqual([
{
matcher: '*',
hooks: [{ type: 'command', command: 'node /home/me/.claude/hooks/context.js' }],
},
{
hooks: [
{
type: 'command',
command: `${ctx.nodeBin} ${ctx.cliPath} hook run post-tool-use --ide claude-code`,
},
],
},
]);

await claudeCode.uninstall(ctx);
const after = JSON.parse(readFileSync(settingsPath, 'utf8')) as typeof installed;
Expand All @@ -123,6 +154,12 @@ describe('claude-code installer', () => {
expect(after.mcpServers.cavemem).toBeUndefined();
expect(after.hooks.SessionStart).toBeUndefined();
expect(after.hooks.CustomEvent).toBeDefined();
expect(after.hooks.PostToolUse).toEqual([
{
matcher: '*',
hooks: [{ type: 'command', command: 'node /home/me/.claude/hooks/context.js' }],
},
]);
});

it('quotes paths with spaces in hook command strings (Windows)', async () => {
Expand Down
Loading