← back to ClawCoder
src/lib/generator/hooks.ts
58 lines
import type { Profile, HookConfig } from '../profiles/types'
/**
* Settings JSON structure for .claude/settings.json
*/
export interface SettingsJson {
hooks?: HookConfig
permissions?: {
allow?: string[]
deny?: string[]
}
}
/**
* Generate .claude/settings.json content from a profile's hook config.
*/
export function generateSettingsJson(profile: Profile): SettingsJson {
const settings: SettingsJson = {}
// Only include hooks if the profile has any defined
const hasHooks = profile.hooks && (
(profile.hooks.PreToolUse && profile.hooks.PreToolUse.length > 0) ||
(profile.hooks.PostToolUse && profile.hooks.PostToolUse.length > 0) ||
(profile.hooks.Stop && profile.hooks.Stop.length > 0) ||
(profile.hooks.SessionStart && profile.hooks.SessionStart.length > 0) ||
(profile.hooks.PreCompact && profile.hooks.PreCompact.length > 0)
)
if (hasHooks) {
settings.hooks = {}
if (profile.hooks.PreToolUse && profile.hooks.PreToolUse.length > 0) {
settings.hooks.PreToolUse = profile.hooks.PreToolUse
}
if (profile.hooks.PostToolUse && profile.hooks.PostToolUse.length > 0) {
settings.hooks.PostToolUse = profile.hooks.PostToolUse
}
if (profile.hooks.Stop && profile.hooks.Stop.length > 0) {
settings.hooks.Stop = profile.hooks.Stop
}
if (profile.hooks.SessionStart && profile.hooks.SessionStart.length > 0) {
settings.hooks.SessionStart = profile.hooks.SessionStart
}
if (profile.hooks.PreCompact && profile.hooks.PreCompact.length > 0) {
settings.hooks.PreCompact = profile.hooks.PreCompact
}
}
return settings
}
/**
* Serialize settings to a formatted JSON string.
*/
export function serializeSettingsJson(settings: SettingsJson): string {
return JSON.stringify(settings, null, 2)
}