@astryxdesign/cli 0.7.1-canary.7e441be → 0.7.1-canary.9550922
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.
- package/api/build/_adapter.d.mts +1 -0
- package/api/build/_adapter.mjs +7 -0
- package/api/build/build.mjs +4 -1
- package/api/search/search.mjs +12 -0
- package/foundation/discovery/component-discovery.mjs +27 -26
- package/foundation/discovery/hook-discovery.mjs +13 -12
- package/foundation/fs/listing-cache.d.mts +36 -0
- package/foundation/fs/listing-cache.mjs +95 -0
- package/foundation/fs/listing-cache.test.mjs +88 -0
- package/foundation/fs/paths.mjs +3 -2
- package/package.json +9 -9
package/api/build/_adapter.d.mts
CHANGED
|
@@ -66,6 +66,7 @@ export function isWeightsFile(file: any): file is import("./kit/weights.mjs").We
|
|
|
66
66
|
* @returns {string[]} Setup notes (empty when the template needs nothing).
|
|
67
67
|
*/
|
|
68
68
|
export function templateSetupNotes(template: PageTemplate, cwd: string): string[];
|
|
69
|
+
export { withListingCache } from "../../foundation/fs/listing-cache.mjs";
|
|
69
70
|
/**
|
|
70
71
|
* A page template the kit can recommend starting from.
|
|
71
72
|
*/
|
package/api/build/_adapter.mjs
CHANGED
|
@@ -23,6 +23,13 @@ import {componentKeywords} from '../search/search.mjs';
|
|
|
23
23
|
import {findCoreDir} from '../../foundation/fs/paths.mjs';
|
|
24
24
|
import {analyzeTemplateNeeds} from '../../foundation/discovery/template-needs.mjs';
|
|
25
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Runs one build with a single directory listing per path: the kit resolves
|
|
28
|
+
* every candidate's source and docs by walking the same tree, which does not
|
|
29
|
+
* change during the call.
|
|
30
|
+
*/
|
|
31
|
+
export {withListingCache} from '../../foundation/fs/listing-cache.mjs';
|
|
32
|
+
|
|
26
33
|
/**
|
|
27
34
|
* A page template the kit can recommend starting from.
|
|
28
35
|
* @typedef {object} PageTemplate
|
package/api/build/build.mjs
CHANGED
|
@@ -17,6 +17,7 @@ import {AstryxError} from '../error.mjs';
|
|
|
17
17
|
import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
|
|
18
18
|
import {buildHelp} from './help/help.mjs';
|
|
19
19
|
import {buildKit} from './kit/kit.mjs';
|
|
20
|
+
import {withListingCache} from './_adapter.mjs';
|
|
20
21
|
|
|
21
22
|
// Barrel: surface the leaves alongside the dispatcher for the fractal shape.
|
|
22
23
|
export {buildHelp, buildKit};
|
|
@@ -40,5 +41,7 @@ export async function build(query, options = {}) {
|
|
|
40
41
|
ERROR_CODES.ERR_INVALID_ARGUMENT,
|
|
41
42
|
);
|
|
42
43
|
}
|
|
43
|
-
|
|
44
|
+
// The kit resolves every candidate's source and docs by walking the same
|
|
45
|
+
// tree; read each directory once for this call.
|
|
46
|
+
return withListingCache(() => buildKit(query, options));
|
|
44
47
|
}
|
package/api/search/search.mjs
CHANGED
|
@@ -79,6 +79,7 @@
|
|
|
79
79
|
* queries they have nothing to do with.
|
|
80
80
|
*/
|
|
81
81
|
|
|
82
|
+
import {withListingCache} from '../../foundation/fs/listing-cache.mjs';
|
|
82
83
|
import {readDocView} from '../../foundation/doc-compiler/read.mjs';
|
|
83
84
|
import {findCoreDir} from '../../foundation/fs/paths.mjs';
|
|
84
85
|
import {
|
|
@@ -1769,6 +1770,17 @@ function toResult(c, score, reason, matchedTerms, queryTerms) {
|
|
|
1769
1770
|
* @returns {Promise<import('./search.type.mjs').SearchResponse>}
|
|
1770
1771
|
*/
|
|
1771
1772
|
export async function search(query, options = {}) {
|
|
1773
|
+
// Every candidate resolves its source and docs by walking the same tree;
|
|
1774
|
+
// read each directory once for this call.
|
|
1775
|
+
return withListingCache(() => searchOnce(query, options));
|
|
1776
|
+
}
|
|
1777
|
+
|
|
1778
|
+
/**
|
|
1779
|
+
* @param {string} query
|
|
1780
|
+
* @param {NonNullable<Parameters<typeof search>[1]>} options
|
|
1781
|
+
* @returns {ReturnType<typeof search>}
|
|
1782
|
+
*/
|
|
1783
|
+
async function searchOnce(query, options) {
|
|
1772
1784
|
const {cwd = process.cwd(), type, limit = 20} = options;
|
|
1773
1785
|
|
|
1774
1786
|
if (!query || !String(query).trim()) {
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import * as fs from 'node:fs';
|
|
8
8
|
import * as path from 'node:path';
|
|
9
|
+
import {listDir, listDirEntries, pathExists} from '../fs/listing-cache.mjs';
|
|
9
10
|
import {existsCaseExact} from '../fs/paths.mjs';
|
|
10
11
|
import {CORE_PROVIDER_ID} from '../identity/providers.mjs';
|
|
11
12
|
import {loadComponentDoc} from './component-loader.mjs';
|
|
@@ -52,8 +53,8 @@ function isComponentSourceFile(fileName, dirPath) {
|
|
|
52
53
|
if (BARE_COMPONENT_FILE_RE.test(fileName)) {
|
|
53
54
|
const base = fileName.replace(/\.tsx$/, '');
|
|
54
55
|
return (
|
|
55
|
-
|
|
56
|
-
|
|
56
|
+
pathExists(path.join(dirPath, `${base}.doc.mjs`)) ||
|
|
57
|
+
pathExists(path.join(dirPath, `XDS${base}.doc.mjs`))
|
|
57
58
|
);
|
|
58
59
|
}
|
|
59
60
|
return false;
|
|
@@ -132,8 +133,8 @@ export function discoverComponents(coreDir) {
|
|
|
132
133
|
function collectXDSFiles(dirPath) {
|
|
133
134
|
/** @type {string[]} */
|
|
134
135
|
const results = [];
|
|
135
|
-
if (!
|
|
136
|
-
const entries =
|
|
136
|
+
if (!pathExists(dirPath)) return results;
|
|
137
|
+
const entries = listDirEntries(dirPath);
|
|
137
138
|
for (const entry of entries) {
|
|
138
139
|
if (entry.isDirectory()) {
|
|
139
140
|
results.push(...collectXDSFiles(path.join(dirPath, entry.name)));
|
|
@@ -144,7 +145,7 @@ export function discoverComponents(coreDir) {
|
|
|
144
145
|
return results;
|
|
145
146
|
}
|
|
146
147
|
|
|
147
|
-
const topEntries =
|
|
148
|
+
const topEntries = listDirEntries(srcDir);
|
|
148
149
|
for (const entry of topEntries) {
|
|
149
150
|
if (!entry.isDirectory() || SKIP_DIRS.has(entry.name)) continue;
|
|
150
151
|
|
|
@@ -154,10 +155,10 @@ export function discoverComponents(coreDir) {
|
|
|
154
155
|
// Read the group from the directory's .doc.mjs file (if it exists)
|
|
155
156
|
// Check both {Name}.doc.mjs and Astryx{Name}.doc.mjs naming conventions
|
|
156
157
|
let docFile = path.join(dirPath, `${entry.name}.doc.mjs`);
|
|
157
|
-
if (!
|
|
158
|
+
if (!pathExists(docFile)) {
|
|
158
159
|
docFile = path.join(dirPath, `XDS${entry.name}.doc.mjs`);
|
|
159
160
|
}
|
|
160
|
-
const {group, hiddenComponents, hidden} =
|
|
161
|
+
const {group, hiddenComponents, hidden} = pathExists(docFile)
|
|
161
162
|
? readDocMeta(docFile)
|
|
162
163
|
: {group: null, hiddenComponents: new Set(), hidden: false};
|
|
163
164
|
|
|
@@ -171,10 +172,10 @@ export function discoverComponents(coreDir) {
|
|
|
171
172
|
// Check for a per-component doc file that overrides the directory group
|
|
172
173
|
let compGroup = group;
|
|
173
174
|
let compDoc = path.join(dirPath, `${componentName}.doc.mjs`);
|
|
174
|
-
if (!
|
|
175
|
+
if (!pathExists(compDoc)) {
|
|
175
176
|
compDoc = path.join(dirPath, `XDS${componentName}.doc.mjs`);
|
|
176
177
|
}
|
|
177
|
-
const hasComponentDoc =
|
|
178
|
+
const hasComponentDoc = pathExists(compDoc);
|
|
178
179
|
if (hasComponentDoc) {
|
|
179
180
|
const compMeta = readDocMeta(compDoc);
|
|
180
181
|
if (compMeta.group) compGroup = compMeta.group;
|
|
@@ -183,7 +184,7 @@ export function discoverComponents(coreDir) {
|
|
|
183
184
|
// Skip components without any .doc.mjs file (directory-level or
|
|
184
185
|
// component-level). They can't be documented, so surfacing them in
|
|
185
186
|
// the component list leads to broken docs-site links.
|
|
186
|
-
if (!
|
|
187
|
+
if (!pathExists(docFile) && !hasComponentDoc) continue;
|
|
187
188
|
|
|
188
189
|
if (!componentGroups.has(componentName)) {
|
|
189
190
|
componentGroups.set(componentName, compGroup);
|
|
@@ -251,7 +252,7 @@ export function findComponentReadme(coreDir, name) {
|
|
|
251
252
|
if (existsCaseExact(directXds, srcDir)) return directXds;
|
|
252
253
|
|
|
253
254
|
// Nested match: src/*/{name}/{Name}.doc.mjs or src/*/{name}/Astryx{Name}.doc.mjs
|
|
254
|
-
const entries =
|
|
255
|
+
const entries = listDirEntries(srcDir);
|
|
255
256
|
for (const entry of entries) {
|
|
256
257
|
if (!entry.isDirectory()) continue;
|
|
257
258
|
const nested = path.join(srcDir, entry.name, name, exactDoc);
|
|
@@ -275,7 +276,7 @@ export function findComponentReadme(coreDir, name) {
|
|
|
275
276
|
if (sourcePath) {
|
|
276
277
|
let dir = path.dirname(sourcePath);
|
|
277
278
|
while (dir.startsWith(srcDir)) {
|
|
278
|
-
const dirEntries =
|
|
279
|
+
const dirEntries = listDir(dir);
|
|
279
280
|
for (const f of dirEntries) {
|
|
280
281
|
if (f.endsWith('.doc.mjs')) return path.join(dir, f);
|
|
281
282
|
}
|
|
@@ -318,8 +319,8 @@ export function findComponentSource(coreDir, name) {
|
|
|
318
319
|
* @returns {string | null}
|
|
319
320
|
*/
|
|
320
321
|
function searchDir(dirPath) {
|
|
321
|
-
if (!
|
|
322
|
-
const entries =
|
|
322
|
+
if (!pathExists(dirPath)) return null;
|
|
323
|
+
const entries = listDirEntries(dirPath);
|
|
323
324
|
|
|
324
325
|
// Check for an exact match (prefixed or bare) first
|
|
325
326
|
for (const candidate of candidateFiles) {
|
|
@@ -345,7 +346,7 @@ export function findComponentSource(coreDir, name) {
|
|
|
345
346
|
}
|
|
346
347
|
|
|
347
348
|
// Search nested (component might be under a parent dir)
|
|
348
|
-
const entries =
|
|
349
|
+
const entries = listDirEntries(srcDir);
|
|
349
350
|
for (const entry of entries) {
|
|
350
351
|
if (!entry.isDirectory()) continue;
|
|
351
352
|
const nestedDir = path.join(srcDir, entry.name, name);
|
|
@@ -369,7 +370,7 @@ export function findComponentSource(coreDir, name) {
|
|
|
369
370
|
export function resolveImportPath(coreDir, componentName) {
|
|
370
371
|
const srcDir = path.join(coreDir, 'src');
|
|
371
372
|
const pkgPath = path.join(coreDir, 'package.json');
|
|
372
|
-
const pkg =
|
|
373
|
+
const pkg = pathExists(pkgPath)
|
|
373
374
|
? JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
|
|
374
375
|
: null;
|
|
375
376
|
|
|
@@ -522,14 +523,14 @@ function readPackageExports(packageDir) {
|
|
|
522
523
|
* @returns {Record<string, string[]>}
|
|
523
524
|
*/
|
|
524
525
|
export function discoverExternalComponentsGrouped(docsDir) {
|
|
525
|
-
if (!
|
|
526
|
+
if (!pathExists(docsDir)) return {};
|
|
526
527
|
|
|
527
528
|
/** @type {Map<string, string|null>} componentName → group */
|
|
528
529
|
const componentGroups = new Map();
|
|
529
530
|
|
|
530
531
|
/** @param {string} dirPath */
|
|
531
532
|
function scanDir(dirPath) {
|
|
532
|
-
const entries =
|
|
533
|
+
const entries = listDirEntries(dirPath);
|
|
533
534
|
for (const entry of entries) {
|
|
534
535
|
if (entry.name === 'node_modules' || entry.name === '__tests__') continue;
|
|
535
536
|
const fullPath = path.join(dirPath, entry.name);
|
|
@@ -593,7 +594,7 @@ export function discoverExternalComponentsGrouped(docsDir) {
|
|
|
593
594
|
* @returns {string | null}
|
|
594
595
|
*/
|
|
595
596
|
export function findExternalComponentDoc(docsDir, name) {
|
|
596
|
-
if (!
|
|
597
|
+
if (!pathExists(docsDir)) return null;
|
|
597
598
|
const target = `${name}.doc.mjs`;
|
|
598
599
|
|
|
599
600
|
/**
|
|
@@ -601,7 +602,7 @@ export function findExternalComponentDoc(docsDir, name) {
|
|
|
601
602
|
* @returns {string | null}
|
|
602
603
|
*/
|
|
603
604
|
function scanDir(dirPath) {
|
|
604
|
-
const entries =
|
|
605
|
+
const entries = listDirEntries(dirPath);
|
|
605
606
|
for (const entry of entries) {
|
|
606
607
|
if (entry.name === 'node_modules' || entry.name === '__tests__') continue;
|
|
607
608
|
const fullPath = path.join(dirPath, entry.name);
|
|
@@ -642,7 +643,7 @@ function integrationSourceForDoc(docPath) {
|
|
|
642
643
|
const dir = path.dirname(docPath);
|
|
643
644
|
const base = path.basename(docPath).replace(/\.doc\.(ts|mjs|js)$/, '');
|
|
644
645
|
const candidate = path.join(dir, `${base}.tsx`);
|
|
645
|
-
return
|
|
646
|
+
return pathExists(candidate) ? candidate : null;
|
|
646
647
|
}
|
|
647
648
|
|
|
648
649
|
/**
|
|
@@ -656,14 +657,14 @@ function integrationSourceForDoc(docPath) {
|
|
|
656
657
|
*/
|
|
657
658
|
export function discoverIntegrationComponents(integration) {
|
|
658
659
|
const componentsDir = integration?.components;
|
|
659
|
-
if (!componentsDir || !
|
|
660
|
+
if (!componentsDir || !pathExists(componentsDir)) return [];
|
|
660
661
|
|
|
661
662
|
/** @type {Map<string, {name: string, package: string, docPath: string, sourcePath: string|null, issuesUrl: string|undefined, group: string|null}>} */
|
|
662
663
|
const byName = new Map();
|
|
663
664
|
|
|
664
665
|
/** @param {string} dirPath */
|
|
665
666
|
function scanDir(dirPath) {
|
|
666
|
-
const entries =
|
|
667
|
+
const entries = listDirEntries(dirPath);
|
|
667
668
|
for (const entry of entries) {
|
|
668
669
|
if (entry.name === 'node_modules' || entry.name === '__tests__') continue;
|
|
669
670
|
const fullPath = path.join(dirPath, entry.name);
|
|
@@ -757,18 +758,18 @@ export async function discoverValidIntegrationComponents(integration) {
|
|
|
757
758
|
*/
|
|
758
759
|
export function findIntegrationComponentDoc(integration, name) {
|
|
759
760
|
const componentsDir = integration?.components;
|
|
760
|
-
if (!componentsDir || !
|
|
761
|
+
if (!componentsDir || !pathExists(componentsDir)) return null;
|
|
761
762
|
|
|
762
763
|
/**
|
|
763
764
|
* @param {string} dirPath
|
|
764
765
|
* @returns {string | null}
|
|
765
766
|
*/
|
|
766
767
|
function scanDir(dirPath) {
|
|
767
|
-
const entries =
|
|
768
|
+
const entries = listDirEntries(dirPath);
|
|
768
769
|
// Exact same-stem match (precedence order) first in this dir.
|
|
769
770
|
for (const suffix of INTEGRATION_DOC_SUFFIXES) {
|
|
770
771
|
const candidate = path.join(dirPath, `${name}${suffix}`);
|
|
771
|
-
if (
|
|
772
|
+
if (pathExists(candidate)) return candidate;
|
|
772
773
|
}
|
|
773
774
|
for (const entry of entries) {
|
|
774
775
|
if (entry.name === 'node_modules' || entry.name === '__tests__') continue;
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import * as fs from 'node:fs';
|
|
8
8
|
import * as path from 'node:path';
|
|
9
|
+
import {listDir, listDirEntries, pathExists} from '../fs/listing-cache.mjs';
|
|
9
10
|
import {existsCaseExact} from '../fs/paths.mjs';
|
|
10
11
|
import {levenshteinDistance} from '../text/string-utils.mjs';
|
|
11
12
|
|
|
@@ -57,8 +58,8 @@ export function discoverHooks(coreDir) {
|
|
|
57
58
|
const hookCategories = new Map();
|
|
58
59
|
|
|
59
60
|
// 1. Scan src/hooks/ for *.doc.mjs files
|
|
60
|
-
if (
|
|
61
|
-
const entries =
|
|
61
|
+
if (pathExists(hooksDir)) {
|
|
62
|
+
const entries = listDirEntries(hooksDir);
|
|
62
63
|
for (const entry of entries) {
|
|
63
64
|
if (!entry.isFile()) continue;
|
|
64
65
|
if (!entry.name.endsWith('.doc.mjs')) continue;
|
|
@@ -70,8 +71,8 @@ export function discoverHooks(coreDir) {
|
|
|
70
71
|
}
|
|
71
72
|
|
|
72
73
|
// 2. Scan ALL component directories for files matching use*.doc.mjs
|
|
73
|
-
if (
|
|
74
|
-
const topEntries =
|
|
74
|
+
if (pathExists(srcDir)) {
|
|
75
|
+
const topEntries = listDirEntries(srcDir);
|
|
75
76
|
for (const entry of topEntries) {
|
|
76
77
|
if (!entry.isDirectory() || SKIP_DIRS.has(entry.name) || entry.name === 'hooks') continue;
|
|
77
78
|
const dirPath = path.join(srcDir, entry.name);
|
|
@@ -114,8 +115,8 @@ export function discoverHooks(coreDir) {
|
|
|
114
115
|
* @param {Map<string, string>} hookCategories
|
|
115
116
|
*/
|
|
116
117
|
function scanDirForHookDocs(dirPath, hookCategories) {
|
|
117
|
-
if (!
|
|
118
|
-
const entries =
|
|
118
|
+
if (!pathExists(dirPath)) return;
|
|
119
|
+
const entries = listDirEntries(dirPath);
|
|
119
120
|
for (const entry of entries) {
|
|
120
121
|
if (entry.isDirectory() && !SKIP_DIRS.has(entry.name)) {
|
|
121
122
|
scanDirForHookDocs(path.join(dirPath, entry.name), hookCategories);
|
|
@@ -162,8 +163,8 @@ export function findHookDoc(coreDir, name) {
|
|
|
162
163
|
}
|
|
163
164
|
|
|
164
165
|
// 2. Case-insensitive search in hooks directory
|
|
165
|
-
if (
|
|
166
|
-
const entries =
|
|
166
|
+
if (pathExists(hooksDir)) {
|
|
167
|
+
const entries = listDir(hooksDir);
|
|
167
168
|
for (const entry of entries) {
|
|
168
169
|
if (!entry.endsWith('.doc.mjs')) continue;
|
|
169
170
|
const hookName = hookNameFromFile(entry);
|
|
@@ -175,8 +176,8 @@ export function findHookDoc(coreDir, name) {
|
|
|
175
176
|
}
|
|
176
177
|
|
|
177
178
|
// 3. Check all component dirs for {name}.doc.mjs or use{Name}.doc.mjs
|
|
178
|
-
if (
|
|
179
|
-
const topEntries =
|
|
179
|
+
if (pathExists(srcDir)) {
|
|
180
|
+
const topEntries = listDirEntries(srcDir);
|
|
180
181
|
for (const entry of topEntries) {
|
|
181
182
|
if (!entry.isDirectory() || SKIP_DIRS.has(entry.name) || entry.name === 'hooks') continue;
|
|
182
183
|
const found = searchDirForHookDoc(path.join(srcDir, entry.name), uniqueCandidates);
|
|
@@ -213,8 +214,8 @@ export function findHookDoc(coreDir, name) {
|
|
|
213
214
|
* @returns {string | null}
|
|
214
215
|
*/
|
|
215
216
|
function searchDirForHookDoc(dirPath, candidates) {
|
|
216
|
-
if (!
|
|
217
|
-
const entries =
|
|
217
|
+
if (!pathExists(dirPath)) return null;
|
|
218
|
+
const entries = listDirEntries(dirPath);
|
|
218
219
|
for (const entry of entries) {
|
|
219
220
|
if (entry.isDirectory() && !SKIP_DIRS.has(entry.name)) {
|
|
220
221
|
const found = searchDirForHookDoc(path.join(dirPath, entry.name), candidates);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// @generated by scripts/sync-api-types.mjs from the JSDoc in foundation/**/*.mjs.
|
|
2
|
+
// DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Run `fn` with a listing cache. A nested call shares the outer cache. Only
|
|
6
|
+
* wrap operations that do not change the files they list.
|
|
7
|
+
* @template T
|
|
8
|
+
* @param {() => T} fn
|
|
9
|
+
* @returns {T}
|
|
10
|
+
*/
|
|
11
|
+
export function withListingCache<T>(fn: () => T): T;
|
|
12
|
+
/**
|
|
13
|
+
* `fs.readdirSync(dir)`, read once per operation. Returns a fresh array, so a
|
|
14
|
+
* caller that sorts it cannot reorder another caller's listing.
|
|
15
|
+
* @param {string} dir
|
|
16
|
+
* @returns {string[]}
|
|
17
|
+
*/
|
|
18
|
+
export function listDir(dir: string): string[];
|
|
19
|
+
/**
|
|
20
|
+
* `fs.readdirSync(dir, {withFileTypes: true})`, read once per operation.
|
|
21
|
+
* @param {string} dir
|
|
22
|
+
* @returns {fs.Dirent[]}
|
|
23
|
+
*/
|
|
24
|
+
export function listDirEntries(dir: string): fs.Dirent[];
|
|
25
|
+
/**
|
|
26
|
+
* `fs.existsSync(target)`, probed once per operation.
|
|
27
|
+
* @param {string} target
|
|
28
|
+
* @returns {boolean}
|
|
29
|
+
*/
|
|
30
|
+
export function pathExists(target: string): boolean;
|
|
31
|
+
export type ListingCache = {
|
|
32
|
+
names: Map<string, string[]>;
|
|
33
|
+
entries: Map<string, fs.Dirent[]>;
|
|
34
|
+
exists: Map<string, boolean>;
|
|
35
|
+
};
|
|
36
|
+
import * as fs from 'node:fs';
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file One directory listing per path for the duration of one read-only
|
|
5
|
+
* operation.
|
|
6
|
+
*
|
|
7
|
+
* @input A read-only operation (`withListingCache(fn)`), and directory paths
|
|
8
|
+
* its discovery helpers list or probe.
|
|
9
|
+
* @output The same answers `fs.readdirSync` / `fs.existsSync` give, read from
|
|
10
|
+
* disk once per path while the operation runs.
|
|
11
|
+
* @position Used by the name resolvers in foundation/discovery and by
|
|
12
|
+
* `existsCaseExact`. A search or build resolves ~160 components and their
|
|
13
|
+
* hooks, and each resolution walks the same source tree again; inside one
|
|
14
|
+
* operation the tree does not change, so each directory is read once.
|
|
15
|
+
* Outside `withListingCache` every helper reads the disk directly, exactly
|
|
16
|
+
* as before, so long-lived callers never see a stale listing.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import {AsyncLocalStorage} from 'node:async_hooks';
|
|
20
|
+
import * as fs from 'node:fs';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @typedef {{
|
|
24
|
+
* names: Map<string, string[]>,
|
|
25
|
+
* entries: Map<string, fs.Dirent[]>,
|
|
26
|
+
* exists: Map<string, boolean>,
|
|
27
|
+
* }} ListingCache
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/** @type {AsyncLocalStorage<ListingCache>} */
|
|
31
|
+
const scope = new AsyncLocalStorage();
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Run `fn` with a listing cache. A nested call shares the outer cache. Only
|
|
35
|
+
* wrap operations that do not change the files they list.
|
|
36
|
+
* @template T
|
|
37
|
+
* @param {() => T} fn
|
|
38
|
+
* @returns {T}
|
|
39
|
+
*/
|
|
40
|
+
export function withListingCache(fn) {
|
|
41
|
+
if (scope.getStore()) return fn();
|
|
42
|
+
return scope.run(
|
|
43
|
+
{names: new Map(), entries: new Map(), exists: new Map()},
|
|
44
|
+
fn,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* `fs.readdirSync(dir)`, read once per operation. Returns a fresh array, so a
|
|
50
|
+
* caller that sorts it cannot reorder another caller's listing.
|
|
51
|
+
* @param {string} dir
|
|
52
|
+
* @returns {string[]}
|
|
53
|
+
*/
|
|
54
|
+
export function listDir(dir) {
|
|
55
|
+
const cache = scope.getStore();
|
|
56
|
+
if (!cache) return fs.readdirSync(dir);
|
|
57
|
+
let names = cache.names.get(dir);
|
|
58
|
+
if (!names) {
|
|
59
|
+
names = fs.readdirSync(dir);
|
|
60
|
+
cache.names.set(dir, names);
|
|
61
|
+
}
|
|
62
|
+
return names.slice();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* `fs.readdirSync(dir, {withFileTypes: true})`, read once per operation.
|
|
67
|
+
* @param {string} dir
|
|
68
|
+
* @returns {fs.Dirent[]}
|
|
69
|
+
*/
|
|
70
|
+
export function listDirEntries(dir) {
|
|
71
|
+
const cache = scope.getStore();
|
|
72
|
+
if (!cache) return fs.readdirSync(dir, {withFileTypes: true});
|
|
73
|
+
let entries = cache.entries.get(dir);
|
|
74
|
+
if (!entries) {
|
|
75
|
+
entries = fs.readdirSync(dir, {withFileTypes: true});
|
|
76
|
+
cache.entries.set(dir, entries);
|
|
77
|
+
}
|
|
78
|
+
return entries.slice();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* `fs.existsSync(target)`, probed once per operation.
|
|
83
|
+
* @param {string} target
|
|
84
|
+
* @returns {boolean}
|
|
85
|
+
*/
|
|
86
|
+
export function pathExists(target) {
|
|
87
|
+
const cache = scope.getStore();
|
|
88
|
+
if (!cache) return fs.existsSync(target);
|
|
89
|
+
let exists = cache.exists.get(target);
|
|
90
|
+
if (exists === undefined) {
|
|
91
|
+
exists = fs.existsSync(target);
|
|
92
|
+
cache.exists.set(target, exists);
|
|
93
|
+
}
|
|
94
|
+
return exists;
|
|
95
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
|
|
4
|
+
import * as fs from 'node:fs';
|
|
5
|
+
import * as os from 'node:os';
|
|
6
|
+
import * as path from 'node:path';
|
|
7
|
+
import {
|
|
8
|
+
listDir,
|
|
9
|
+
listDirEntries,
|
|
10
|
+
pathExists,
|
|
11
|
+
withListingCache,
|
|
12
|
+
} from './listing-cache.mjs';
|
|
13
|
+
import {existsCaseExact} from './paths.mjs';
|
|
14
|
+
|
|
15
|
+
let dir;
|
|
16
|
+
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-listing-cache-'));
|
|
19
|
+
fs.writeFileSync(path.join(dir, 'b.txt'), '');
|
|
20
|
+
fs.writeFileSync(path.join(dir, 'a.txt'), '');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
afterEach(() => {
|
|
24
|
+
fs.rmSync(dir, {recursive: true, force: true});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
describe('listing cache', () => {
|
|
28
|
+
it('reads the disk directly outside an operation', () => {
|
|
29
|
+
expect(listDir(dir).sort()).toEqual(['a.txt', 'b.txt']);
|
|
30
|
+
fs.writeFileSync(path.join(dir, 'c.txt'), '');
|
|
31
|
+
expect(listDir(dir).sort()).toEqual(['a.txt', 'b.txt', 'c.txt']);
|
|
32
|
+
expect(pathExists(path.join(dir, 'c.txt'))).toBe(true);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('answers from one read for the rest of an operation', async () => {
|
|
36
|
+
await withListingCache(async () => {
|
|
37
|
+
expect(listDir(dir).sort()).toEqual(['a.txt', 'b.txt']);
|
|
38
|
+
expect(pathExists(path.join(dir, 'c.txt'))).toBe(false);
|
|
39
|
+
fs.writeFileSync(path.join(dir, 'c.txt'), '');
|
|
40
|
+
await new Promise(resolve => setTimeout(resolve, 0));
|
|
41
|
+
expect(listDir(dir).sort()).toEqual(['a.txt', 'b.txt']);
|
|
42
|
+
expect(pathExists(path.join(dir, 'c.txt'))).toBe(false);
|
|
43
|
+
expect(
|
|
44
|
+
listDirEntries(dir)
|
|
45
|
+
.map(entry => entry.name)
|
|
46
|
+
.sort(),
|
|
47
|
+
).toEqual(['a.txt', 'b.txt', 'c.txt']);
|
|
48
|
+
});
|
|
49
|
+
// The operation's snapshot does not outlive it.
|
|
50
|
+
expect(listDir(dir).sort()).toEqual(['a.txt', 'b.txt', 'c.txt']);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('shares one cache with nested operations', () => {
|
|
54
|
+
withListingCache(() => {
|
|
55
|
+
listDir(dir);
|
|
56
|
+
fs.writeFileSync(path.join(dir, 'c.txt'), '');
|
|
57
|
+
withListingCache(() => {
|
|
58
|
+
expect(listDir(dir).sort()).toEqual(['a.txt', 'b.txt']);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('hands every caller its own array', () => {
|
|
64
|
+
withListingCache(() => {
|
|
65
|
+
listDir(dir).reverse().push('mutated');
|
|
66
|
+
expect(listDir(dir)).not.toContain('mutated');
|
|
67
|
+
listDirEntries(dir).length = 0;
|
|
68
|
+
expect(listDirEntries(dir)).toHaveLength(2);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('does not remember a failed read', () => {
|
|
73
|
+
const missing = path.join(dir, 'later');
|
|
74
|
+
withListingCache(() => {
|
|
75
|
+
expect(() => listDir(missing)).toThrow();
|
|
76
|
+
fs.mkdirSync(missing);
|
|
77
|
+
expect(listDir(missing)).toEqual([]);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('keeps existsCaseExact exact-case inside an operation', () => {
|
|
82
|
+
fs.mkdirSync(path.join(dir, 'Button'));
|
|
83
|
+
withListingCache(() => {
|
|
84
|
+
expect(existsCaseExact(path.join(dir, 'Button'), dir)).toBe(true);
|
|
85
|
+
expect(existsCaseExact(path.join(dir, 'button'), dir)).toBe(false);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
});
|
package/foundation/fs/paths.mjs
CHANGED
|
@@ -10,6 +10,7 @@ import * as fs from 'node:fs';
|
|
|
10
10
|
import {createRequire} from 'node:module';
|
|
11
11
|
import * as path from 'node:path';
|
|
12
12
|
import {fileURLToPath} from 'node:url';
|
|
13
|
+
import {listDir, pathExists} from './listing-cache.mjs';
|
|
13
14
|
|
|
14
15
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
15
16
|
|
|
@@ -231,7 +232,7 @@ export function listComponents(coreDir) {
|
|
|
231
232
|
* @returns {boolean}
|
|
232
233
|
*/
|
|
233
234
|
export function existsCaseExact(targetPath, rootDir) {
|
|
234
|
-
if (!
|
|
235
|
+
if (!pathExists(targetPath)) return false;
|
|
235
236
|
|
|
236
237
|
const rel = path.relative(rootDir, targetPath);
|
|
237
238
|
if (rel === '') return true;
|
|
@@ -241,7 +242,7 @@ export function existsCaseExact(targetPath, rootDir) {
|
|
|
241
242
|
for (const segment of rel.split(path.sep)) {
|
|
242
243
|
let entries;
|
|
243
244
|
try {
|
|
244
|
-
entries =
|
|
245
|
+
entries = listDir(dir);
|
|
245
246
|
} catch {
|
|
246
247
|
return false;
|
|
247
248
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astryxdesign/cli",
|
|
3
|
-
"version": "0.7.1-canary.
|
|
3
|
+
"version": "0.7.1-canary.9550922",
|
|
4
4
|
"displayName": "CLI",
|
|
5
5
|
"description": "Scaffold projects, browse templates, generate themes, and get agent-ready docs from the command line.",
|
|
6
6
|
"author": "Meta Open Source",
|
|
@@ -101,10 +101,10 @@
|
|
|
101
101
|
"zod": "^4.4.3"
|
|
102
102
|
},
|
|
103
103
|
"peerDependencies": {
|
|
104
|
-
"@astryxdesign/charts": "0.7.1-canary.
|
|
105
|
-
"@astryxdesign/core": "0.7.1-canary.
|
|
106
|
-
"@astryxdesign/lab": "0.7.1-canary.
|
|
107
|
-
"@astryxdesign/theme-neutral": "0.7.1-canary.
|
|
104
|
+
"@astryxdesign/charts": "0.7.1-canary.9550922",
|
|
105
|
+
"@astryxdesign/core": "0.7.1-canary.9550922",
|
|
106
|
+
"@astryxdesign/lab": "0.7.1-canary.9550922",
|
|
107
|
+
"@astryxdesign/theme-neutral": "0.7.1-canary.9550922"
|
|
108
108
|
},
|
|
109
109
|
"peerDependenciesMeta": {
|
|
110
110
|
"@astryxdesign/charts": {
|
|
@@ -121,10 +121,10 @@
|
|
|
121
121
|
}
|
|
122
122
|
},
|
|
123
123
|
"devDependencies": {
|
|
124
|
-
"@astryxdesign/charts": "0.7.1-canary.
|
|
125
|
-
"@astryxdesign/core": "0.7.1-canary.
|
|
126
|
-
"@astryxdesign/lab": "0.7.1-canary.
|
|
127
|
-
"@astryxdesign/theme-neutral": "0.7.1-canary.
|
|
124
|
+
"@astryxdesign/charts": "0.7.1-canary.9550922",
|
|
125
|
+
"@astryxdesign/core": "0.7.1-canary.9550922",
|
|
126
|
+
"@astryxdesign/lab": "0.7.1-canary.9550922",
|
|
127
|
+
"@astryxdesign/theme-neutral": "0.7.1-canary.9550922",
|
|
128
128
|
"@heroicons/react": "^2.2.0",
|
|
129
129
|
"@stylexjs/stylex": "^0.19.0",
|
|
130
130
|
"@types/babel__core": "^7.20.5",
|