@astryxdesign/cli 0.7.6-canary.eae71cb → 0.7.6-canary.eb282ba

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.
Files changed (31) hide show
  1. package/README.md +2 -2
  2. package/api/component/detail/showcase/showcase.mjs +9 -3
  3. package/api/search/_adapter.d.mts +3 -1
  4. package/api/search/_adapter.mjs +19 -19
  5. package/api/template/copy/copy.mjs +13 -7
  6. package/api/template/show/show.mjs +12 -8
  7. package/api/template/skeleton/skeleton.mjs +14 -5
  8. package/api/template/template.d.mts +1 -1
  9. package/api/template/template.doc.mjs +2 -2
  10. package/api/template/template.mjs +1 -0
  11. package/api/template/template.type.d.mts +2 -0
  12. package/api/template/template.type.mjs +2 -0
  13. package/assets/templates/blocks/components/BottomSheet/BottomSheetShowcase.doc.mjs +1 -1
  14. package/assets/templates/blocks/components/BottomSheet/BottomSheetShowcase.tsx +12 -12
  15. package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherShowcase.doc.mjs +1 -1
  16. package/assets/templates/blocks/components/BottomSheet/BottomSheetSwitcherShowcase.tsx +14 -10
  17. package/assets/templates/blocks/components/Hooks/useCollatorHookUsage.doc.mjs +20 -0
  18. package/assets/templates/blocks/components/Hooks/useCollatorHookUsage.tsx +55 -0
  19. package/assets/templates/blocks/components/Toast/useToastHookUsage.doc.mjs +14 -0
  20. package/assets/templates/blocks/components/Toast/useToastHookUsage.tsx +65 -0
  21. package/clients/cli/commands/template.mjs +29 -3
  22. package/foundation/discovery/package-tsconfig.mjs +34 -0
  23. package/foundation/discovery/template-adapter.d.mts +76 -1
  24. package/foundation/discovery/template-adapter.mjs +418 -13
  25. package/foundation/discovery/template-icons.mjs +7 -89
  26. package/foundation/discovery/template-source-tree.d.mts +48 -0
  27. package/foundation/discovery/template-source-tree.mjs +159 -0
  28. package/foundation/fs/read-record.d.mts +4 -1
  29. package/foundation/fs/read-record.mjs +9 -5
  30. package/foundation/response/response-types.doc.mjs +2 -2
  31. package/package.json +9 -9
