Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 9 additions & 1 deletion packages/cli/src/ui/commands/hooksCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,15 @@ const listCommand: SlashCommand = {
for (const [eventName, hooks] of hooksByEvent) {
output += `### ${eventName}\n`;
for (const hook of hooks) {
const name = hook.config.name || hook.config.command || 'unnamed';
const name =
hook.config.name ||
(hook.config.type === 'command'
? (hook.config as { command?: string }).command
: undefined) ||
(hook.config.type === 'http'
? (hook.config as { url?: string }).url
: undefined) ||
'unnamed';
const source = formatHookSource(hook.source);
const status = formatHookStatus(hook.enabled);
const matcher = hook.matcher ? ` (matcher: ${hook.matcher})` : '';
Expand Down
22 changes: 15 additions & 7 deletions packages/cli/src/ui/components/hooks/HooksManagementDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,21 @@ const debugLogger = createDebugLogger('HOOKS_DIALOG');
* Type guard to check if a value is a valid HookConfig
*/
function isValidHookConfig(config: unknown): config is HookConfig {
return (
typeof config === 'object' &&
config !== null &&
'type' in config &&
'command' in config &&
typeof (config as HookConfig).command === 'string'
);
if (typeof config !== 'object' || config === null || !('type' in config)) {
return false;
}
const obj = config as Record<string, unknown>;
// Check based on type
if (obj['type'] === 'command') {
return 'command' in obj && typeof obj['command'] === 'string';
}
if (obj['type'] === 'http') {
return 'url' in obj && typeof obj['url'] === 'string';
}
if (obj['type'] === 'function') {
return 'callback' in obj && typeof obj['callback'] === 'function';
}
return false;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions packages/core/src/extension/claude-converter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,10 @@ describe('convertClaudePluginPackage', () => {
expect(result.config.hooks).toBeDefined();
expect(result.config.hooks!['PostToolUse']).toHaveLength(1);
// Check that the variable was substituted
expect(result.config.hooks!['PostToolUse']![0].hooks![0].command).toBe(
`${pluginSourceDir}/scripts/post-install.sh`,
);
expect(
(result.config.hooks!['PostToolUse']![0].hooks![0] as { command: string })
.command,
).toBe(`${pluginSourceDir}/scripts/post-install.sh`);

// Clean up converted directory
fs.rmSync(result.convertedDir, { recursive: true, force: true });
Expand Down
60 changes: 42 additions & 18 deletions packages/core/src/extension/extensionManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -808,9 +808,13 @@ describe('extension tests', () => {
expect(extensions).toHaveLength(1);
expect(extensions[0].hooks).toBeDefined();
expect(extensions[0].hooks!['PreToolUse']).toHaveLength(1);
expect(extensions[0].hooks!['PreToolUse']![0].hooks![0].command).toBe(
'echo "hello"',
);
expect(
(
extensions[0].hooks!['PreToolUse']![0].hooks![0] as {
command: string;
}
).command,
).toBe('echo "hello"');
});

it('should load hooks from hooks/hooks.json when not in main config', async () => {
Expand Down Expand Up @@ -861,9 +865,13 @@ describe('extension tests', () => {
expect(extensions).toHaveLength(1);
expect(extensions[0].hooks).toBeDefined();
expect(extensions[0].hooks!['PostToolUse']).toHaveLength(1);
expect(extensions[0].hooks!['PostToolUse']![0].hooks![0].command).toBe(
`echo "installed in ${extensionDir}"`,
);
expect(
(
extensions[0].hooks!['PostToolUse']![0].hooks![0] as {
command: string;
}
).command,
).toBe(`echo "installed in ${extensionDir}"`);
});

it('should substitute ${CLAUDE_PLUGIN_ROOT} variable in hooks', async () => {
Expand Down Expand Up @@ -901,9 +909,13 @@ describe('extension tests', () => {
expect(extensions).toHaveLength(1);
expect(extensions[0].hooks).toBeDefined();
expect(extensions[0].hooks!['PreToolUse']).toHaveLength(1);
expect(extensions[0].hooks!['PreToolUse']![0].hooks![0].command).toBe(
`${extensionDir}/scripts/setup.sh`,
);
expect(
(
extensions[0].hooks!['PreToolUse']![0].hooks![0] as {
command: string;
}
).command,
).toBe(`${extensionDir}/scripts/setup.sh`);
});

it('should load hooks from config.hooks string path', async () => {
Expand Down Expand Up @@ -955,9 +967,13 @@ describe('extension tests', () => {
expect(extensions).toHaveLength(1);
expect(extensions[0].hooks).toBeDefined();
expect(extensions[0].hooks!['PreToolUse']).toHaveLength(1);
expect(extensions[0].hooks!['PreToolUse']![0].hooks![0].command).toBe(
'echo "custom hooks path"',
);
expect(
(
extensions[0].hooks!['PreToolUse']![0].hooks![0] as {
command: string;
}
).command,
).toBe('echo "custom hooks path"');
});

it('should prefer config.hooks string path over hooks/hooks.json', async () => {
Expand Down Expand Up @@ -1013,9 +1029,13 @@ describe('extension tests', () => {

expect(extensions).toHaveLength(1);
expect(extensions[0].hooks).toBeDefined();
expect(extensions[0].hooks!['PreToolUse']![0].hooks![0].command).toBe(
'echo "config path"',
);
expect(
(
extensions[0].hooks!['PreToolUse']![0].hooks![0] as {
command: string;
}
).command,
).toBe('echo "config path"');
});

it('should substitute ${CLAUDE_PLUGIN_ROOT} in hooks file from config.hooks string path', async () => {
Expand Down Expand Up @@ -1065,9 +1085,13 @@ describe('extension tests', () => {
expect(extensions).toHaveLength(1);
expect(extensions[0].hooks).toBeDefined();
expect(extensions[0].hooks!['PreToolUse']).toHaveLength(1);
expect(extensions[0].hooks!['PreToolUse']![0].hooks![0].command).toBe(
`${extensionDir}/scripts/setup.sh`,
);
expect(
(
extensions[0].hooks!['PreToolUse']![0].hooks![0] as {
command: string;
}
).command,
).toBe(`${extensionDir}/scripts/setup.sh`);
});
});
});
61 changes: 32 additions & 29 deletions packages/core/src/extension/variables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('substituteHookVariables', () => {
description: 'Setup before start',
hooks: [
{
type: HookType.Command,
type: HookType.Command as const,
command: '${CLAUDE_PLUGIN_ROOT}/scripts/setup.sh',
},
],
Expand All @@ -47,9 +47,9 @@ describe('substituteHookVariables', () => {

expect(result).toBeDefined();
expect(result!['PreToolUse']).toHaveLength(1);
expect(result!['PreToolUse']![0].hooks![0].command).toBe(
'/path/to/plugin/scripts/setup.sh',
);
expect(
(result!['PreToolUse']![0].hooks![0] as { command: string }).command,
).toBe('/path/to/plugin/scripts/setup.sh');
});

it('should handle multiple hooks with variables', () => {
Expand All @@ -61,7 +61,7 @@ describe('substituteHookVariables', () => {
description: 'Post install hook 1',
hooks: [
{
type: HookType.Command,
type: HookType.Command as const,
command: '${CLAUDE_PLUGIN_ROOT}/bin/init.sh',
},
],
Expand All @@ -70,7 +70,7 @@ describe('substituteHookVariables', () => {
description: 'Post install hook 2',
hooks: [
{
type: HookType.Command,
type: HookType.Command as const,
command: 'chmod +x ${CLAUDE_PLUGIN_ROOT}/bin/executable.sh',
},
],
Expand All @@ -82,12 +82,12 @@ describe('substituteHookVariables', () => {

expect(result).toBeDefined();
expect(result!['PostToolUse']).toHaveLength(2);
expect(result!['PostToolUse']![0].hooks![0].command).toBe(
'/project/plugins/my-plugin/bin/init.sh',
);
expect(result!['PostToolUse']![1].hooks![0].command).toBe(
'chmod +x /project/plugins/my-plugin/bin/executable.sh',
);
expect(
(result!['PostToolUse']![0].hooks![0] as { command: string }).command,
).toBe('/project/plugins/my-plugin/bin/init.sh');
expect(
(result!['PostToolUse']![1].hooks![0] as { command: string }).command,
).toBe('chmod +x /project/plugins/my-plugin/bin/executable.sh');
});

it('should handle multiple event types with hooks', () => {
Expand All @@ -101,7 +101,7 @@ describe('substituteHookVariables', () => {
hooks: [
// HookConfig[] array inside HookDefinition
{
type: HookType.Command, // HookType.Command
type: HookType.Command as const, // HookType.Command
command: '${CLAUDE_PLUGIN_ROOT}/scripts/pre-start.sh',
},
],
Expand All @@ -114,7 +114,7 @@ describe('substituteHookVariables', () => {
hooks: [
// HookConfig[] array inside HookDefinition
{
type: HookType.Command, // HookType.Command
type: HookType.Command as const, // HookType.Command
command: '${CLAUDE_PLUGIN_ROOT}/setup/install.py',
},
],
Expand All @@ -126,13 +126,14 @@ describe('substituteHookVariables', () => {

expect(result).toBeDefined();
expect(result!['PreToolUse']).toHaveLength(1);
expect(result!['PreToolUse']![0].hooks![0].command).toBe(
'/home/user/.qwen/extensions/my-extension/scripts/pre-start.sh',
);
expect(
(result!['PreToolUse']![0].hooks![0] as { command: string }).command,
).toBe('/home/user/.qwen/extensions/my-extension/scripts/pre-start.sh');
expect(result!['UserPromptSubmit']).toHaveLength(1);
expect(result!['UserPromptSubmit']![0].hooks![0].command).toBe(
'/home/user/.qwen/extensions/my-extension/setup/install.py',
);
expect(
(result!['UserPromptSubmit']![0].hooks![0] as { command: string })
.command,
).toBe('/home/user/.qwen/extensions/my-extension/setup/install.py');
});

it('should not modify non-command hooks', () => {
Expand All @@ -146,7 +147,7 @@ describe('substituteHookVariables', () => {
hooks: [
// This is the HookConfig[] array inside HookDefinition
{
type: HookType.Command, // This is part of HookConfig
type: HookType.Command as const, // This is part of HookConfig
command: '${CLAUDE_PLUGIN_ROOT}/scripts/run.sh', // This is part of HookConfig
},
{
Expand All @@ -162,12 +163,12 @@ describe('substituteHookVariables', () => {

expect(result).toBeDefined();
expect(result!['SessionStart']).toHaveLength(1);
expect(result!['SessionStart']![0].hooks![0].command).toBe(
'/path/to/extension/scripts/run.sh',
);
expect(result!['SessionStart']![0].hooks![1].command).toBe(
'${CLAUDE_PLUGIN_ROOT}/not-affected',
); // Non-command type won't be processed
expect(
(result!['SessionStart']![0].hooks![0] as { command: string }).command,
).toBe('/path/to/extension/scripts/run.sh');
expect(
(result!['SessionStart']![0].hooks![1] as { command: string }).command,
).toBe('${CLAUDE_PLUGIN_ROOT}/not-affected'); // Non-command type won't be processed
});

it('should return undefined when hooks is undefined', () => {
Expand All @@ -186,7 +187,7 @@ describe('substituteHookVariables', () => {
hooks: [
// This is the HookConfig[] array inside HookDefinition
{
type: HookType.Command, // This is part of CommandHookConfig
type: HookType.Command as const, // This is part of CommandHookConfig
command: 'echo "hello world"', // This is part of CommandHookConfig
},
],
Expand All @@ -198,7 +199,9 @@ describe('substituteHookVariables', () => {

expect(result).toBeDefined();
expect(result).toEqual(hooks); // Should be equal but not the same object (deep clone)
expect(result!['Stop']![0].hooks![0].command).toBe('echo "hello world"');
expect((result!['Stop']![0].hooks![0] as { command: string }).command).toBe(
'echo "hello world"',
);
});
});

Expand Down
Loading
Loading