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
10 changes: 5 additions & 5 deletions src/features/hooks/claudecode-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import type { RulesyncHooks } from "./rulesync-hooks.js";

import {
CLAUDE_HOOK_EVENTS,
CLAUDE_TO_CURSOR_EVENT_NAMES,
CURSOR_TO_CLAUDE_EVENT_NAMES,
CLAUDE_TO_CANONICAL_EVENT_NAMES,
CANONICAL_TO_CLAUDE_EVENT_NAMES,
} from "../../types/hooks.js";
import { formatError } from "../../utils/error.js";
import { readFileContentOrNull, readOrInitializeFileContent } from "../../utils/file.js";
Expand All @@ -21,7 +21,7 @@ import {
} from "./tool-hooks.js";

/**
* Convert canonical (Cursor-style) hooks config to Claude format.
* Convert canonical hooks config to Claude format.
* Filters shared hooks to CLAUDE_HOOK_EVENTS, merges config.claudecode?.hooks,
* then converts to PascalCase and Claude matcher/hooks structure.
*/
Expand All @@ -39,7 +39,7 @@ function canonicalToClaudeHooks(config: HooksConfig): Record<string, unknown[]>
};
const claude: Record<string, unknown[]> = {};
for (const [eventName, definitions] of Object.entries(effectiveHooks)) {
const claudeEventName = CURSOR_TO_CLAUDE_EVENT_NAMES[eventName] ?? eventName;
const claudeEventName = CANONICAL_TO_CLAUDE_EVENT_NAMES[eventName] ?? eventName;
const byMatcher = new Map<string, HooksConfig["hooks"][string]>();
for (const def of definitions) {
const key = def.matcher ?? "";
Expand Down Expand Up @@ -97,7 +97,7 @@ function claudeHooksToCanonical(claudeHooks: unknown): HooksConfig["hooks"] {
}
const canonical: HooksConfig["hooks"] = {};
for (const [claudeEventName, matcherEntries] of Object.entries(claudeHooks)) {
const eventName = CLAUDE_TO_CURSOR_EVENT_NAMES[claudeEventName] ?? claudeEventName;
const eventName = CLAUDE_TO_CANONICAL_EVENT_NAMES[claudeEventName] ?? claudeEventName;
if (!Array.isArray(matcherEntries)) continue;
const defs: HooksConfig["hooks"][string] = [];
for (const rawEntry of matcherEntries) {
Expand Down
22 changes: 18 additions & 4 deletions src/features/hooks/cursor-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import type { ValidationResult } from "../../types/ai-file.js";
import type { HooksConfig } from "../../types/hooks.js";
import type { RulesyncHooks } from "./rulesync-hooks.js";

import { CURSOR_HOOK_EVENTS } from "../../types/hooks.js";
import {
CURSOR_HOOK_EVENTS,
CURSOR_TO_CANONICAL_EVENT_NAMES,
CANONICAL_TO_CURSOR_EVENT_NAMES,
} from "../../types/hooks.js";
import { readFileContent } from "../../utils/file.js";
import {
ToolHooks,
Expand Down Expand Up @@ -69,9 +73,14 @@ export class CursorHooks extends ToolHooks {
...sharedHooks,
...config.cursor?.hooks,
};
const mappedHooks: HooksConfig["hooks"] = {};
for (const [eventName, defs] of Object.entries(mergedHooks)) {
const cursorEventName = CANONICAL_TO_CURSOR_EVENT_NAMES[eventName] ?? eventName;
mappedHooks[cursorEventName] = defs;
}
const cursorConfig = {
version: config.version ?? 1,
hooks: mergedHooks,
hooks: mappedHooks,
};
const fileContent = JSON.stringify(cursorConfig, null, 2);
const paths = CursorHooks.getSettablePaths();
Expand All @@ -88,10 +97,15 @@ export class CursorHooks extends ToolHooks {
toRulesyncHooks(): RulesyncHooks {
const content = this.getFileContent();
const parsed: { version?: number; hooks?: HooksConfig["hooks"] } = JSON.parse(content);
const hooks = parsed.hooks ?? {};
const cursorHooks = parsed.hooks ?? {};
const canonicalHooks: HooksConfig["hooks"] = {};
for (const [cursorEventName, defs] of Object.entries(cursorHooks)) {
const eventName = CURSOR_TO_CANONICAL_EVENT_NAMES[cursorEventName] ?? cursorEventName;
canonicalHooks[eventName] = defs;
}
const version = parsed.version ?? 1;
return this.toRulesyncHooksDefault({
fileContent: JSON.stringify({ version, hooks }, null, 2),
fileContent: JSON.stringify({ version, hooks: canonicalHooks }, null, 2),
});
}

Expand Down
Loading