@@ -0,0 +1,14 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /** @type {import('@astryxdesign/cli/authoring').TemplateDoc} */
4
+ export const doc = {
5
+ type: 'block',
6
+ exampleFor: 'useToast',
7
+ name: 'useToast — Settings Saved',
8
+ displayName: 'useToast — Settings Saved',
9
+ description:
10
+ 'Shows a toast from the function useToast returns: a "Settings saved" confirmation with an Undo action, a uniqueID so pressing the button again replaces the toast instead of stacking another, and an error toast when the request fails.',
11
+ isReady: true,
12
+ aspectRatio: 4 / 3,
13
+ componentsUsed: ['Toast', 'Card', 'TextInput', 'Switch', 'Button', 'Layout'],
14
+ };
@@ -0,0 +1,65 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ 'use client';
4
+
5
+ import {useState} from 'react';
6
+ import {Button} from '@astryxdesign/core/Button';
7
+ import {Card} from '@astryxdesign/core/Card';
8
+ import {VStack} from '@astryxdesign/core/Layout';
9
+ import {Switch} from '@astryxdesign/core/Switch';
10
+ import {TextInput} from '@astryxdesign/core/TextInput';
11
+ import {useToast} from '@astryxdesign/core/Toast';
12
+
13
+ export default function UseToastHookUsage() {
14
+ const toast = useToast();
15
+ const [name, setName] = useState('Ada Lovelace');
16
+ const [savedName, setSavedName] = useState('Ada Lovelace');
17
+ const [undoName, setUndoName] = useState('Ada Lovelace');
18
+ const [isSaveFailing, setIsSaveFailing] = useState(false);
19
+
20
+ const save = () => {
21
+ if (isSaveFailing) {
22
+ toast({
23
+ type: 'error',
24
+ body: 'Could not save settings',
25
+ uniqueID: 'settings-save',
26
+ });
27
+ return;
28
+ }
29
+ // Saving an unchanged name keeps the earlier undo target.
30
+ const nextUndoName = name === savedName ? undoName : savedName;
31
+ setUndoName(nextUndoName);
32
+ setSavedName(name);
33
+ const dismiss = toast({
34
+ body: 'Settings saved',
35
+ // A repeated Save replaces this toast instead of stacking another.
36
+ uniqueID: 'settings-save',
37
+ endContent: (
38
+ <Button
39
+ label="Undo"
40
+ variant="ghost"
41
+ size="sm"
42
+ onClick={() => {
43
+ setName(nextUndoName);
44
+ setSavedName(nextUndoName);
45
+ dismiss();
46
+ }}
47
+ />
48
+ ),
49
+ });
50
+ };
51
+
52
+ return (
53
+ <Card width={360} padding={4}>
54
+ <VStack gap={3}>
55
+ <TextInput label="Display name" value={name} onChange={setName} />
56
+ <Switch
57
+ label="Simulate a failed save"
58
+ value={isSaveFailing}
59
+ onChange={setIsSaveFailing}
60
+ />
61
+ <Button label="Save" variant="primary" onClick={save} />
62
+ </VStack>
63
+ </Card>
64
+ );
65
+ }
@@ -32,6 +32,20 @@ export {discoverTemplates, listTemplates} from '../../../api/template/template.m
32
32
  * )} TemplateResponse
33
33
  */
34
34
 
35
+ /**
36
+ * The line saying how many docs-only demo content elements became a
37
+ * placeholder, in the copied file when one is named.
38
+ * @param {number} count
39
+ * @param {string} [file]
40
+ * @returns {string}
41
+ */
42
+ function demoContentNote(count, file) {
43
+ return (
44
+ `Replaced ${count} Astryx demo content element${count === 1 ? '' : 's'}${file ? ` in ${file}` : ''} ` +
45
+ 'with a <Text>Card content</Text> placeholder. Put your own content there.'
46
+ );
47
+ }
48
+
35
49
  /**
36
50
  * The icon report as text lines (AST-084 FR10): one line per mapped or kept
37
51
  * icon, or one line saying why nothing was mapped. Nothing when the project
@@ -245,7 +259,8 @@ export function registerTemplate(program) {
245
259
  // from (cli-surface INV28) and the note about replaced demo media go
246
260
  // to stderr, the note in the copy receipt's words. (Text mode only:
247
261
  // --json returned above with both in the envelope.)
248
- const {source, demoMediaReplaced, icons} = result.data;
262
+ const {source, demoMediaReplaced, demoContentReplaced, icons} =
263
+ result.data;
249
264
  console.error(`package: ${result.package}`);
250
265
  emit(code(source));
251
266
  if (demoMediaReplaced > 0) {
@@ -254,14 +269,23 @@ export function registerTemplate(program) {
254
269
  'images now show a neutral placeholder and videos have an empty source. Supply your own media there.',
255
270
  );
256
271
  }
272
+ if (demoContentReplaced > 0) {
273
+ console.error(demoContentNote(demoContentReplaced));
274
+ }
257
275
  // The icon report goes to stderr too, so stdout stays the source.
258
276
  for (const line of iconLines(icons)) console.error(line);
259
277
  break;
260
278
  }
261
279
 
262
280
  case 'template.copy': {
263
- const {outputDir, fileName, demoMediaReplaced, notes, icons} =
264
- result.data;
281
+ const {
282
+ outputDir,
283
+ fileName,
284
+ demoMediaReplaced,
285
+ demoContentReplaced,
286
+ notes,
287
+ icons,
288
+ } = result.data;
265
289
  const file = `${outputDir}/${fileName}`;
266
290
  const iconText = iconLines(icons);
267
291
  emit(
@@ -272,6 +296,8 @@ export function registerTemplate(program) {
272
296
  `Replaced ${demoMediaReplaced} Astryx demo media reference${demoMediaReplaced === 1 ? '' : 's'} in ${file}: ` +
273
297
  'images now show a neutral placeholder and videos have an empty source. Supply your own media there.',
274
298
  ),
299
+ demoContentReplaced > 0 &&
300
+ text(demoContentNote(demoContentReplaced, file)),
275
301
  ...notes.map(/** @param {string} note */ note => text(note)),
