Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

perf(cli): a hook read lists each folder once (#10324)

Christopher Chedeau9ee9c06ad3ad
diff --git a/.changeset/cli-hook-lists-each-folder-once.md b/.changeset/cli-hook-lists-each-folder-once.md
new file mode 100644
index 0000000000..d63c56aff1
--- /dev/null
+++ b/.changeset/cli-hook-lists-each-folder-once.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[perf] A compact or full hook list, and the suggestions for an unknown hook name, list each of Core's folders once. Before, they listed a folder again for each hook they looked up.
+
+@vjeux
diff --git a/packages/cli/api/hook/hook.mjs b/packages/cli/api/hook/hook.mjs
index e92ebd0199..f92f45f0bd 100644
--- a/packages/cli/api/hook/hook.mjs
+++ b/packages/cli/api/hook/hook.mjs
@@ -19,6 +19,7 @@ import {detail as detailLeaf} from './detail/detail.mjs';
import {params as paramsLeaf} from './detail/params/params.mjs';
import {AstryxError} from '../error.mjs';
import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
+import {withListingCache} from '../../foundation/fs/listing-cache.mjs';
/**
* @param {string} [name]
@@ -33,6 +34,18 @@ import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
* @returns {Promise<{type: string, data: unknown}>}
*/
export async function hook(name, options = {}) {
+ // Each hook's lookup walks the same source tree, so the call reads each
+ // directory once.
+ return withListingCache(() => hookRead(name, options));
+}
+
+/**
+ * {@link hook}, without the call's listing cache.
+ * @param {string} [name]
+ * @param {Parameters<typeof hook>[1]} [options]
+ * @returns {ReturnType<typeof hook>}
+ */
+async function hookRead(name, options = {}) {
const {
cwd = process.cwd(),
list: listFlag = false,
diff --git a/packages/cli/api/hook/listing-scope.test.mjs b/packages/cli/api/hook/listing-scope.test.mjs
new file mode 100644
index 0000000000..7a62ac1e7d
--- /dev/null
+++ b/packages/cli/api/hook/listing-scope.test.mjs
@@ -0,0 +1,51 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file Colocated test that one hook call reads each folder once. It runs
+ * against the real @astryxdesign/core hooks: one compact list, which finds
+ * and loads every hook's doc. A counting wrapper replaces `fs.readdirSync`,
+ * so the test runs in a file of its own.
+ */
+
+import {describe, it, expect, vi} from 'vitest';
+import * as path from 'node:path';
+import {fileURLToPath} from 'node:url';
+import {hook} from './hook.mjs';
+
+const reads = vi.hoisted(() => ({byFolder: new Map()}));
+
+vi.mock('node:fs', async importOriginal => {
+ /** @type {typeof import('node:fs')} */
+ const actual = await importOriginal();
+ /** @type {typeof actual.readdirSync} */
+ const readdirSync = /** @type {any} */ (
+ (/** @type {any} */ folder, /** @type {any} */ options) => {
+ const key = String(folder);
+ reads.byFolder.set(key, (reads.byFolder.get(key) ?? 0) + 1);
+ return actual.readdirSync(folder, options);
+ }
+ );
+ return {...actual, readdirSync, default: {...actual, readdirSync}};
+});
+
+// api/hook/ -> up 4 = repo root (has packages/core, which findCoreDir walks to).
+const REPO = path.resolve(
+ path.dirname(fileURLToPath(import.meta.url)),
+ '../../../..',
+);
+
+describe('one hook call', () => {
+ it('reads each folder once for the compact list of every hook', async () => {
+ reads.byFolder.clear();
+ const result = await hook(undefined, {
+ cwd: REPO,
+ list: true,
+ detail: 'compact',
+ });
+ expect(result.type).toBe('hook.list');
+ // The count sees the call's reads, so a list of no repeats means something.
+ expect(reads.byFolder.size).toBeGreaterThan(0);
+ const repeated = [...reads.byFolder].filter(([, count]) => count > 1);
+ expect(repeated).toEqual([]);
+ });
+});