|
| 1 | +import { join } from 'node:path'; |
| 2 | +import { FILE_HOOKS_JSON } from '../constants'; |
| 3 | +import type { CursorHooksConfig } from '../schema'; |
| 4 | +import { fileExists, readJsonFile, writeJsonFile } from './fs'; |
| 5 | +import { defaultIO } from './io'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Read existing Cursor hooks.json file |
| 9 | + * |
| 10 | + * @param cursorDir - The .cursor directory path |
| 11 | + * @returns Existing hooks configuration or null if file doesn't exist |
| 12 | + */ |
| 13 | +export async function readExistingHooks(cursorDir: string): Promise<CursorHooksConfig | null> { |
| 14 | + const hooksPath = join(cursorDir, FILE_HOOKS_JSON); |
| 15 | + |
| 16 | + if (!(await fileExists(hooksPath))) { |
| 17 | + return null; |
| 18 | + } |
| 19 | + |
| 20 | + try { |
| 21 | + // Read without strict schema validation to allow user hooks that don't match AIPM format |
| 22 | + const rawData = await readJsonFile<any>(hooksPath); |
| 23 | + |
| 24 | + // Basic validation - must have version and hooks |
| 25 | + if (!rawData || typeof rawData !== 'object' || !rawData.hooks) { |
| 26 | + defaultIO.logInfo(`⚠️ Invalid ${FILE_HOOKS_JSON} format - will create new file`); |
| 27 | + return null; |
| 28 | + } |
| 29 | + |
| 30 | + // Return as CursorHooksConfig (may contain non-AIPM hooks) |
| 31 | + return rawData as CursorHooksConfig; |
| 32 | + } catch (error) { |
| 33 | + // If file exists but is invalid JSON, log warning and return null |
| 34 | + // This allows AIPM to create a new hooks.json |
| 35 | + defaultIO.logInfo(`⚠️ Failed to parse existing ${FILE_HOOKS_JSON} - will create new file: ${error}`); |
| 36 | + return null; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Preserve user hooks (hooks not managed by AIPM) and merge AIPM hooks |
| 42 | + * |
| 43 | + * @param existingHooks - Existing hooks configuration (may be null) |
| 44 | + * @param aipmHooks - Array of AIPM-managed hook configurations to merge |
| 45 | + * @returns Merged hooks configuration |
| 46 | + */ |
| 47 | +export function preserveUserHooks( |
| 48 | + existingHooks: CursorHooksConfig | null, |
| 49 | + aipmHooks: CursorHooksConfig[], |
| 50 | +): CursorHooksConfig { |
| 51 | + const result: CursorHooksConfig = { |
| 52 | + version: 1, |
| 53 | + hooks: {}, |
| 54 | + }; |
| 55 | + |
| 56 | + // First, preserve user hooks (hooks without x-managedBy: "aipm") |
| 57 | + if (existingHooks) { |
| 58 | + for (const [eventName, hooks] of Object.entries(existingHooks.hooks)) { |
| 59 | + const userHooks = hooks.filter((hook) => { |
| 60 | + // Guard against non-object hook entries (null, undefined, primitives, arrays) |
| 61 | + if (!hook || typeof hook !== 'object' || Array.isArray(hook)) { |
| 62 | + return false; // Skip malformed entries |
| 63 | + } |
| 64 | + // Preserve hooks that don't have x-managedBy field or have a different value |
| 65 | + return !('x-managedBy' in hook) || hook['x-managedBy'] !== 'aipm'; |
| 66 | + }); |
| 67 | + |
| 68 | + if (userHooks.length > 0) { |
| 69 | + result.hooks[eventName] = [...(result.hooks[eventName] || []), ...userHooks]; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Then, merge all AIPM hooks |
| 75 | + for (const aipmHookConfig of aipmHooks) { |
| 76 | + for (const [eventName, hooks] of Object.entries(aipmHookConfig.hooks)) { |
| 77 | + if (!result.hooks[eventName]) { |
| 78 | + result.hooks[eventName] = []; |
| 79 | + } |
| 80 | + result.hooks[eventName].push(...hooks); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return result; |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Merge hooks from multiple plugins into a single .cursor/hooks.json file |
| 89 | + * |
| 90 | + * @param cursorDir - The .cursor directory path |
| 91 | + * @param pluginHooks - Array of hook configurations from enabled plugins |
| 92 | + */ |
| 93 | +export async function mergeHooks(cursorDir: string, pluginHooks: CursorHooksConfig[]): Promise<void> { |
| 94 | + // Read existing hooks |
| 95 | + const existingHooks = await readExistingHooks(cursorDir); |
| 96 | + |
| 97 | + // Merge: preserve user hooks + add AIPM hooks |
| 98 | + const mergedHooks = preserveUserHooks(existingHooks, pluginHooks); |
| 99 | + |
| 100 | + // Write merged hooks.json |
| 101 | + // Don't use strict schema validation because user hooks may not have x-managedBy |
| 102 | + const hooksPath = join(cursorDir, FILE_HOOKS_JSON); |
| 103 | + await writeJsonFile(hooksPath, mergedHooks); |
| 104 | +} |
0 commit comments