@astryxdesign/cli 0.7.7-canary.550ff21 → 0.7.7-canary.58c5b8a
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/integration/authoring-checks.mjs +31 -0
- package/api/integration/check/check.mjs +8 -3
- package/assets/discover/official-packages.json +0 -4
- package/assets/templates/blocks/components/Hooks/useGridFocusHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Hooks/useGridFocusHookUsage.tsx +86 -0
- package/assets/templates/blocks/components/Hooks/useOverflowHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Hooks/useOverflowHookUsage.tsx +89 -0
- package/assets/templates/blocks/components/Hooks/usePressFeedbackHookUsage.tsx +24 -21
- package/assets/templates/blocks/components/Outline/useOutlineFromMarkdownHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Outline/useOutlineFromMarkdownHookUsage.tsx +69 -0
- package/assets/templates/blocks/components/Selector/useComboboxHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Selector/useComboboxHookUsage.tsx +113 -0
- package/assets/templates/blocks/components/createAnimator/AnimatorPlayingCardsVanilla.doc.mjs +14 -0
- package/assets/templates/blocks/components/createAnimator/AnimatorPlayingCardsVanilla.tsx +199 -0
- package/assets/templates/blocks/components/useAnimator/AnimatorPlayingCards.doc.mjs +21 -0
- package/assets/templates/blocks/components/useAnimator/AnimatorPlayingCards.tsx +210 -0
- package/foundation/discovery/component-discovery.mjs +26 -6
- package/foundation/discovery/doc-types.mjs +4 -2
- package/foundation/discovery/prop-drift.mjs +6 -0
- package/foundation/discovery/source-file-cache.d.mts +15 -0
- package/foundation/discovery/source-file-cache.mjs +140 -0
- package/foundation/discovery/template-adapter.d.mts +7 -3
- package/foundation/discovery/template-adapter.mjs +150 -10
- package/foundation/fs/listing-cache.d.mts +15 -0
- package/foundation/fs/listing-cache.mjs +50 -1
- package/foundation/fs/paths.mjs +50 -5
- package/package.json +9 -9
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file source-file-cache.mjs
|
|
5
|
+
* @input A TypeScript compiler host, the project's own `typescript`, and the
|
|
6
|
+
* compiler options its programs use
|
|
7
|
+
* @output `keepSourceFiles`: the host's `getSourceFile`, keeping each parsed
|
|
8
|
+
* file while its stamp (`fileStamp`) is the same, for every program in this
|
|
9
|
+
* process whose options parse and bind a file alike.
|
|
10
|
+
* @position The props comparison (./prop-drift.mjs) and the doc types check
|
|
11
|
+
* (./doc-types.mjs) build a program for every check, and each parsed the
|
|
12
|
+
* same declarations again: the TypeScript library's, React's, the
|
|
13
|
+
* authoring types. A process that checks more than once (a test run, a
|
|
14
|
+
* long-running caller, a check over several roots) now parses each once.
|
|
15
|
+
* TypeScript's language service shares parsed files between programs the
|
|
16
|
+
* same way; from 5.3 a program keeps what it resolved for a file itself, not
|
|
17
|
+
* on the file, which is what makes sharing them safe across programs.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import path from 'node:path';
|
|
21
|
+
import {fileStamp, recordContentRead} from '../fs/read-record.mjs';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The files kept, per TypeScript and per how the options parse and bind a
|
|
25
|
+
* file (`settingsKey`), the most recently used options last.
|
|
26
|
+
* @type {WeakMap<object, Map<string, Map<string, {stamp: string, sourceFile: any}>>>}
|
|
27
|
+
*/
|
|
28
|
+
const kept = new WeakMap();
|
|
29
|
+
|
|
30
|
+
/** The most files kept for one key; the oldest go first. */
|
|
31
|
+
const MAX_FILES = 5000;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The most keys kept per TypeScript; the least recently used go first. Each
|
|
35
|
+
* holds its own parse of the TypeScript library's declarations, the largest
|
|
36
|
+
* files a program reads.
|
|
37
|
+
*/
|
|
38
|
+
const MAX_KEYS = 8;
|
|
39
|
+
|
|
40
|
+
/** @type {WeakMap<object, any>} one document registry per TypeScript */
|
|
41
|
+
const registries = new WeakMap();
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* What in `options` decides how a file parses and binds: the key TypeScript's
|
|
45
|
+
* language service shares parsed files by (its document registry's), and
|
|
46
|
+
* whether a script binds in strict mode, which that key reads only from an
|
|
47
|
+
* explicit `alwaysStrict`. Options that only resolve or check (`paths`,
|
|
48
|
+
* `types`, `lib`, the strict checks) do not change it.
|
|
49
|
+
* @param {any} ts
|
|
50
|
+
* @param {any} options
|
|
51
|
+
* @returns {string}
|
|
52
|
+
*/
|
|
53
|
+
function settingsKey(ts, options) {
|
|
54
|
+
let registry = registries.get(ts);
|
|
55
|
+
if (registry == null) {
|
|
56
|
+
registry = ts.createDocumentRegistry();
|
|
57
|
+
registries.set(ts, registry);
|
|
58
|
+
}
|
|
59
|
+
const strict =
|
|
60
|
+
typeof ts.getStrictOptionValue === 'function'
|
|
61
|
+
? ts.getStrictOptionValue(options, 'alwaysStrict')
|
|
62
|
+
: (options.alwaysStrict ?? options.strict ?? false);
|
|
63
|
+
return `${registry.getKeyForCompilationSettings(options)}|${strict ? 1 : 0}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Whether this TypeScript keeps a program's module resolutions on the program
|
|
68
|
+
* (5.3 and later) rather than on the parsed files it shares.
|
|
69
|
+
* @param {any} ts
|
|
70
|
+
*/
|
|
71
|
+
function sharesSafely(ts) {
|
|
72
|
+
const [major, minor] = String(ts.versionMajorMinor ?? '0.0')
|
|
73
|
+
.split('.')
|
|
74
|
+
.map(Number);
|
|
75
|
+
return major > 5 || (major === 5 && minor >= 3);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* `host.getSourceFile`, keeping each file it parses while the file's stamp is
|
|
80
|
+
* the same, for every program whose options parse and bind a file as
|
|
81
|
+
* `options` do. A kept file still counts as read by the running operation
|
|
82
|
+
* (`recordContentRead`), as a parse would. On a TypeScript older than 5.3 the
|
|
83
|
+
* host's own `getSourceFile` is returned.
|
|
84
|
+
* @param {any} ts the project's own `typescript`
|
|
85
|
+
* @param {any} host
|
|
86
|
+
* @param {any} options the compiler options of the programs that use it
|
|
87
|
+
* @returns {(fileName: string, languageVersionOrOptions: any, onError?: any, shouldCreate?: boolean) => any}
|
|
88
|
+
*/
|
|
89
|
+
export function keepSourceFiles(ts, host, options) {
|
|
90
|
+
const getSourceFile = host.getSourceFile.bind(host);
|
|
91
|
+
if (!sharesSafely(ts)) return getSourceFile;
|
|
92
|
+
let bySettings = kept.get(ts);
|
|
93
|
+
if (bySettings == null) {
|
|
94
|
+
bySettings = new Map();
|
|
95
|
+
kept.set(ts, bySettings);
|
|
96
|
+
}
|
|
97
|
+
const settings = settingsKey(ts, options);
|
|
98
|
+
const byFile = bySettings.get(settings) ?? new Map();
|
|
99
|
+
// Most recently used last; past MAX_KEYS, the least recently used go.
|
|
100
|
+
bySettings.delete(settings);
|
|
101
|
+
bySettings.set(settings, byFile);
|
|
102
|
+
if (bySettings.size > MAX_KEYS) {
|
|
103
|
+
bySettings.delete(/** @type {string} */ (bySettings.keys().next().value));
|
|
104
|
+
}
|
|
105
|
+
return (fileName, languageVersionOrOptions, onError, shouldCreate) => {
|
|
106
|
+
// How the file parses: its language version and, from TypeScript 5.0,
|
|
107
|
+
// its module format and JSDoc parsing (the module detection the options
|
|
108
|
+
// give is in `settings`).
|
|
109
|
+
const parse =
|
|
110
|
+
languageVersionOrOptions != null &&
|
|
111
|
+
typeof languageVersionOrOptions === 'object'
|
|
112
|
+
? [
|
|
113
|
+
languageVersionOrOptions.languageVersion,
|
|
114
|
+
languageVersionOrOptions.impliedNodeFormat,
|
|
115
|
+
languageVersionOrOptions.jsDocParsingMode,
|
|
116
|
+
]
|
|
117
|
+
: [languageVersionOrOptions];
|
|
118
|
+
const key = `${path.resolve(fileName)}\0${JSON.stringify(parse)}`;
|
|
119
|
+
const stamp = fileStamp(fileName);
|
|
120
|
+
const seen = byFile.get(key);
|
|
121
|
+
if (seen != null && stamp !== '-' && seen.stamp === stamp) {
|
|
122
|
+
recordContentRead(fileName);
|
|
123
|
+
return seen.sourceFile;
|
|
124
|
+
}
|
|
125
|
+
const sourceFile = getSourceFile(
|
|
126
|
+
fileName,
|
|
127
|
+
languageVersionOrOptions,
|
|
128
|
+
onError,
|
|
129
|
+
shouldCreate,
|
|
130
|
+
);
|
|
131
|
+
byFile.delete(key);
|
|
132
|
+
if (sourceFile != null && stamp !== '-') {
|
|
133
|
+
byFile.set(key, {stamp, sourceFile});
|
|
134
|
+
if (byFile.size > MAX_FILES) {
|
|
135
|
+
byFile.delete(/** @type {string} */ (byFile.keys().next().value));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return sourceFile;
|
|
139
|
+
};
|
|
140
|
+
}
|
|
@@ -69,7 +69,11 @@ export function replaceDemoMedia(source: string, { videoPlaceholder }?: {
|
|
|
69
69
|
/**
|
|
70
70
|
* Replace each JSX element imported from {@link DEMO_CONTENT_MODULE} with a
|
|
71
71
|
* generic `<Text>` placeholder and remove that import, so emitted source never
|
|
72
|
-
* depends on content only Astryx's previews provide.
|
|
72
|
+
* depends on content only Astryx's previews provide. An element that wraps one
|
|
73
|
+
* element, such as a hint shown under a demo, is removed instead: its child
|
|
74
|
+
* takes its place, with the indentation the child would have had without it.
|
|
75
|
+
* `demoContentReplaced` counts the placeholders, because only they need the
|
|
76
|
+
* consumer's own content.
|
|
73
77
|
*
|
|
74
78
|
* Every check resolves a name to its binding through the module's scope, so a
|
|
75
79
|
* local declaration or parameter with the same name never counts as the same
|
|
@@ -79,12 +83,12 @@ export function replaceDemoMedia(source: string, { videoPlaceholder }?: {
|
|
|
79
83
|
* writes the name `Text`.
|
|
80
84
|
*
|
|
81
85
|
* Only a plain static use is replaced: a named import used only as a JSX tag
|
|
82
|
-
* whose children
|
|
86
|
+
* whose children are whitespace or one element, and whose props are literal values or
|
|
83
87
|
* read only bindings that code outside the replaced elements still reads. So
|
|
84
88
|
* `faceDown={!isOpen}` is safe when the same `isOpen` is also read elsewhere.
|
|
85
89
|
* Anything else (a namespace or type import, a re-export, a dynamic import, the
|
|
86
90
|
* element used as a value or a type, a spread or computed prop, a prop whose
|
|
87
|
-
* binding nothing else reads, children) throws, naming the element, rather than
|
|
91
|
+
* binding nothing else reads, other children) throws, naming the element, rather than
|
|
88
92
|
* leaving a reference behind, leaving a binding unused, or dropping code the
|
|
89
93
|
* rest of the file relies on. Source that never names the module is returned
|
|
90
94
|
* as is.
|
|
@@ -409,7 +409,11 @@ const TEXT_ALIAS = 'AstryxText';
|
|
|
409
409
|
/**
|
|
410
410
|
* Replace each JSX element imported from {@link DEMO_CONTENT_MODULE} with a
|
|
411
411
|
* generic `<Text>` placeholder and remove that import, so emitted source never
|
|
412
|
-
* depends on content only Astryx's previews provide.
|
|
412
|
+
* depends on content only Astryx's previews provide. An element that wraps one
|
|
413
|
+
* element, such as a hint shown under a demo, is removed instead: its child
|
|
414
|
+
* takes its place, with the indentation the child would have had without it.
|
|
415
|
+
* `demoContentReplaced` counts the placeholders, because only they need the
|
|
416
|
+
* consumer's own content.
|
|
413
417
|
*
|
|
414
418
|
* Every check resolves a name to its binding through the module's scope, so a
|
|
415
419
|
* local declaration or parameter with the same name never counts as the same
|
|
@@ -419,12 +423,12 @@ const TEXT_ALIAS = 'AstryxText';
|
|
|
419
423
|
* writes the name `Text`.
|
|
420
424
|
*
|
|
421
425
|
* Only a plain static use is replaced: a named import used only as a JSX tag
|
|
422
|
-
* whose children
|
|
426
|
+
* whose children are whitespace or one element, and whose props are literal values or
|
|
423
427
|
* read only bindings that code outside the replaced elements still reads. So
|
|
424
428
|
* `faceDown={!isOpen}` is safe when the same `isOpen` is also read elsewhere.
|
|
425
429
|
* Anything else (a namespace or type import, a re-export, a dynamic import, the
|
|
426
430
|
* element used as a value or a type, a spread or computed prop, a prop whose
|
|
427
|
-
* binding nothing else reads, children) throws, naming the element, rather than
|
|
431
|
+
* binding nothing else reads, other children) throws, naming the element, rather than
|
|
428
432
|
* leaving a reference behind, leaving a binding unused, or dropping code the
|
|
429
433
|
* rest of the file relies on. Source that never names the module is returned
|
|
430
434
|
* as is.
|
|
@@ -520,6 +524,13 @@ export function replaceDemoContent(source) {
|
|
|
520
524
|
},
|
|
521
525
|
});
|
|
522
526
|
|
|
527
|
+
const wrappers = elements.filter(
|
|
528
|
+
element => wrappedElement(element.node) !== null,
|
|
529
|
+
);
|
|
530
|
+
if (wrappers.length > 0) {
|
|
531
|
+
return replaceDemoContent(unwrapDemoWrappers(source, wrappers));
|
|
532
|
+
}
|
|
533
|
+
|
|
523
534
|
/** @param {any} node */
|
|
524
535
|
const isReplaced = node =>
|
|
525
536
|
elements.some(
|
|
@@ -638,14 +649,147 @@ function uniqueName(base, written) {
|
|
|
638
649
|
return name;
|
|
639
650
|
}
|
|
640
651
|
|
|
652
|
+
/**
|
|
653
|
+
* Replace each outermost demo-content wrapper with the element it wraps.
|
|
654
|
+
* Wrappers inside a kept child go on the next pass, when they are outermost.
|
|
655
|
+
* Throws when a wrapper's prop reads a binding that nothing outside the
|
|
656
|
+
* wrappers' opening tags reads, so the removal would leave it unused.
|
|
657
|
+
* @param {string} source
|
|
658
|
+
* @param {any[]} wrappers - Element paths whose element wraps one element.
|
|
659
|
+
* @returns {string}
|
|
660
|
+
*/
|
|
661
|
+
function unwrapDemoWrappers(source, wrappers) {
|
|
662
|
+
/** @param {any} node */
|
|
663
|
+
const isInWrapperTag = node =>
|
|
664
|
+
wrappers.some(
|
|
665
|
+
wrapper =>
|
|
666
|
+
wrapper.node.openingElement.start <= node.start &&
|
|
667
|
+
node.end <= wrapper.node.openingElement.end,
|
|
668
|
+
);
|
|
669
|
+
for (const wrapper of wrappers) {
|
|
670
|
+
for (const variable of staticPropReads(wrapper.node)) {
|
|
671
|
+
const binding = wrapper.scope.getBinding(variable);
|
|
672
|
+
if (!binding || isExportedDeclaration(binding)) continue;
|
|
673
|
+
if (
|
|
674
|
+
binding.referencePaths.every((/** @type {any} */ ref) =>
|
|
675
|
+
isInWrapperTag(ref.node),
|
|
676
|
+
)
|
|
677
|
+
) {
|
|
678
|
+
throw unsafeDemoContent(
|
|
679
|
+
`<${wrapper.node.openingElement.name.name}> reads ${variable}, and nothing else in the file reads it, so the copy would leave it unused`,
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
const outermost = wrappers.filter(
|
|
685
|
+
wrapper =>
|
|
686
|
+
!wrappers.some(
|
|
687
|
+
other =>
|
|
688
|
+
other !== wrapper &&
|
|
689
|
+
other.node.start <= wrapper.node.start &&
|
|
690
|
+
wrapper.node.end <= other.node.end,
|
|
691
|
+
),
|
|
692
|
+
);
|
|
693
|
+
let output = source;
|
|
694
|
+
for (const wrapper of outermost.sort((a, b) => b.node.start - a.node.start)) {
|
|
695
|
+
output =
|
|
696
|
+
output.slice(0, wrapper.node.start) +
|
|
697
|
+
dedentedChild(source, wrapper) +
|
|
698
|
+
output.slice(wrapper.node.end);
|
|
699
|
+
}
|
|
700
|
+
return output;
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
/**
|
|
704
|
+
* The source of the element a wrapper holds, with each of its later lines
|
|
705
|
+
* moved left by the indent the wrapper added. A line that starts inside a
|
|
706
|
+
* string keeps its whitespace, because that whitespace is part of the value.
|
|
707
|
+
* @param {string} source
|
|
708
|
+
* @param {any} wrapper - An element path whose element wraps one element.
|
|
709
|
+
* @returns {string}
|
|
710
|
+
*/
|
|
711
|
+
function dedentedChild(source, wrapper) {
|
|
712
|
+
const child = /** @type {any} */ (wrappedElement(wrapper.node));
|
|
713
|
+
/** @param {number} offset */
|
|
714
|
+
const indentOfLine = offset => {
|
|
715
|
+
const lineStart = source.lastIndexOf('\n', offset - 1) + 1;
|
|
716
|
+
return /^[ \t]*/u.exec(source.slice(lineStart))?.[0].length ?? 0;
|
|
717
|
+
};
|
|
718
|
+
const depth = indentOfLine(child.start) - indentOfLine(wrapper.node.start);
|
|
719
|
+
/** @type {any[]} */
|
|
720
|
+
const strings = [];
|
|
721
|
+
wrapper.traverse({
|
|
722
|
+
'StringLiteral|TemplateLiteral'(/** @type {any} */ path) {
|
|
723
|
+
strings.push(path.node);
|
|
724
|
+
},
|
|
725
|
+
});
|
|
726
|
+
const leadingIndent = new RegExp(`^[ \\t]{0,${Math.max(depth, 0)}}`, 'u');
|
|
727
|
+
let lineStart = child.start;
|
|
728
|
+
return source
|
|
729
|
+
.slice(child.start, child.end)
|
|
730
|
+
.split('\n')
|
|
731
|
+
.map((line, index) => {
|
|
732
|
+
const start = lineStart;
|
|
733
|
+
lineStart += line.length + 1;
|
|
734
|
+
if (
|
|
735
|
+
index === 0 ||
|
|
736
|
+
strings.some(string => string.start < start && start < string.end)
|
|
737
|
+
) {
|
|
738
|
+
return line;
|
|
739
|
+
}
|
|
740
|
+
return line.replace(leadingIndent, '');
|
|
741
|
+
})
|
|
742
|
+
.join('\n');
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
/**
|
|
746
|
+
* The one element a demo-content element wraps, or null when its children are
|
|
747
|
+
* only whitespace. Throws for any other children.
|
|
748
|
+
* @param {any} element
|
|
749
|
+
* @returns {any}
|
|
750
|
+
*/
|
|
751
|
+
function wrappedElement(element) {
|
|
752
|
+
const content = element.children.filter(
|
|
753
|
+
(/** @type {any} */ child) =>
|
|
754
|
+
!(child.type === 'JSXText' && child.value.trim() === ''),
|
|
755
|
+
);
|
|
756
|
+
if (content.length === 0) return null;
|
|
757
|
+
if (
|
|
758
|
+
content.length === 1 &&
|
|
759
|
+
(content[0].type === 'JSXElement' || content[0].type === 'JSXFragment')
|
|
760
|
+
) {
|
|
761
|
+
return content[0];
|
|
762
|
+
}
|
|
763
|
+
throw unsafeDemoContent(
|
|
764
|
+
`<${element.openingElement.name.name}> has children other than one element`,
|
|
765
|
+
);
|
|
766
|
+
}
|
|
767
|
+
|
|
641
768
|
/**
|
|
642
769
|
* Throw unless a demo-content element can be replaced without dropping code:
|
|
643
|
-
* each prop a literal or a read of variables (see {@link
|
|
644
|
-
* no children but whitespace.
|
|
770
|
+
* each prop a literal or a read of variables (see {@link staticPropReads}),
|
|
771
|
+
* and no children but whitespace.
|
|
645
772
|
* @param {any} element
|
|
646
773
|
* @returns {string[]} The variables its props read.
|
|
647
774
|
*/
|
|
648
775
|
function assertStaticDemoElement(element) {
|
|
776
|
+
const variables = staticPropReads(element);
|
|
777
|
+
for (const child of element.children) {
|
|
778
|
+
if (child.type === 'JSXText' && child.value.trim() === '') continue;
|
|
779
|
+
throw unsafeDemoContent(
|
|
780
|
+
`<${element.openingElement.name.name}> has children`,
|
|
781
|
+
);
|
|
782
|
+
}
|
|
783
|
+
return variables;
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* Throw unless each prop of a demo-content element is a literal or a read of
|
|
788
|
+
* variables (see {@link isVariableRead}).
|
|
789
|
+
* @param {any} element
|
|
790
|
+
* @returns {string[]} The variables its props read.
|
|
791
|
+
*/
|
|
792
|
+
function staticPropReads(element) {
|
|
649
793
|
const name = element.openingElement.name.name;
|
|
650
794
|
/** @type {string[]} */
|
|
651
795
|
const variables = [];
|
|
@@ -666,10 +810,6 @@ function assertStaticDemoElement(element) {
|
|
|
666
810
|
`<${name}> prop ${attribute.name.name ?? ''} is not a literal value or a variable read`,
|
|
667
811
|
);
|
|
668
812
|
}
|
|
669
|
-
for (const child of element.children) {
|
|
670
|
-
if (child.type === 'JSXText' && child.value.trim() === '') continue;
|
|
671
|
-
throw unsafeDemoContent(`<${name}> has children`);
|
|
672
|
-
}
|
|
673
813
|
return variables;
|
|
674
814
|
}
|
|
675
815
|
|
|
@@ -715,7 +855,7 @@ function isLiteralExpression(node) {
|
|
|
715
855
|
/** @param {string} reason */
|
|
716
856
|
function unsafeDemoContent(reason) {
|
|
717
857
|
return new Error(
|
|
718
|
-
`Demo content from ${DEMO_CONTENT_MODULE} cannot be replaced safely: ${reason}. Use each element as a JSX tag with no children, and give each prop a literal or a variable the file also reads elsewhere.`,
|
|
858
|
+
`Demo content from ${DEMO_CONTENT_MODULE} cannot be replaced safely: ${reason}. Use each element as a JSX tag with no children or with one element child, and give each prop a literal or a variable the file also reads elsewhere.`,
|
|
719
859
|
);
|
|
720
860
|
}
|
|
721
861
|
|
|
@@ -9,6 +9,11 @@
|
|
|
9
9
|
* @returns {T}
|
|
10
10
|
*/
|
|
11
11
|
export function withListingCache<T>(fn: () => T): T;
|
|
12
|
+
/**
|
|
13
|
+
* Whether an operation's listing cache is active.
|
|
14
|
+
* @returns {boolean}
|
|
15
|
+
*/
|
|
16
|
+
export function listingCacheActive(): boolean;
|
|
12
17
|
/**
|
|
13
18
|
* `fs.readdirSync(dir)`, read once per operation. Returns a fresh array, so a
|
|
14
19
|
* caller that sorts it cannot reorder another caller's listing.
|
|
@@ -22,6 +27,15 @@ export function listDir(dir: string): string[];
|
|
|
22
27
|
* @returns {fs.Dirent[]}
|
|
23
28
|
*/
|
|
24
29
|
export function listDirEntries(dir: string): fs.Dirent[];
|
|
30
|
+
/**
|
|
31
|
+
* The entry named exactly `name` in `dir`'s listing, or null when the folder
|
|
32
|
+
* holds no such name or cannot be read. Inside an operation, a name the
|
|
33
|
+
* listing lacks costs no probe of the disk.
|
|
34
|
+
* @param {string} dir
|
|
35
|
+
* @param {string} name
|
|
36
|
+
* @returns {fs.Dirent | null}
|
|
37
|
+
*/
|
|
38
|
+
export function findEntry(dir: string, name: string): fs.Dirent | null;
|
|
25
39
|
/**
|
|
26
40
|
* `fs.existsSync(target)`, probed once per operation.
|
|
27
41
|
* @param {string} target
|
|
@@ -31,6 +45,7 @@ export function pathExists(target: string): boolean;
|
|
|
31
45
|
export type ListingCache = {
|
|
32
46
|
names: Map<string, string[]>;
|
|
33
47
|
entries: Map<string, fs.Dirent[]>;
|
|
48
|
+
byName: Map<string, Map<string, fs.Dirent>>;
|
|
34
49
|
exists: Map<string, boolean>;
|
|
35
50
|
};
|
|
36
51
|
import * as fs from 'node:fs';
|
|
@@ -23,6 +23,7 @@ import * as fs from 'node:fs';
|
|
|
23
23
|
* @typedef {{
|
|
24
24
|
* names: Map<string, string[]>,
|
|
25
25
|
* entries: Map<string, fs.Dirent[]>,
|
|
26
|
+
* byName: Map<string, Map<string, fs.Dirent>>,
|
|
26
27
|
* exists: Map<string, boolean>,
|
|
27
28
|
* }} ListingCache
|
|
28
29
|
*/
|
|
@@ -40,11 +41,24 @@ const scope = new AsyncLocalStorage();
|
|
|
40
41
|
export function withListingCache(fn) {
|
|
41
42
|
if (scope.getStore()) return fn();
|
|
42
43
|
return scope.run(
|
|
43
|
-
{
|
|
44
|
+
{
|
|
45
|
+
names: new Map(),
|
|
46
|
+
entries: new Map(),
|
|
47
|
+
byName: new Map(),
|
|
48
|
+
exists: new Map(),
|
|
49
|
+
},
|
|
44
50
|
fn,
|
|
45
51
|
);
|
|
46
52
|
}
|
|
47
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Whether an operation's listing cache is active.
|
|
56
|
+
* @returns {boolean}
|
|
57
|
+
*/
|
|
58
|
+
export function listingCacheActive() {
|
|
59
|
+
return scope.getStore() != null;
|
|
60
|
+
}
|
|
61
|
+
|
|
48
62
|
/**
|
|
49
63
|
* `fs.readdirSync(dir)`, read once per operation. Returns a fresh array, so a
|
|
50
64
|
* caller that sorts it cannot reorder another caller's listing.
|
|
@@ -78,6 +92,41 @@ export function listDirEntries(dir) {
|
|
|
78
92
|
return entries.slice();
|
|
79
93
|
}
|
|
80
94
|
|
|
95
|
+
/**
|
|
96
|
+
* The entry named exactly `name` in `dir`'s listing, or null when the folder
|
|
97
|
+
* holds no such name or cannot be read. Inside an operation, a name the
|
|
98
|
+
* listing lacks costs no probe of the disk.
|
|
99
|
+
* @param {string} dir
|
|
100
|
+
* @param {string} name
|
|
101
|
+
* @returns {fs.Dirent | null}
|
|
102
|
+
*/
|
|
103
|
+
export function findEntry(dir, name) {
|
|
104
|
+
const cache = scope.getStore();
|
|
105
|
+
if (!cache) {
|
|
106
|
+
try {
|
|
107
|
+
return (
|
|
108
|
+
fs
|
|
109
|
+
.readdirSync(dir, {withFileTypes: true})
|
|
110
|
+
.find(entry => entry.name === name) ?? null
|
|
111
|
+
);
|
|
112
|
+
} catch {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
let byName = cache.byName.get(dir);
|
|
117
|
+
if (!byName) {
|
|
118
|
+
let entries;
|
|
119
|
+
try {
|
|
120
|
+
entries = listDirEntries(dir);
|
|
121
|
+
} catch {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
byName = new Map(entries.map(entry => [entry.name, entry]));
|
|
125
|
+
cache.byName.set(dir, byName);
|
|
126
|
+
}
|
|
127
|
+
return byName.get(name) ?? null;
|
|
128
|
+
}
|
|
129
|
+
|
|
81
130
|
/**
|
|
82
131
|
* `fs.existsSync(target)`, probed once per operation.
|
|
83
132
|
* @param {string} target
|
package/foundation/fs/paths.mjs
CHANGED
|
@@ -13,7 +13,12 @@ import {createRequire} from 'node:module';
|
|
|
13
13
|
import * as os from 'node:os';
|
|
14
14
|
import * as path from 'node:path';
|
|
15
15
|
import {fileURLToPath} from 'node:url';
|
|
16
|
-
import {
|
|
16
|
+
import {
|
|
17
|
+
findEntry,
|
|
18
|
+
listDir,
|
|
19
|
+
listingCacheActive,
|
|
20
|
+
pathExists,
|
|
21
|
+
} from './listing-cache.mjs';
|
|
17
22
|
import {ERROR_CODES} from '../response/error-codes.mjs';
|
|
18
23
|
import {carriedCore, runChoice} from '../release/run-context.mjs';
|
|
19
24
|
import {findDeclaringPackage} from '../release/project.mjs';
|
|
@@ -867,6 +872,31 @@ export function listComponents(coreDir) {
|
|
|
867
872
|
.sort();
|
|
868
873
|
}
|
|
869
874
|
|
|
875
|
+
/**
|
|
876
|
+
* `path.relative(root, target)`, read straight off the string when `target`
|
|
877
|
+
* is a plain path under `root`. A resolver probes tens of thousands of such
|
|
878
|
+
* paths, and `path.relative` normalizes both on every call.
|
|
879
|
+
* @param {string} root
|
|
880
|
+
* @param {string} target
|
|
881
|
+
* @returns {string}
|
|
882
|
+
*/
|
|
883
|
+
function relativeInside(root, target) {
|
|
884
|
+
const prefix = root.endsWith(path.sep) ? root : `${root}${path.sep}`;
|
|
885
|
+
if (path.isAbsolute(root) && target.startsWith(prefix)) {
|
|
886
|
+
const rest = target.slice(prefix.length);
|
|
887
|
+
const plain =
|
|
888
|
+
rest !== '' &&
|
|
889
|
+
!(path.sep === '\\' && rest.includes('/')) &&
|
|
890
|
+
rest
|
|
891
|
+
.split(path.sep)
|
|
892
|
+
.every(
|
|
893
|
+
segment => segment !== '' && segment !== '.' && segment !== '..',
|
|
894
|
+
);
|
|
895
|
+
if (plain) return rest;
|
|
896
|
+
}
|
|
897
|
+
return path.relative(root, target);
|
|
898
|
+
}
|
|
899
|
+
|
|
870
900
|
/**
|
|
871
901
|
* Does `targetPath` exist with EXACTLY this spelling?
|
|
872
902
|
*
|
|
@@ -883,13 +913,28 @@ export function listComponents(coreDir) {
|
|
|
883
913
|
* @returns {boolean}
|
|
884
914
|
*/
|
|
885
915
|
export function existsCaseExact(targetPath, rootDir) {
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
const rel = path.relative(rootDir, targetPath);
|
|
889
|
-
if (rel === '') return true;
|
|
916
|
+
const rel = relativeInside(rootDir, targetPath);
|
|
917
|
+
if (rel === '') return pathExists(targetPath);
|
|
890
918
|
if (path.isAbsolute(rel) || rel === '..' || rel.startsWith(`..${path.sep}`))
|
|
891
919
|
return false;
|
|
892
920
|
|
|
921
|
+
if (listingCacheActive()) {
|
|
922
|
+
// Inside an operation the folders' listings answer, so a name that a
|
|
923
|
+
// listing lacks costs no probe of the disk. A symbolic link still needs
|
|
924
|
+
// its target.
|
|
925
|
+
let folder = rootDir;
|
|
926
|
+
let entry = null;
|
|
927
|
+
for (const segment of rel.split(path.sep)) {
|
|
928
|
+
entry = findEntry(folder, segment);
|
|
929
|
+
if (!entry) return false;
|
|
930
|
+
folder = folder.endsWith(path.sep)
|
|
931
|
+
? `${folder}${segment}`
|
|
932
|
+
: `${folder}${path.sep}${segment}`;
|
|
933
|
+
}
|
|
934
|
+
return !entry?.isSymbolicLink() || pathExists(targetPath);
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
if (!pathExists(targetPath)) return false;
|
|
893
938
|
let dir = rootDir;
|
|
894
939
|
for (const segment of rel.split(path.sep)) {
|
|
895
940
|
let entries;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astryxdesign/cli",
|
|
3
|
-
"version": "0.7.7-canary.
|
|
3
|
+
"version": "0.7.7-canary.58c5b8a",
|
|
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",
|
|
@@ -124,10 +124,10 @@
|
|
|
124
124
|
"zod": "^4.4.3"
|
|
125
125
|
},
|
|
126
126
|
"peerDependencies": {
|
|
127
|
-
"@astryxdesign/charts": "0.7.7-canary.
|
|
128
|
-
"@astryxdesign/core": "0.7.7-canary.
|
|
129
|
-
"@astryxdesign/lab": "0.7.7-canary.
|
|
130
|
-
"@astryxdesign/theme-neutral": "0.7.7-canary.
|
|
127
|
+
"@astryxdesign/charts": "0.7.7-canary.58c5b8a",
|
|
128
|
+
"@astryxdesign/core": "0.7.7-canary.58c5b8a",
|
|
129
|
+
"@astryxdesign/lab": "0.7.7-canary.58c5b8a",
|
|
130
|
+
"@astryxdesign/theme-neutral": "0.7.7-canary.58c5b8a"
|
|
131
131
|
},
|
|
132
132
|
"peerDependenciesMeta": {
|
|
133
133
|
"@astryxdesign/charts": {
|
|
@@ -144,10 +144,10 @@
|
|
|
144
144
|
}
|
|
145
145
|
},
|
|
146
146
|
"devDependencies": {
|
|
147
|
-
"@astryxdesign/charts": "0.7.7-canary.
|
|
148
|
-
"@astryxdesign/core": "0.7.7-canary.
|
|
149
|
-
"@astryxdesign/lab": "0.7.7-canary.
|
|
150
|
-
"@astryxdesign/theme-neutral": "0.7.7-canary.
|
|
147
|
+
"@astryxdesign/charts": "0.7.7-canary.58c5b8a",
|
|
148
|
+
"@astryxdesign/core": "0.7.7-canary.58c5b8a",
|
|
149
|
+
"@astryxdesign/lab": "0.7.7-canary.58c5b8a",
|
|
150
|
+
"@astryxdesign/theme-neutral": "0.7.7-canary.58c5b8a",
|
|
151
151
|
"@heroicons/react": "^2.2.0",
|
|
152
152
|
"@stylexjs/stylex": "^0.19.0",
|
|
153
153
|
"@types/babel__core": "^7.20.5",
|