Skip to content

Commit 9d19496

Browse files
authored
refactor(hooks): count unique files once during discovery (#108)
1 parent b5183e6 commit 9d19496

2 files changed

Lines changed: 24 additions & 37 deletions

File tree

‎src/bundle-plugins.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -729,16 +729,17 @@ process.exitCode = ${code};
729729
path.join(root, ".claude-plugin", "plugin.json"),
730730
JSON.stringify({ name: "conventional-plus-dir", hooks: "./hooks" }),
731731
);
732-
const hook = { SessionStart: [{ hooks: [{ type: "command", command: "exit 0" }] }] };
733-
await fs.writeFile(path.join(hooksDir, "hooks.json"), JSON.stringify({ hooks: hook }));
732+
const hook = (command: string) => ({ SessionStart: [{ hooks: [{ type: "command", command }] }] });
733+
await fs.writeFile(path.join(hooksDir, "hooks.json"), JSON.stringify({ hooks: hook("exit 1") }));
734734
for (let index = 1; index < MAX_HOOK_JSON_FILES; index += 1) {
735735
await fs.writeFile(
736736
path.join(hooksDir, `hook-${String(index).padStart(2, "0")}.json`),
737-
JSON.stringify({ hooks: hook }),
737+
JSON.stringify({ hooks: hook("exit 0") }),
738738
);
739739
}
740740
const plugin = await inspectBundlePlugin("claude-code", root);
741741
expect(plugin.hooks).toHaveLength(MAX_HOOK_JSON_FILES);
742+
expect(plugin.hooks[0]?.command).toBe("exit 1");
742743
});
743744

744745
it("counts unique JSON hook files at and above the Codex directory limit", async () => {

‎src/bundle-plugins.ts‎

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,8 @@ async function readHooks(
313313
collectHooks(app, asObject(inline.hooks) ?? inline, hooks, unsupported);
314314
}
315315
const seen = new Set<string>();
316-
let remainingJsonFiles = MAX_HOOK_JSON_FILES;
317316
for (const candidate of paths) {
318-
const discovered = await hookFiles(root, candidate, remainingJsonFiles, seen);
319-
remainingJsonFiles -= discovered.length;
320-
for (const file of discovered) {
317+
for (const file of await hookFiles(root, candidate, seen)) {
321318
const raw = await readJson(file);
322319
const events = asObject(raw?.hooks) ?? raw;
323320
if (events) {
@@ -331,7 +328,6 @@ async function readHooks(
331328
async function hookFiles(
332329
root: string,
333330
candidate: string,
334-
remainingJsonFiles: number,
335331
seen: Set<string>,
336332
): Promise<string[]> {
337333
const target = underRoot(root, candidate);
@@ -347,43 +343,33 @@ async function hookFiles(
347343
if (stats.isSymbolicLink()) {
348344
throw new Error(`Plugin hook path uses a symlink: ${candidate}`);
349345
}
350-
if (!stats.isDirectory()) {
351-
if (seen.has(target)) {
352-
return [];
353-
}
354-
if (remainingJsonFiles <= 0) {
346+
const files: string[] = [];
347+
function addFile(file: string): void {
348+
if (seen.has(file)) return;
349+
if (seen.size >= MAX_HOOK_JSON_FILES) {
355350
throw new Error(`Plugin hook tree exceeded the ${MAX_HOOK_JSON_FILES}-file limit`);
356351
}
357-
seen.add(target);
358-
return [target];
352+
seen.add(file);
353+
files.push(file);
354+
}
355+
if (!stats.isDirectory()) {
356+
addFile(target);
357+
return files;
359358
}
360-
const files: string[] = [];
361359
async function walk(directory: string, depth: number): Promise<void> {
362360
if (depth > MAX_HOOK_WALK_DEPTH) {
363361
throw new Error(`Plugin hook tree exceeded the ${MAX_HOOK_WALK_DEPTH}-directory depth limit`);
364362
}
365-
const dir = await fs.opendir(directory);
366-
try {
367-
for await (const entry of dir) {
368-
const child = path.join(directory, entry.name);
369-
if (entry.isSymbolicLink()) {
370-
throw new Error(`Plugin hook path uses a symlink: ${path.relative(root, child)}`);
371-
}
372-
if (entry.isDirectory()) {
373-
await walk(child, depth + 1);
374-
} else if (entry.isFile() && entry.name.endsWith(".json")) {
375-
if (seen.has(child)) {
376-
continue;
377-
}
378-
if (files.length >= remainingJsonFiles) {
379-
throw new Error(`Plugin hook tree exceeded the ${MAX_HOOK_JSON_FILES}-file limit`);
380-
}
381-
seen.add(child);
382-
files.push(child);
383-
}
363+
for await (const entry of await fs.opendir(directory)) {
364+
const child = path.join(directory, entry.name);
365+
if (entry.isSymbolicLink()) {
366+
throw new Error(`Plugin hook path uses a symlink: ${path.relative(root, child)}`);
367+
}
368+
if (entry.isDirectory()) {
369+
await walk(child, depth + 1);
370+
} else if (entry.isFile() && entry.name.endsWith(".json")) {
371+
addFile(child);
384372
}
385-
} finally {
386-
await dir.close().catch(() => undefined);
387373
}
388374
}
389375
await walk(target, 0);

0 commit comments

Comments
 (0)