Fix crash when hook entries have no matcher (#160)
Claude Code allows hook entries without a `matcher` field (e.g., SessionStart and SubagentStop hooks don't need one). The OpenCode converter assumed `matcher.matcher` was always present, causing "undefined is not an object (evaluating 'matcher.matcher.split')" when converting plugins with matcher-less hooks. Make `matcher` optional in the type and guard all accesses.
This commit is contained in:
@@ -209,9 +209,11 @@ function renderHookStatements(
|
|||||||
): string[] {
|
): string[] {
|
||||||
if (!matcher.hooks || matcher.hooks.length === 0) return []
|
if (!matcher.hooks || matcher.hooks.length === 0) return []
|
||||||
const tools = matcher.matcher
|
const tools = matcher.matcher
|
||||||
.split("|")
|
? matcher.matcher
|
||||||
.map((tool) => tool.trim().toLowerCase())
|
.split("|")
|
||||||
.filter(Boolean)
|
.map((tool) => tool.trim().toLowerCase())
|
||||||
|
.filter(Boolean)
|
||||||
|
: []
|
||||||
|
|
||||||
const useMatcher = useToolMatcher && tools.length > 0 && !tools.includes("*")
|
const useMatcher = useToolMatcher && tools.length > 0 && !tools.includes("*")
|
||||||
const condition = useMatcher
|
const condition = useMatcher
|
||||||
@@ -232,10 +234,10 @@ function renderHookStatements(
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if (hook.type === "prompt") {
|
if (hook.type === "prompt") {
|
||||||
statements.push(`// Prompt hook for ${matcher.matcher}: ${hook.prompt.replace(/\n/g, " ")}`)
|
statements.push(`// Prompt hook for ${matcher.matcher ?? "*"}: ${hook.prompt.replace(/\n/g, " ")}`)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
statements.push(`// Agent hook for ${matcher.matcher}: ${hook.agent}`)
|
statements.push(`// Agent hook for ${matcher.matcher ?? "*"}: ${hook.agent}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
return statements
|
return statements
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ export type ClaudeHookAgent = {
|
|||||||
export type ClaudeHookEntry = ClaudeHookCommand | ClaudeHookPrompt | ClaudeHookAgent
|
export type ClaudeHookEntry = ClaudeHookCommand | ClaudeHookPrompt | ClaudeHookAgent
|
||||||
|
|
||||||
export type ClaudeHookMatcher = {
|
export type ClaudeHookMatcher = {
|
||||||
matcher: string
|
matcher?: string
|
||||||
hooks: ClaudeHookEntry[]
|
hooks: ClaudeHookEntry[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user