@goodfoot/claude-code-hooks 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Scaffold module for generating new Claude Code hook projects.
3
+ *
4
+ * Generates a complete TypeScript project structure with:
5
+ * - package.json with dependencies and scripts
6
+ * - tsconfig.json with ESM/Node20 configuration
7
+ * - biome.json for linting/formatting
8
+ * - Hook template files for each requested hook type
9
+ * - Vitest test files for each hook
10
+ * - vitest.config.ts for test configuration
11
+ * @module
12
+ * @example
13
+ * ```bash
14
+ * npx @goodfoot/claude-code-hooks --scaffold ./my-hooks --hooks Stop,SubagentStop -o dist/hooks.json
15
+ * ```
16
+ */
17
+ /**
18
+ * Options for scaffolding a new hook project.
19
+ */
20
+ export interface ScaffoldOptions {
21
+ /** Directory path where the project will be created. */
22
+ directory: string;
23
+ /** Array of hook event names to generate (e.g., ['Stop', 'SubagentStop']). */
24
+ hooks: string[];
25
+ /** Relative path for hooks.json output in the build script. */
26
+ outputPath: string;
27
+ }
28
+ /**
29
+ * Scaffolds a new Claude Code hook project.
30
+ *
31
+ * Creates the complete project structure including:
32
+ * - package.json, tsconfig.json, biome.json, vitest.config.ts
33
+ * - src/ directory with hook implementations
34
+ * - test/ directory with vitest tests
35
+ * @param options - Scaffold configuration options
36
+ * @throws Exits with code 1 if directory exists or hook names are invalid
37
+ * @example
38
+ * ```typescript
39
+ * scaffoldProject({
40
+ * directory: './my-hooks',
41
+ * hooks: ['Stop', 'SubagentStop'],
42
+ * outputPath: 'dist/hooks.json'
43
+ * });
44
+ * ```
45
+ */
46
+ export declare function scaffoldProject(options: ScaffoldOptions): void;
@@ -0,0 +1,336 @@
1
+ /**
2
+ * Type guards and helper functions for Claude Code tool inputs.
3
+ *
4
+ * Provides safe type narrowing for tool inputs and utility functions
5
+ * for common patterns like file path extraction and content inspection.
6
+ * @example
7
+ * ```typescript
8
+ * import {
9
+ * preToolUseHook,
10
+ * preToolUseOutput,
11
+ * isWriteTool,
12
+ * getFilePath,
13
+ * isTsFile,
14
+ * checkContentForPattern
15
+ * } from '@goodfoot/claude-code-hooks';
16
+ *
17
+ * export default preToolUseHook({ matcher: 'Write|Edit|MultiEdit' }, (input) => {
18
+ * const filePath = getFilePath(input);
19
+ * if (!filePath || !isTsFile(filePath)) return preToolUseOutput({});
20
+ *
21
+ * const result = checkContentForPattern(input, /@ts-ignore/g);
22
+ * if (result?.isAddition) {
23
+ * return preToolUseOutput({
24
+ * hookSpecificOutput: {
25
+ * permissionDecision: 'deny',
26
+ * permissionDecisionReason: `Cannot add: ${result.matches.join(', ')}`
27
+ * }
28
+ * });
29
+ * }
30
+ *
31
+ * return preToolUseOutput({});
32
+ * });
33
+ * ```
34
+ * @see https://code.claude.com/docs/en/hooks
35
+ * @module
36
+ */
37
+ import type { PreToolUseInput, PostToolUseInput, PostToolUseFailureInput, PermissionRequestInput } from './inputs.js';
38
+ import type {
39
+ WriteToolInput,
40
+ EditToolInput,
41
+ MultiEditToolInput,
42
+ ReadToolInput,
43
+ BashToolInput,
44
+ GlobToolInput,
45
+ GrepToolInput,
46
+ FileModifyingToolInput,
47
+ FileModifyingToolName
48
+ } from './tool-inputs.js';
49
+ /**
50
+ * Union of all hook input types that include tool_input.
51
+ */
52
+ export type ToolUseInput = PreToolUseInput | PostToolUseInput | PostToolUseFailureInput | PermissionRequestInput;
53
+ /**
54
+ * Type guard for Write tool inputs.
55
+ *
56
+ * Narrows the input type to include a typed WriteToolInput.
57
+ * @param input - The hook input to check
58
+ * @returns True if the input is for a Write tool
59
+ * @example
60
+ * ```typescript
61
+ * if (isWriteTool(input)) {
62
+ * // input.tool_input is now typed as WriteToolInput
63
+ * console.log(input.tool_input.file_path);
64
+ * console.log(input.tool_input.content);
65
+ * }
66
+ * ```
67
+ */
68
+ export declare function isWriteTool<T extends ToolUseInput>(
69
+ input: T
70
+ ): input is T & {
71
+ tool_name: 'Write';
72
+ tool_input: WriteToolInput;
73
+ };
74
+ /**
75
+ * Type guard for Edit tool inputs.
76
+ *
77
+ * Narrows the input type to include a typed EditToolInput.
78
+ * @param input - The hook input to check
79
+ * @returns True if the input is for an Edit tool
80
+ * @example
81
+ * ```typescript
82
+ * if (isEditTool(input)) {
83
+ * console.log(input.tool_input.old_string);
84
+ * console.log(input.tool_input.new_string);
85
+ * }
86
+ * ```
87
+ */
88
+ export declare function isEditTool<T extends ToolUseInput>(
89
+ input: T
90
+ ): input is T & {
91
+ tool_name: 'Edit';
92
+ tool_input: EditToolInput;
93
+ };
94
+ /**
95
+ * Type guard for MultiEdit tool inputs.
96
+ *
97
+ * Narrows the input type to include a typed MultiEditToolInput.
98
+ * @param input - The hook input to check
99
+ * @returns True if the input is for a MultiEdit tool
100
+ * @example
101
+ * ```typescript
102
+ * if (isMultiEditTool(input)) {
103
+ * for (const edit of input.tool_input.edits) {
104
+ * console.log(`${edit.old_string} -> ${edit.new_string}`);
105
+ * }
106
+ * }
107
+ * ```
108
+ */
109
+ export declare function isMultiEditTool<T extends ToolUseInput>(
110
+ input: T
111
+ ): input is T & {
112
+ tool_name: 'MultiEdit';
113
+ tool_input: MultiEditToolInput;
114
+ };
115
+ /**
116
+ * Type guard for any file-modifying tool (Write, Edit, or MultiEdit).
117
+ *
118
+ * Use this when you need to handle all file modifications generically.
119
+ * @param input - The hook input to check
120
+ * @returns True if the input is for a Write, Edit, or MultiEdit tool
121
+ * @example
122
+ * ```typescript
123
+ * if (isFileModifyingTool(input)) {
124
+ * const filePath = getFilePath(input); // Works for all three types
125
+ * }
126
+ * ```
127
+ */
128
+ export declare function isFileModifyingTool<T extends ToolUseInput>(
129
+ input: T
130
+ ): input is T & {
131
+ tool_name: FileModifyingToolName;
132
+ tool_input: FileModifyingToolInput;
133
+ };
134
+ /**
135
+ * Type guard for Read tool inputs.
136
+ *
137
+ * Narrows the input type to include a typed ReadToolInput.
138
+ * @param input - The hook input to check
139
+ * @returns True if the input is for a Read tool
140
+ * @example
141
+ * ```typescript
142
+ * if (isReadTool(input)) {
143
+ * console.log(input.tool_input.file_path);
144
+ * console.log(input.tool_input.offset);
145
+ * }
146
+ * ```
147
+ */
148
+ export declare function isReadTool<T extends ToolUseInput>(
149
+ input: T
150
+ ): input is T & {
151
+ tool_name: 'Read';
152
+ tool_input: ReadToolInput;
153
+ };
154
+ /**
155
+ * Type guard for Bash tool inputs.
156
+ *
157
+ * Narrows the input type to include a typed BashToolInput.
158
+ * @param input - The hook input to check
159
+ * @returns True if the input is for a Bash tool
160
+ * @example
161
+ * ```typescript
162
+ * if (isBashTool(input)) {
163
+ * console.log(input.tool_input.command);
164
+ * console.log(input.tool_input.timeout);
165
+ * }
166
+ * ```
167
+ */
168
+ export declare function isBashTool<T extends ToolUseInput>(
169
+ input: T
170
+ ): input is T & {
171
+ tool_name: 'Bash';
172
+ tool_input: BashToolInput;
173
+ };
174
+ /**
175
+ * Type guard for Glob tool inputs.
176
+ *
177
+ * Narrows the input type to include a typed GlobToolInput.
178
+ * @param input - The hook input to check
179
+ * @returns True if the input is for a Glob tool
180
+ * @example
181
+ * ```typescript
182
+ * if (isGlobTool(input)) {
183
+ * console.log(input.tool_input.pattern);
184
+ * console.log(input.tool_input.path);
185
+ * }
186
+ * ```
187
+ */
188
+ export declare function isGlobTool<T extends ToolUseInput>(
189
+ input: T
190
+ ): input is T & {
191
+ tool_name: 'Glob';
192
+ tool_input: GlobToolInput;
193
+ };
194
+ /**
195
+ * Type guard for Grep tool inputs.
196
+ *
197
+ * Narrows the input type to include a typed GrepToolInput.
198
+ * @param input - The hook input to check
199
+ * @returns True if the input is for a Grep tool
200
+ * @example
201
+ * ```typescript
202
+ * if (isGrepTool(input)) {
203
+ * console.log(input.tool_input.pattern);
204
+ * console.log(input.tool_input.glob);
205
+ * }
206
+ * ```
207
+ */
208
+ export declare function isGrepTool<T extends ToolUseInput>(
209
+ input: T
210
+ ): input is T & {
211
+ tool_name: 'Grep';
212
+ tool_input: GrepToolInput;
213
+ };
214
+ /**
215
+ * Extracts the file path from a tool input.
216
+ *
217
+ * Works with Write, Edit, MultiEdit, and Read tools.
218
+ * Returns null for other tools or if file_path is missing.
219
+ * @param input - The hook input to extract from
220
+ * @returns The file path, or null if not applicable
221
+ * @example
222
+ * ```typescript
223
+ * const filePath = getFilePath(input);
224
+ * if (filePath && isTsFile(filePath)) {
225
+ * // Handle TypeScript file
226
+ * }
227
+ * ```
228
+ */
229
+ export declare function getFilePath(input: ToolUseInput): string | null;
230
+ /**
231
+ * Checks if a file path is a JavaScript or TypeScript file.
232
+ *
233
+ * Matches .js, .jsx, .ts, .tsx, .mjs, .mts, .cjs, .cts extensions.
234
+ * @param filePath - The file path to check
235
+ * @returns True if the file is JavaScript or TypeScript
236
+ * @example
237
+ * ```typescript
238
+ * if (isJsTsFile(filePath)) {
239
+ * // Check for TypeScript-specific patterns
240
+ * }
241
+ * ```
242
+ */
243
+ export declare function isJsTsFile(filePath: string): boolean;
244
+ /**
245
+ * Checks if a file path is a TypeScript file.
246
+ *
247
+ * Matches .ts, .tsx, .mts, .cts extensions.
248
+ * @param filePath - The file path to check
249
+ * @returns True if the file is TypeScript
250
+ * @example
251
+ * ```typescript
252
+ * if (isTsFile(filePath)) {
253
+ * // Enforce TypeScript-specific rules
254
+ * }
255
+ * ```
256
+ */
257
+ export declare function isTsFile(filePath: string): boolean;
258
+ /**
259
+ * Result of checking content for a pattern.
260
+ */
261
+ export interface PatternCheckResult {
262
+ /** True if the pattern was found in any content. */
263
+ found: boolean;
264
+ /** True if the pattern is being added (not present in old content, present in new). */
265
+ isAddition: boolean;
266
+ /** All matches found across all content (deduplicated). */
267
+ matches: string[];
268
+ /** Per-edit details for MultiEdit operations. */
269
+ details?: Array<{
270
+ /** Index of the edit (for MultiEdit) or 0 for Write/Edit. */
271
+ index: number;
272
+ /** True if found in this edit. */
273
+ found: boolean;
274
+ /** True if this edit adds the pattern. */
275
+ isAddition: boolean;
276
+ /** Matches in this edit. */
277
+ matches: string[];
278
+ }>;
279
+ }
280
+ /**
281
+ * Checks if a pattern exists in the content being written or edited.
282
+ *
283
+ * For Write: checks the content being written
284
+ * For Edit: checks new_string (and old_string to detect additions)
285
+ * For MultiEdit: checks all edits and aggregates results
286
+ * @param input - The PreToolUse hook input
287
+ * @param pattern - The regex pattern to search for (global flag will be used)
288
+ * @returns Result object, or null if not a file-modifying tool
289
+ * @example
290
+ * ```typescript
291
+ * // Block @ts-ignore being added
292
+ * const result = checkContentForPattern(input, /@ts-ignore/g);
293
+ * if (result?.isAddition) {
294
+ * return preToolUseOutput({
295
+ * hookSpecificOutput: {
296
+ * permissionDecision: 'deny',
297
+ * permissionDecisionReason: `Cannot add: ${result.matches.join(', ')}`
298
+ * }
299
+ * });
300
+ * }
301
+ * ```
302
+ */
303
+ export declare function checkContentForPattern(input: PreToolUseInput, pattern: RegExp): PatternCheckResult | null;
304
+ /**
305
+ * Context passed to the forEachContent callback.
306
+ */
307
+ export interface ContentContext {
308
+ /** The new content being written or replacing old content. */
309
+ newContent: string;
310
+ /** The old content being replaced (null for Write). */
311
+ oldContent: string | null;
312
+ /** Index of the edit (0 for Write/Edit, index for MultiEdit). */
313
+ index: number;
314
+ /** True if this is a Write operation (not Edit/MultiEdit). */
315
+ isWrite: boolean;
316
+ }
317
+ /**
318
+ * Iterates over content in Write/Edit/MultiEdit operations.
319
+ *
320
+ * Provides a unified way to inspect content regardless of operation type.
321
+ * Return false from the callback to stop iteration early.
322
+ * @param input - The PreToolUse hook input
323
+ * @param callback - Function called for each content piece, return false to stop
324
+ * @returns True if all callbacks returned true, false if stopped early or not applicable
325
+ * @example
326
+ * ```typescript
327
+ * // Check all content for sensitive data
328
+ * const hasSensitive = !forEachContent(input, ({ newContent }) => {
329
+ * if (/password|secret|api.?key/i.test(newContent)) {
330
+ * return false; // Stop - found sensitive data
331
+ * }
332
+ * return true; // Continue
333
+ * });
334
+ * ```
335
+ */
336
+ export declare function forEachContent(input: PreToolUseInput, callback: (ctx: ContentContext) => boolean): boolean;
@@ -0,0 +1,228 @@
1
+ /**
2
+ * Type definitions for well-known Claude Code tool inputs.
3
+ *
4
+ * These types define the structure of `toolInput` for common Claude Code tools.
5
+ * Use them with type guards from `tool-helpers.ts` for safe type narrowing,
6
+ * or with typed hook factory overloads for automatic typing.
7
+ * @example
8
+ * ```typescript
9
+ * import { isWriteTool, getFilePath } from '@goodfoot/claude-code-hooks';
10
+ *
11
+ * preToolUseHook({ matcher: 'Write|Edit|MultiEdit' }, (input) => {
12
+ * if (isWriteTool(input)) {
13
+ * // input.tool_input is now typed as WriteToolInput
14
+ * console.log(input.tool_input.file_path);
15
+ * }
16
+ * });
17
+ * ```
18
+ * @see https://code.claude.com/docs/en/hooks
19
+ * @module
20
+ */
21
+ /**
22
+ * Input structure for the Write tool.
23
+ *
24
+ * The Write tool creates or overwrites files with the specified content.
25
+ * @example
26
+ * ```typescript
27
+ * // Example tool input
28
+ * {
29
+ * file_path: '/workspace/src/index.ts',
30
+ * content: 'export const hello = "world";'
31
+ * }
32
+ * ```
33
+ */
34
+ export interface WriteToolInput {
35
+ /** Absolute path to the file to write. */
36
+ file_path: string;
37
+ /** The content to write to the file. */
38
+ content: string;
39
+ }
40
+ /**
41
+ * Input structure for the Edit tool.
42
+ *
43
+ * The Edit tool performs search-and-replace operations on files.
44
+ * @example
45
+ * ```typescript
46
+ * // Example tool input
47
+ * {
48
+ * file_path: '/workspace/src/index.ts',
49
+ * old_string: 'const x = 1;',
50
+ * new_string: 'const x = 2;',
51
+ * replace_all: false
52
+ * }
53
+ * ```
54
+ */
55
+ export interface EditToolInput {
56
+ /** Absolute path to the file to edit. */
57
+ file_path: string;
58
+ /** The text to search for (must be unique in the file unless replace_all is true). */
59
+ old_string: string;
60
+ /** The text to replace old_string with. */
61
+ new_string: string;
62
+ /** If true, replace all occurrences. If false, old_string must be unique. */
63
+ replace_all?: boolean;
64
+ }
65
+ /**
66
+ * A single edit entry within a MultiEdit operation.
67
+ */
68
+ export interface MultiEditEntry {
69
+ /** The text to search for. */
70
+ old_string: string;
71
+ /** The text to replace old_string with. */
72
+ new_string: string;
73
+ }
74
+ /**
75
+ * Input structure for the MultiEdit tool.
76
+ *
77
+ * The MultiEdit tool performs multiple search-and-replace operations on a single file.
78
+ * All edits are applied atomically.
79
+ * @example
80
+ * ```typescript
81
+ * // Example tool input
82
+ * {
83
+ * file_path: '/workspace/src/index.ts',
84
+ * edits: [
85
+ * { old_string: 'const x = 1;', new_string: 'const x = 2;' },
86
+ * { old_string: 'const y = 1;', new_string: 'const y = 2;' }
87
+ * ]
88
+ * }
89
+ * ```
90
+ */
91
+ export interface MultiEditToolInput {
92
+ /** Absolute path to the file to edit. */
93
+ file_path: string;
94
+ /** Array of edit operations to apply. */
95
+ edits: MultiEditEntry[];
96
+ }
97
+ /**
98
+ * Input structure for the Read tool.
99
+ *
100
+ * The Read tool reads file contents, optionally with offset and limit.
101
+ * @example
102
+ * ```typescript
103
+ * // Example tool input
104
+ * {
105
+ * file_path: '/workspace/src/index.ts',
106
+ * offset: 0,
107
+ * limit: 100
108
+ * }
109
+ * ```
110
+ */
111
+ export interface ReadToolInput {
112
+ /** Absolute path to the file to read. */
113
+ file_path: string;
114
+ /** Line offset to start reading from. */
115
+ offset?: number;
116
+ /** Maximum number of lines to read. */
117
+ limit?: number;
118
+ }
119
+ /**
120
+ * Input structure for the Bash tool.
121
+ *
122
+ * The Bash tool executes shell commands.
123
+ * @example
124
+ * ```typescript
125
+ * // Example tool input
126
+ * {
127
+ * command: 'npm install',
128
+ * timeout: 60000,
129
+ * description: 'Install dependencies'
130
+ * }
131
+ * ```
132
+ */
133
+ export interface BashToolInput {
134
+ /** The command to execute. */
135
+ command: string;
136
+ /** Optional timeout in milliseconds. */
137
+ timeout?: number;
138
+ /** Optional description of what the command does. */
139
+ description?: string;
140
+ }
141
+ /**
142
+ * Input structure for the Glob tool.
143
+ *
144
+ * The Glob tool finds files matching a glob pattern.
145
+ * @example
146
+ * ```typescript
147
+ * // Example tool input
148
+ * {
149
+ * pattern: '**\/*.ts',
150
+ * path: '/workspace/src'
151
+ * }
152
+ * ```
153
+ */
154
+ export interface GlobToolInput {
155
+ /** Glob pattern to match files against. */
156
+ pattern: string;
157
+ /** Directory to search in (defaults to cwd). */
158
+ path?: string;
159
+ }
160
+ /**
161
+ * Input structure for the Grep tool.
162
+ *
163
+ * The Grep tool searches file contents using regex patterns.
164
+ * @example
165
+ * ```typescript
166
+ * // Example tool input
167
+ * {
168
+ * pattern: 'function\\s+\\w+',
169
+ * path: '/workspace/src',
170
+ * glob: '*.ts'
171
+ * }
172
+ * ```
173
+ */
174
+ export interface GrepToolInput {
175
+ /** Regular expression pattern to search for. */
176
+ pattern: string;
177
+ /** Directory to search in (defaults to cwd). */
178
+ path?: string;
179
+ /** Glob pattern to filter files. */
180
+ glob?: string;
181
+ }
182
+ /**
183
+ * Union of all file-modifying tool inputs.
184
+ *
185
+ * Use this type when you need to handle Write, Edit, or MultiEdit generically.
186
+ */
187
+ export type FileModifyingToolInput = WriteToolInput | EditToolInput | MultiEditToolInput;
188
+ /**
189
+ * Tool names for file-modifying tools.
190
+ *
191
+ * Use this type when you need to reference the tool name in type guards.
192
+ */
193
+ export type FileModifyingToolName = 'Write' | 'Edit' | 'MultiEdit';
194
+ /**
195
+ * Union of all known tool inputs.
196
+ *
197
+ * This includes all tool inputs that have well-defined type structures.
198
+ */
199
+ export type KnownToolInput =
200
+ | WriteToolInput
201
+ | EditToolInput
202
+ | MultiEditToolInput
203
+ | ReadToolInput
204
+ | BashToolInput
205
+ | GlobToolInput
206
+ | GrepToolInput;
207
+ /**
208
+ * Tool names for all known tools with typed inputs.
209
+ */
210
+ export type KnownToolName = 'Write' | 'Edit' | 'MultiEdit' | 'Read' | 'Bash' | 'Glob' | 'Grep';
211
+ /**
212
+ * Type mapping from tool name to tool input type.
213
+ *
214
+ * Used by typed factory overloads to provide automatic typing.
215
+ * @example
216
+ * ```typescript
217
+ * type WriteInput = ToolInputMap['Write']; // WriteToolInput
218
+ * ```
219
+ */
220
+ export interface ToolInputMap {
221
+ Write: WriteToolInput;
222
+ Edit: EditToolInput;
223
+ MultiEdit: MultiEditToolInput;
224
+ Read: ReadToolInput;
225
+ Bash: BashToolInput;
226
+ Glob: GlobToolInput;
227
+ Grep: GrepToolInput;
228
+ }