@astryxdesign/cli 0.7.1-canary.ff40b78 → 0.7.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.
@@ -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 (fs.existsSync(hooksDir)) {
61
- const entries = fs.readdirSync(hooksDir, {withFileTypes: true});
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 (fs.existsSync(srcDir)) {
74
- const topEntries = fs.readdirSync(srcDir, {withFileTypes: true});
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 (!fs.existsSync(dirPath)) return;
118
- const entries = fs.readdirSync(dirPath, {withFileTypes: true});
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 (fs.existsSync(hooksDir)) {
166
- const entries = fs.readdirSync(hooksDir);
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 (fs.existsSync(srcDir)) {
179
- const topEntries = fs.readdirSync(srcDir, {withFileTypes: true});
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 (!fs.existsSync(dirPath)) return null;
217
- const entries = fs.readdirSync(dirPath, {withFileTypes: true});
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
+ });
@@ -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 (!fs.existsSync(targetPath)) return false;
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 = fs.readdirSync(dir);
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.ff40b78",
3
+ "version": "0.7.1",
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.ff40b78",
105
- "@astryxdesign/core": "0.7.1-canary.ff40b78",
106
- "@astryxdesign/lab": "0.7.1-canary.ff40b78",
107
- "@astryxdesign/theme-neutral": "0.7.1-canary.ff40b78"
104
+ "@astryxdesign/charts": "*",
105
+ "@astryxdesign/core": "*",
106
+ "@astryxdesign/lab": "*",
107
+ "@astryxdesign/theme-neutral": "*"
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.ff40b78",
125
- "@astryxdesign/core": "0.7.1-canary.ff40b78",
126
- "@astryxdesign/lab": "0.7.1-canary.ff40b78",
127
- "@astryxdesign/theme-neutral": "0.7.1-canary.ff40b78",
124
+ "@astryxdesign/charts": "*",
125
+ "@astryxdesign/core": "*",
126
+ "@astryxdesign/lab": "*",
127
+ "@astryxdesign/theme-neutral": "*",
128
128
  "@heroicons/react": "^2.2.0",
129
129
  "@stylexjs/stylex": "^0.19.0",
130
130
  "@types/babel__core": "^7.20.5",