276
302
  iconText.length > 0 && text(iconText.join('\n')),
277
303
  );
@@ -26,6 +26,16 @@
26
26
 
27
27
  import * as fs from 'node:fs';
28
28
  import * as path from 'node:path';
29
+ import {fileStamp, recordContentRead} from '../fs/read-record.mjs';
30
+
31
+ /**
32
+ * What `isAmbient` found for each file, per TypeScript, kept while the
33
+ * file's stamp (`fileStamp`) is the same. A process that compares a package
34
+ * more than once reads the same config each time, and reading a file the way
35
+ * TypeScript does costs far more than a stat.
36
+ * @type {WeakMap<object, Map<string, {stamp: string, ambient: boolean}>>}
37
+ */
38
+ const ambientByFile = new WeakMap();
29
39
 
30
40
  /**
31
41
  * Whether a file the config includes declares types every file sees: a
@@ -36,6 +46,30 @@ import * as path from 'node:path';
36
46
  */
37
47
  function isAmbient(ts, file) {
38
48
  if (/\.d\.[cm]?ts$/.test(file)) return true;
49
+ // A kept answer still counts as reading the file, so an operation that
50
+ // keeps its own result while what it read is unchanged sees a change to it.
51
+ recordContentRead(file);
52
+ const stamp = fileStamp(file);
53
+ if (stamp === '-') return false;
54
+ let known = ambientByFile.get(ts);
55
+ if (known == null) {
56
+ known = new Map();
57
+ ambientByFile.set(ts, known);
58
+ }
59
+ const key = path.resolve(file);
60
+ const seen = known.get(key);
61
+ if (seen && seen.stamp === stamp) return seen.ambient;
62
+ const ambient = readAmbient(ts, file);
63
+ known.set(key, {stamp, ambient});
64
+ return ambient;
65
+ }
66
+
67
+ /**
68
+ * `isAmbient` for a file that is not a declaration file, read afresh.
69
+ * @param {any} ts
70
+ * @param {string} file
71
+ */
72
+ function readAmbient(ts, file) {
39
73
  let text;
40
74
  try {
41
75
  text = fs.readFileSync(file, 'utf8');
@@ -56,12 +56,63 @@ export function stripTemplateAssetRefs(source: string): string;
56
56
  * third-party URL left as written. That count is what a receipt discloses.
57
57
  *
58
58
  * @param {string} source - Template source code.
59
+ * @param {{videoPlaceholder?: string}} [options] - `videoPlaceholder` replaces
60
+ * each video reference; the default is an empty source.
59
61
  * @returns {{source: string, demoMediaReplaced: number}}
60
62
  */
61
- export function replaceDemoMedia(source: string): {
63
+ export function replaceDemoMedia(source: string, { videoPlaceholder }?: {
64
+ videoPlaceholder?: string;
65
+ }): {
62
66
  source: string;
63
67
  demoMediaReplaced: number;
64
68
  };
69
+ /**
70
+ * Replace each JSX element imported from {@link DEMO_CONTENT_MODULE} with a
71
+ * generic `<Text>` placeholder and remove that import, so emitted source never
72
+ * depends on content only Astryx's previews provide.
73
+ *
74
+ * Every check resolves a name to its binding through the module's scope, so a
75
+ * local declaration or parameter with the same name never counts as the same
76
+ * variable. The placeholder uses an existing Text import from Astryx when that
77
+ * import is the binding its name resolves to at every replaced element. Else
78
+ * the placeholder imports Text, under a unique alias when the file already
79
+ * writes the name `Text`.
80
+ *
81
+ * Only a plain static use is replaced: a named import used only as a JSX tag
82
+ * whose children, if any, are whitespace, and whose props are literal values or
83
+ * read only bindings that code outside the replaced elements still reads. So
84
+ * `faceDown={!isOpen}` is safe when the same `isOpen` is also read elsewhere.
85
+ * Anything else (a namespace or type import, a re-export, a dynamic import, the
86
+ * 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
88
+ * leaving a reference behind, leaving a binding unused, or dropping code the
89
+ * rest of the file relies on. Source that never names the module is returned
90
+ * as is.
91
+ *
92
+ * @param {string} source - Template source code.
93
+ * @returns {{source: string, demoContentReplaced: number}}
94
+ */
95
+ export function replaceDemoContent(source: string): {
96
+ source: string;
97
+ demoContentReplaced: number;
98
+ };
99
+ /**
100
+ * Replace everything in a template that only Astryx's previews provide, in
101
+ * the order a copy applies it: demo-content elements
102
+ * ({@link replaceDemoContent}), then demo media ({@link replaceDemoMedia}).
103
+ * Both counts are what a receipt discloses.
104
+ *
105
+ * @param {string} source - Template source code.
106
+ * @param {{videoPlaceholder?: string}} [options] - As {@link replaceDemoMedia}.
107
+ * @returns {{source: string, demoContentReplaced: number, demoMediaReplaced: number}}
108
+ */
109
+ export function replaceTemplateDemos(source: string, options?: {
110
+ videoPlaceholder?: string;
111
+ }): {
112
+ source: string;
113
+ demoContentReplaced: number;
114
+ demoMediaReplaced: number;
115
+ };
65
116
  /**
66
117
  * List the core page-template directory names (sorted). The `template --list`
67
118
  * view discovers far more; this is the minimal core-page set `init` scaffolds
@@ -171,10 +222,34 @@ export function findShowcase(componentName: string, cwd?: string, options?: {
171
222
  docPath: string;
172
223
  } | null>;
173
224
  /**
225
+ * The components a template file composes, as written in the catalog.
174
226
  * @param {string} pagePath
175
227
  * @returns {string[]}
176
228
  */
177
229
  export function extractComponents(pagePath: string): string[];
230
+ /**
231
+ * The components a template source composes. A surface that returns rewritten
232
+ * source lists the components of that source, so a component the rewrite
233
+ * removed is not listed.
234
+ * @param {string} src
235
+ * @returns {string[]}
236
+ */
237
+ export function componentsInSource(src: string): string[];
238
+ /**
239
+ * A video placeholder that names itself in the source, for a copy that has no
240
+ * response to disclose the replacement in, such as the shadcn install. It is a
241
+ * text data URL: it makes no request, it does not play, and it is not image
242
+ * data, so a video element never receives image bytes.
243
+ */
244
+ export const MARKED_VIDEO_PLACEHOLDER: "data:,placeholder-video-add-your-own";
245
+ /**
246
+ * The one module docs-only demo content is imported from. A template imports
247
+ * an element such as a playing card's face from here so its live preview shows
248
+ * real-looking content; the module exists only where Astryx's own previews
249
+ * build templates, so a copy replaces every element imported from it (see
250
+ * {@link replaceDemoContent}).
251
+ */
252
+ export const DEMO_CONTENT_MODULE: "@astryx-docs/demo-content";
178
253
  /**
179
254
  * A discovered template (page or block), normalized across core, external, and
180
255
  * integration sources. Not every source populates every field, so