feat(shared): add Claude hooks command executor and utilities
- Add snake-case.ts: objectToSnakeCase, objectToCamelCase utilities - Add tool-name.ts: transformToolName with PascalCase conversion - Add pattern-matcher.ts: findMatchingHooks for hook config matching - Add hook-disabled.ts: isHookDisabled for hook config validation - Add temporary stub types at src/hooks/claude-code-hooks/types.ts - Export all new utilities from src/shared/index.ts Stub types will be replaced with full implementation in Task 1. Import paths adjusted from opencode-cc-plugin structure to oh-my-opencode. 🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)
This commit is contained in:
25
src/hooks/claude-code-hooks/types.ts
Normal file
25
src/hooks/claude-code-hooks/types.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
// Temporary stub types for Task 0 - will be fully implemented in Task 1
|
||||
// These are minimal definitions to allow shared utilities to type-check
|
||||
|
||||
export interface HookCommand {
|
||||
type: string
|
||||
command: string
|
||||
}
|
||||
|
||||
export interface HookMatcher {
|
||||
matcher: string
|
||||
hooks: HookCommand[]
|
||||
}
|
||||
|
||||
export interface ClaudeHooksConfig {
|
||||
PreToolUse?: HookMatcher[]
|
||||
PostToolUse?: HookMatcher[]
|
||||
UserPromptSubmit?: HookMatcher[]
|
||||
Stop?: HookMatcher[]
|
||||
}
|
||||
|
||||
export type ClaudeHookEvent = "PreToolUse" | "PostToolUse" | "UserPromptSubmit" | "Stop"
|
||||
|
||||
export interface PluginConfig {
|
||||
disabledHooks?: boolean | ClaudeHookEvent[]
|
||||
}
|
||||
22
src/shared/hook-disabled.ts
Normal file
22
src/shared/hook-disabled.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import type { ClaudeHookEvent, PluginConfig } from "../hooks/claude-code-hooks/types"
|
||||
|
||||
export function isHookDisabled(
|
||||
config: PluginConfig,
|
||||
hookType: ClaudeHookEvent
|
||||
): boolean {
|
||||
const { disabledHooks } = config
|
||||
|
||||
if (disabledHooks === undefined) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (disabledHooks === true) {
|
||||
return true
|
||||
}
|
||||
|
||||
if (Array.isArray(disabledHooks)) {
|
||||
return disabledHooks.includes(hookType)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -3,3 +3,7 @@ export * from "./command-executor"
|
||||
export * from "./file-reference-resolver"
|
||||
export * from "./model-sanitizer"
|
||||
export * from "./logger"
|
||||
export * from "./snake-case"
|
||||
export * from "./tool-name"
|
||||
export * from "./pattern-matcher"
|
||||
export * from "./hook-disabled"
|
||||
|
||||
29
src/shared/pattern-matcher.ts
Normal file
29
src/shared/pattern-matcher.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import type { ClaudeHooksConfig, HookMatcher } from "../hooks/claude-code-hooks/types"
|
||||
|
||||
export function matchesToolMatcher(toolName: string, matcher: string): boolean {
|
||||
if (!matcher) {
|
||||
return true
|
||||
}
|
||||
const patterns = matcher.split("|").map((p) => p.trim())
|
||||
return patterns.some((p) => {
|
||||
if (p.includes("*")) {
|
||||
const regex = new RegExp(`^${p.replace(/\*/g, ".*")}$`, "i")
|
||||
return regex.test(toolName)
|
||||
}
|
||||
return p.toLowerCase() === toolName.toLowerCase()
|
||||
})
|
||||
}
|
||||
|
||||
export function findMatchingHooks(
|
||||
config: ClaudeHooksConfig,
|
||||
eventName: keyof ClaudeHooksConfig,
|
||||
toolName?: string
|
||||
): HookMatcher[] {
|
||||
const hookMatchers = config[eventName]
|
||||
if (!hookMatchers) return []
|
||||
|
||||
return hookMatchers.filter((hookMatcher) => {
|
||||
if (!toolName) return true
|
||||
return matchesToolMatcher(toolName, hookMatcher.matcher)
|
||||
})
|
||||
}
|
||||
51
src/shared/snake-case.ts
Normal file
51
src/shared/snake-case.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
export function camelToSnake(str: string): string {
|
||||
return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`)
|
||||
}
|
||||
|
||||
export function snakeToCamel(str: string): string {
|
||||
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase())
|
||||
}
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value)
|
||||
}
|
||||
|
||||
export function objectToSnakeCase(
|
||||
obj: Record<string, unknown>,
|
||||
deep: boolean = true
|
||||
): Record<string, unknown> {
|
||||
const result: Record<string, unknown> = {}
|
||||
for (const [key, value] of Object.entries(obj)) {
|
||||
const snakeKey = camelToSnake(key)
|
||||
if (deep && isPlainObject(value)) {
|
||||
result[snakeKey] = objectToSnakeCase(value, true)
|
||||
} else if (deep && Array.isArray(value)) {
|
||||
result[snakeKey] = value.map((item) =>
|
||||
isPlainObject(item) ? objectToSnakeCase(item, true) : item
|
||||
)
|
||||
} else {
|
||||
result[snakeKey] = value
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
export function objectToCamelCase(
|
||||
obj: Record<string, unknown>,
|
||||
deep: boolean = true
|
||||
): Record<string, unknown> {
|
||||
const result: Record<string, unknown> = {}
|
||||
for (const [key, value] of Object.entries(obj)) {
|
||||
const camelKey = snakeToCamel(key)
|
||||
if (deep && isPlainObject(value)) {
|
||||
result[camelKey] = objectToCamelCase(value, true)
|
||||
} else if (deep && Array.isArray(value)) {
|
||||
result[camelKey] = value.map((item) =>
|
||||
isPlainObject(item) ? objectToCamelCase(item, true) : item
|
||||
)
|
||||
} else {
|
||||
result[camelKey] = value
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
26
src/shared/tool-name.ts
Normal file
26
src/shared/tool-name.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
const SPECIAL_TOOL_MAPPINGS: Record<string, string> = {
|
||||
webfetch: "WebFetch",
|
||||
websearch: "WebSearch",
|
||||
todoread: "TodoRead",
|
||||
todowrite: "TodoWrite",
|
||||
}
|
||||
|
||||
function toPascalCase(str: string): string {
|
||||
return str
|
||||
.split(/[-_\s]+/)
|
||||
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
|
||||
.join("")
|
||||
}
|
||||
|
||||
export function transformToolName(toolName: string): string {
|
||||
const lower = toolName.toLowerCase()
|
||||
if (lower in SPECIAL_TOOL_MAPPINGS) {
|
||||
return SPECIAL_TOOL_MAPPINGS[lower]
|
||||
}
|
||||
|
||||
if (toolName.includes("-") || toolName.includes("_")) {
|
||||
return toPascalCase(toolName)
|
||||
}
|
||||
|
||||
return toolName.charAt(0).toUpperCase() + toolName.slice(1)
|
||||
}
|
||||
Reference in New Issue
Block a user