An open source design system that's fully customizable and agent ready
TypeScript
perf(cli): gatherCandidates runs in its own listing-cache operation (#9943)
Christopher Chedeau2c90cc494e14
diff --git a/.changeset/cli-search-gather-listing-cache.md b/.changeset/cli-search-gather-listing-cache.mdnew file mode 100644index 0000000000..2fda4ce67f--- /dev/null+++ b/.changeset/cli-search-gather-listing-cache.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[chore] Internal: the search gather runs in its own listing-cache operation, so a caller that gathers candidates directly lists each folder about once, as `astryx search` already does. The command's results and output are unchanged. Not exported from the package entry point.++@vjeuxdiff --git a/packages/cli/api/search/_adapter.listings.test.mjs b/packages/cli/api/search/_adapter.listings.test.mjsnew file mode 100644index 0000000000..ca2fa0c75a--- /dev/null+++ b/packages/cli/api/search/_adapter.listings.test.mjs@@ -0,0 +1,66 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file Tests that a cold gather (_adapter.mjs) lists each folder about once+ * when its caller runs no listing-cache operation of its own, as the search+ * tests do. Run against the real @astryxdesign/core.+ */++import {describe, expect, it} from 'vitest';+import {createRequire, syncBuiltinESMExports} from 'node:module';+import * as path from 'node:path';+import {fileURLToPath} from 'node:url';+import {gatherCandidates} from './_adapter.mjs';++const REPO = path.resolve(+ path.dirname(fileURLToPath(import.meta.url)),+ '../../../..',+);++/** node:fs's module object, whose functions every import of it shares. */+const fs = createRequire(import.meta.url)('node:fs');++/**+ * Run `run` and count the folder listings it makes, by folder.+ * @param {() => Promise<unknown>} run+ * @returns {Promise<Map<string, number>>}+ */+async function countListings(run) {+ /** @type {Map<string, number>} */+ const listings = new Map();+ const readdirSync = fs.readdirSync;+ /**+ * @this {unknown}+ * @param {any} dir+ * @param {...any} rest+ */+ fs.readdirSync = function (dir, ...rest) {+ const folder = path.resolve(+ dir instanceof URL ? fileURLToPath(dir) : String(dir),+ );+ listings.set(folder, (listings.get(folder) ?? 0) + 1);+ return readdirSync.call(this, dir, ...rest);+ };+ // ESM imports of node:fs see the counting function too.+ syncBuiltinESMExports();+ try {+ await run();+ } finally {+ fs.readdirSync = readdirSync;+ syncBuiltinESMExports();+ }+ return listings;+}++describe('gatherCandidates', () => {+ it('lists each folder about once in a cold gather', async () => {+ const listings = await countListings(() => gatherCandidates({cwd: REPO}));+ const calls = [...listings.values()].reduce((sum, n) => sum + n, 0);+ // A few readers list a folder outside the cache, so a folder may be listed+ // twice.+ expect(+ calls,+ `${calls} listings of ${listings.size} folders`,+ ).toBeLessThanOrEqual(2 * listings.size);+ });+});diff --git a/packages/cli/api/search/_adapter.mjs b/packages/cli/api/search/_adapter.mjsindex 61faf58512..f93f11c451 100644--- a/packages/cli/api/search/_adapter.mjs+++ b/packages/cli/api/search/_adapter.mjs@@ -9,6 +9,7 @@import {readFileSync} from 'node:fs';import * as path from 'node:path';import {requireCoreDir} from '../../foundation/fs/paths.mjs';+import {withListingCache} from '../../foundation/fs/listing-cache.mjs';import {reusedWhileUnchanged,unchangedSince,@@ -37,12 +38,21 @@ export function requireSearchCoreDir(cwd) {}/**- * Gathers every candidate the query's domains hold.+ * Gathers every candidate the query's domains hold. The gather runs in one+ * listing-cache operation, so its discovery helpers list each folder once, as+ * in the CLI's searches. A caller that runs its own operation shares it.** @param {{cwd?: string, type?: string}} options* @returns {Promise<{all: Candidate[], components: Candidate[], searchedComponents: boolean, outside: boolean, coreSkipped: string | null}>}*/-export async function gatherCandidates(options) {+export function gatherCandidates(options) {+ return withListingCache(() => gatherDomains(options));+}++/**+ * @param {{cwd?: string, type?: string}} options+ */+async function gatherDomains(options) {const {cwd = process.cwd(), type} = options;// `astryx docs` reads docs without @astryxdesign/core, and `astryx theme// list` reads themes without it (bundled themes need no project), so a