Merge pull request #4180 from JacobZyy/fix/plugin-hooks-merge
This commit is contained in:
@@ -4,6 +4,7 @@ import type { ModelCacheState } from "../plugin-state";
|
||||
import { log } from "../shared";
|
||||
import { applyAgentConfig } from "./agent-config-handler";
|
||||
import { applyCommandConfig } from "./command-config-handler";
|
||||
import { applyHookConfig } from "./hook-config-handler";
|
||||
import { applyMcpConfig } from "./mcp-config-handler";
|
||||
import { applyProviderConfig } from "./provider-config-handler";
|
||||
import { loadPluginComponents } from "./plugin-components-loader";
|
||||
@@ -30,6 +31,8 @@ export function createConfigHandler(deps: ConfigHandlerDeps) {
|
||||
|
||||
const pluginComponents = await loadPluginComponents({ pluginConfig });
|
||||
|
||||
applyHookConfig({ pluginComponents, ctx });
|
||||
|
||||
const agentResult = await applyAgentConfig({
|
||||
config,
|
||||
pluginConfig,
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
const { afterEach, beforeEach, describe, expect, mock, test } = require("bun:test")
|
||||
|
||||
const mockSetPluginHooksConfigs = mock(() => {})
|
||||
|
||||
mock.module("../hooks/claude-code-hooks/config", () => ({
|
||||
setPluginHooksConfigs: mockSetPluginHooksConfigs,
|
||||
}))
|
||||
|
||||
const { applyHookConfig } = await import("./hook-config-handler")
|
||||
|
||||
describe("applyHookConfig", () => {
|
||||
beforeEach(() => {
|
||||
mockSetPluginHooksConfigs.mockClear()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
mockSetPluginHooksConfigs.mockClear()
|
||||
})
|
||||
|
||||
test("#given ctx.directory #when applyHookConfig called #then setPluginHooksConfigs receives ctx.directory", () => {
|
||||
// given
|
||||
const testDirectory = "/test/dir"
|
||||
const pluginComponents = {
|
||||
commands: {},
|
||||
skills: {},
|
||||
agents: {},
|
||||
mcpServers: {},
|
||||
hooksConfigs: [
|
||||
{
|
||||
hooks: {
|
||||
Stop: [
|
||||
{
|
||||
matcher: "*",
|
||||
hooks: [{ type: "command", command: "echo test" }],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
],
|
||||
plugins: [{ name: "test-plugin", version: "1.0.0" }],
|
||||
errors: [],
|
||||
}
|
||||
|
||||
// when
|
||||
applyHookConfig({
|
||||
pluginComponents,
|
||||
ctx: { directory: testDirectory },
|
||||
})
|
||||
|
||||
// then
|
||||
expect(mockSetPluginHooksConfigs).toHaveBeenCalledTimes(1)
|
||||
expect(mockSetPluginHooksConfigs).toHaveBeenCalledWith(
|
||||
testDirectory,
|
||||
pluginComponents.hooksConfigs,
|
||||
)
|
||||
})
|
||||
|
||||
test("#given empty hooksConfigs #when applyHookConfig called #then setPluginHooksConfigs still called with empty array", () => {
|
||||
// given
|
||||
const testDirectory = "/another/dir"
|
||||
const pluginComponents = {
|
||||
commands: {},
|
||||
skills: {},
|
||||
agents: {},
|
||||
mcpServers: {},
|
||||
hooksConfigs: [],
|
||||
plugins: [],
|
||||
errors: [],
|
||||
}
|
||||
|
||||
// when
|
||||
applyHookConfig({
|
||||
pluginComponents,
|
||||
ctx: { directory: testDirectory },
|
||||
})
|
||||
|
||||
// then
|
||||
expect(mockSetPluginHooksConfigs).toHaveBeenCalledTimes(1)
|
||||
expect(mockSetPluginHooksConfigs).toHaveBeenCalledWith(testDirectory, [])
|
||||
})
|
||||
})
|
||||
|
||||
export {}
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { PluginComponents } from "./plugin-components-loader"
|
||||
import { setPluginHooksConfigs } from "../hooks/claude-code-hooks/config"
|
||||
import { log } from "../shared"
|
||||
|
||||
export function applyHookConfig(params: {
|
||||
pluginComponents: PluginComponents;
|
||||
ctx: { directory: string };
|
||||
}): void {
|
||||
const { pluginComponents, ctx } = params
|
||||
|
||||
if (pluginComponents.hooksConfigs.length > 0) {
|
||||
log("[hook-config-handler] Merging plugin hooks configs", {
|
||||
count: pluginComponents.hooksConfigs.length,
|
||||
plugins: pluginComponents.plugins.map(p => p.name),
|
||||
})
|
||||
}
|
||||
|
||||
setPluginHooksConfigs(ctx.directory, pluginComponents.hooksConfigs)
|
||||
}
|
||||
@@ -3,6 +3,7 @@ export * from "./provider-config-handler";
|
||||
export * from "./agent-config-handler";
|
||||
export * from "./tool-config-handler";
|
||||
export * from "./mcp-config-handler";
|
||||
export * from "./hook-config-handler";
|
||||
export * from "./command-config-handler";
|
||||
export * from "./plugin-components-loader";
|
||||
export * from "./category-config-resolver";
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { OhMyOpenCodeConfig } from "../config";
|
||||
import { loadAllPluginComponents } from "../features/claude-code-plugin-loader";
|
||||
import type { PluginHooksConfig } from "../hooks/claude-code-hooks/types";
|
||||
import { addConfigLoadError, log } from "../shared";
|
||||
|
||||
export type PluginComponents = {
|
||||
@@ -7,7 +8,7 @@ export type PluginComponents = {
|
||||
skills: Record<string, unknown>;
|
||||
agents: Record<string, unknown>;
|
||||
mcpServers: Record<string, unknown>;
|
||||
hooksConfigs: Array<{ hooks?: Record<string, unknown> }>;
|
||||
hooksConfigs: PluginHooksConfig[];
|
||||
plugins: Array<{ name: string; version: string }>;
|
||||
errors: Array<{ pluginKey: string; installPath: string; error: string }>;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user