@astryxdesign/cli 0.7.14 → 0.7.15-canary.g06b7e61

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 (203) hide show
  1. package/CHANGELOG.md +3 -3
  2. package/README.md +63 -63
  3. package/api/build/_adapter.d.mts +4 -0
  4. package/api/build/_adapter.mjs +28 -2
  5. package/api/build/blocks/blocks.d.mts +10 -1
  6. package/api/build/blocks/blocks.mjs +84 -7
  7. package/api/build/build.mjs +6 -1
  8. package/api/build/tree.d.mts +4 -0
  9. package/api/build/tree.mjs +9 -2
  10. package/api/doctor/_adapter.d.mts +3 -1
  11. package/api/doctor/_adapter.mjs +14 -1
  12. package/api/doctor/_authoring-audit.d.mts +42 -0
  13. package/api/doctor/_authoring-audit.mjs +106 -0
  14. package/api/doctor/doctor.d.mts +7 -0
  15. package/api/doctor/doctor.mjs +24 -39
  16. package/api/doctor/theme-checks.mjs +56 -21
  17. package/api/integration/add-helpers.mjs +5 -0
  18. package/api/integration/add-theme.mjs +15 -110
  19. package/api/integration/validate-integration.mjs +17 -3
  20. package/api/integration/validateIntegration.doc.mjs +1 -1
  21. package/api/search/_adapter.mjs +124 -3
  22. package/api/search/_candidate-score.mjs +63 -9
  23. package/api/search/_entries.d.mts +10 -0
  24. package/api/search/_entries.mjs +68 -1
  25. package/api/search/_gather-templates.d.mts +11 -0
  26. package/api/search/_gather-templates.mjs +22 -2
  27. package/api/search/_words.mjs +26 -6
  28. package/api/swizzle/copy/copy.mjs +1 -1
  29. package/api/theme/_adapter.d.mts +37 -5
  30. package/api/theme/_adapter.mjs +381 -57
  31. package/api/theme/_fork.d.mts +90 -0
  32. package/api/theme/_fork.mjs +246 -0
  33. package/api/theme/build/build.d.mts +40 -2
  34. package/api/theme/build/build.mjs +477 -66
  35. package/api/theme/eject/eject.mjs +36 -15
  36. package/api/theme/list/list.mjs +3 -6
  37. package/api/theme/palette/generate/generator.d.mts +13 -0
  38. package/api/theme/palette/generate/generator.mjs +32 -2
  39. package/api/theme/targets/targets.d.mts +8 -0
  40. package/api/theme/targets/targets.mjs +24 -4
  41. package/api/theme/template/template.mjs +48 -2
  42. package/api/theme/theme.type.d.mts +8 -0
  43. package/api/theme/theme.type.mjs +5 -1
  44. package/api/theme/themeBuild.doc.mjs +7 -4
  45. package/api/theme/themeEject.doc.mjs +3 -3
  46. package/api/theme/themeRemove.doc.mjs +5 -2
  47. package/api/theme/themeTargets.doc.mjs +1 -1
  48. package/api/theme/themeTemplate.doc.mjs +2 -2
  49. package/api/theme/themeUse.doc.mjs +8 -2
  50. package/api/theme-edit/_adapter.d.mts +46 -3
  51. package/api/theme-edit/_adapter.mjs +496 -112
  52. package/api/theme-edit/_catalog.d.mts +1 -6
  53. package/api/theme-edit/_catalog.mjs +5 -268
  54. package/api/theme-edit/_check.d.mts +9 -4
  55. package/api/theme-edit/_check.mjs +1232 -229
  56. package/api/theme-edit/_color.d.mts +19 -0
  57. package/api/theme-edit/_color.mjs +280 -34
  58. package/api/theme-edit/_compiler.d.mts +3 -1
  59. package/api/theme-edit/_compiler.mjs +52 -13
  60. package/api/theme-edit/_context.d.mts +4 -1
  61. package/api/theme-edit/_context.mjs +126 -13
  62. package/api/theme-edit/_css.d.mts +84 -0
  63. package/api/theme-edit/_css.mjs +700 -0
  64. package/api/theme-edit/_edit.d.mts +9 -3
  65. package/api/theme-edit/_edit.mjs +806 -103
  66. package/api/theme-edit/_how-commands.d.mts +38 -7
  67. package/api/theme-edit/_how-commands.mjs +64 -45
  68. package/api/theme-edit/_how-question.d.mts +33 -0
  69. package/api/theme-edit/_how-question.mjs +2553 -381
  70. package/api/theme-edit/_how.mjs +402 -31
  71. package/api/theme-edit/_overview.mjs +2 -7
  72. package/api/theme-edit/_palette.d.mts +60 -13
  73. package/api/theme-edit/_palette.mjs +826 -295
  74. package/api/theme-edit/_parts.d.mts +42 -10
  75. package/api/theme-edit/_parts.mjs +280 -69
  76. package/api/theme-edit/_png.d.mts +4 -4
  77. package/api/theme-edit/_png.mjs +4 -2
  78. package/api/theme-edit/_preview-app.d.mts +14 -0
  79. package/api/theme-edit/_preview-app.mjs +1675 -235
  80. package/api/theme-edit/_preview.d.mts +7 -16
  81. package/api/theme-edit/_preview.mjs +79 -86
  82. package/api/theme-edit/_project.d.mts +32 -2
  83. package/api/theme-edit/_project.mjs +80 -19
  84. package/api/theme-edit/_settings.d.mts +199 -52
  85. package/api/theme-edit/_settings.mjs +1422 -217
  86. package/api/theme-edit/_tables.d.mts +19 -6
  87. package/api/theme-edit/_tables.mjs +150 -92
  88. package/api/theme-edit/theme-edit.type.d.mts +20 -1
  89. package/api/theme-edit/theme-edit.type.mjs +6 -3
  90. package/api/theme-edit/themeEditHow.doc.mjs +6 -0
  91. package/api/theme-edit/themeEditNew.doc.mjs +1 -1
  92. package/api/theme-edit/themeEditPreviewAdd.doc.mjs +1 -1
  93. package/api/theme-edit/themeEditPreviewApp.doc.mjs +1 -1
  94. package/api/theme-edit/themeEditPreviewList.doc.mjs +2 -2
  95. package/api/theme-edit/themeEditPreviewRemove.doc.mjs +5 -1
  96. package/api/theme-edit/themeEditSet.doc.mjs +1 -1
  97. package/api/theme-edit/themeEditUndo.doc.mjs +1 -1
  98. package/api/upgrade/_adapter.d.mts +1 -0
  99. package/assets/codemods/run-codemod.mjs +61 -0
  100. package/assets/codemods/runner.mjs +5 -4
  101. package/assets/codemods/transforms/v0.0.14/rename-status-variants.mjs +3 -0
  102. package/assets/codemods/transforms/v0.1.8/rename-avatar-size-scale.mjs +5 -1
  103. package/assets/codemods/transforms/v0.6.4/migrate-native-picker-to-presentation.mjs +3 -0
  104. package/assets/codemods/transforms/v0.7.0/migrate-deprecated-theme-surface.mjs +4 -0
  105. package/assets/codemods/transforms/v0.7.0/rename-empty-search-results-text.mjs +17 -12
  106. package/assets/codemods/transforms/v0.7.2/migrate-list-edge-compensation-to-edge.mjs +3 -0
  107. package/assets/codemods/transforms/v0.7.5/migrate-codeblock-tokenizer-to-language.mjs +3 -0
  108. package/assets/codemods/transforms/v0.7.8/promote-tokenizer-presentation.mjs +3 -0
  109. package/assets/docs/tree/calendars.doc.mjs +1 -3
  110. package/assets/docs/tree/checks.doc.mjs +22 -1
  111. package/assets/docs/tree/publishing.doc.mjs +1 -0
  112. package/assets/docs/tree/tokens-and-setup.doc.mjs +39 -3
  113. package/assets/docs.generated.json +2 -1
  114. package/assets/preview-app/next.config.mjs +7 -1
  115. package/assets/self-audit.generated.json +1 -0
  116. package/assets/templates/blocks/components/Theme/useThemeNameHookUsage.doc.mjs +14 -0
  117. package/assets/templates/blocks/components/Theme/useThemeNameHookUsage.tsx +31 -0
  118. package/assets/templates/components.generated.json +1 -1
  119. package/authoring/integration/integration.doc.mjs +8 -0
  120. package/authoring/integration/schema.d.mts +8 -0
  121. package/authoring/integration/schema.mjs +1 -0
  122. package/authoring/integration/type.ts +9 -0
  123. package/clients/cli/bin/astryx.mjs +19 -15
  124. package/clients/cli/build-id.generated.json +1 -0
  125. package/clients/cli/commands/ensure-core-built.mjs +12 -5
  126. package/clients/cli/commands/experimental/index.mjs +10 -10
  127. package/clients/cli/commands/experimental/theme.mjs +200 -81
  128. package/clients/cli/commands/experimental-theme-compile.doc.mjs +3 -3
  129. package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +1 -1
  130. package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +3 -3
  131. package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +1 -2
  132. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
  133. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +2 -2
  134. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  135. package/clients/cli/commands/experimental-theme-eject.doc.mjs +3 -3
  136. package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +1 -1
  137. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
  138. package/clients/cli/commands/experimental-theme-preview-list.doc.mjs +1 -1
  139. package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +1 -1
  140. package/clients/cli/commands/experimental-theme-remove.doc.mjs +1 -1
  141. package/clients/cli/commands/experimental-theme-template.doc.mjs +4 -3
  142. package/clients/cli/commands/integration.mjs +8 -0
  143. package/clients/cli/commands/manifest.mjs +5 -2
  144. package/clients/cli/commands/theme-build.doc.mjs +2 -2
  145. package/clients/cli/commands/theme-list.doc.mjs +1 -1
  146. package/clients/cli/commands/theme-template.doc.mjs +1 -1
  147. package/clients/cli/commands/theme.mjs +439 -66
  148. package/clients/cli/index.mjs +27 -6
  149. package/clients/cli/lib/cli-error.mjs +118 -3
  150. package/clients/cli/lib/manifest.mjs +37 -0
  151. package/clients/cli/lib/resolve-theme.mjs +2 -1
  152. package/clients/cli/manifest.generated.json +1 -0
  153. package/foundation/config/project.mjs +34 -8
  154. package/foundation/config/theme-state.d.mts +16 -0
  155. package/foundation/config/theme-state.mjs +84 -13
  156. package/foundation/discovery/module-exports.mjs +16 -2
  157. package/foundation/discovery/stored-self-audit.d.mts +31 -0
  158. package/foundation/discovery/stored-self-audit.mjs +119 -0
  159. package/foundation/discovery/template-adapter.d.mts +48 -2
  160. package/foundation/discovery/template-adapter.mjs +51 -19
  161. package/foundation/discovery/template-needs.d.mts +19 -0
  162. package/foundation/discovery/template-needs.mjs +50 -16
  163. package/foundation/discovery/template-source-tree.d.mts +7 -0
  164. package/foundation/discovery/template-source-tree.mjs +15 -0
  165. package/foundation/discovery/theme-discovery.d.mts +2 -1
  166. package/foundation/discovery/theme-discovery.mjs +16 -10
  167. package/foundation/discovery/theme-imports.d.mts +16 -0
  168. package/foundation/discovery/theme-imports.mjs +30 -2
  169. package/foundation/discovery/theme-module-extensions.d.mts +18 -0
  170. package/foundation/discovery/theme-module-extensions.mjs +30 -0
  171. package/foundation/doc-compiler/import.mjs +22 -15
  172. package/foundation/doc-compiler/stored-docs.d.mts +18 -12
  173. package/foundation/doc-compiler/stored-docs.mjs +52 -19
  174. package/foundation/env/private-dir.d.mts +17 -0
  175. package/foundation/env/private-dir.mjs +42 -0
  176. package/foundation/fs/gather-cache.d.mts +108 -0
  177. package/foundation/fs/gather-cache.mjs +783 -0
  178. package/foundation/fs/jiti-cache.d.mts +21 -0
  179. package/foundation/fs/jiti-cache.mjs +66 -0
  180. package/foundation/fs/module-loader.mjs +14 -3
  181. package/foundation/fs/read-record.d.mts +31 -1
  182. package/foundation/fs/read-record.mjs +100 -7
  183. package/foundation/integrations/autolink.d.mts +1 -24
  184. package/foundation/integrations/autolink.mjs +7 -60
  185. package/foundation/integrations/declared-dependencies.d.mts +27 -0
  186. package/foundation/integrations/declared-dependencies.mjs +73 -0
  187. package/foundation/integrations/integrations.d.mts +8 -0
  188. package/foundation/integrations/integrations.mjs +4 -0
  189. package/foundation/integrations/may-have-integrations.d.mts +8 -0
  190. package/foundation/integrations/may-have-integrations.mjs +47 -0
  191. package/foundation/integrations/validate-contributions.d.mts +62 -1
  192. package/foundation/integrations/validate-contributions.mjs +476 -40
  193. package/foundation/release/cache.d.mts +32 -4
  194. package/foundation/release/cache.mjs +105 -16
  195. package/foundation/release/folder-lock.d.mts +28 -0
  196. package/foundation/release/folder-lock.mjs +205 -0
  197. package/foundation/release/lock-lease.d.mts +2 -1
  198. package/foundation/release/lock-lease.mjs +7 -2
  199. package/foundation/response/error-codes.doc.mjs +1 -1
  200. package/foundation/response/response-types.doc.mjs +5 -5
  201. package/package.json +8 -6
  202. package/api/theme-edit/_css-properties.d.mts +0 -10
  203. package/api/theme-edit/_css-properties.mjs +0 -556
@@ -2,20 +2,45 @@
2
2
  // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
3
 
4
4
  /**
5
- * @typedef {object} PartRecord
6
- * @property {Record<string, {tokens: Record<string, string[]>, values: string[]}>} own - property -> tokens (with conditions) and raw values
7
- * @property {Record<string, string[]>} inside - tokens read by unnamed elements inside the part -> properties
8
- * @property {Record<string, string[]>} attrs - data attributes seen on the part
9
- * @property {string[]} contains - parts rendered inside it
5
+ * The props a component renders with: its doc's playground defaults, or,
6
+ * for a doc with none (an older Core), a sample for each required prop.
7
+ * A group with no children gets three of the items its doc suggests. Null
8
+ * for a doc with no defaults that has no theming targets (a hook or a
9
+ * helper), or that is a part its parent lists as an item: it works only
10
+ * inside the parent, whose own entry shows it.
11
+ * @param {import('./_project.mjs').ThemeProject} project
12
+ * @param {string} name
13
+ * @returns {Record<string, unknown> | null}
14
+ */
15
+ export function demoProps(project: import("./_project.mjs").ThemeProject, name: string): Record<string, unknown> | null;
16
+ /**
17
+ * Whether a part's parent lists it as an item of its children.
18
+ * @param {import('./_project.mjs').ThemeProject} project
19
+ * @param {string} name
10
20
  */
21
+ export function isItemOf(project: import("./_project.mjs").ThemeProject, name: string): boolean;
11
22
  /**
12
- * @typedef {{target: string | null, tokens: string[], raw: string | null}} PaintSide
13
- * @typedef {{text: string, fg: PaintSide, bg: PaintSide, sample: string}} TextPair
14
- * @typedef {{version: number, coreKey: string, map: Record<string, PartRecord>, pairs: TextPair[], rendered: number, items: number}} PartData
23
+ * A component's catalog entries, all from its doc: its demo props
24
+ * ({@link demoProps}), then once per value of each visual prop with a
25
+ * closed set of values (`variant`, `status`, `color`, and a number union
26
+ * such as a heading's level), and its checked and disabled states. An
27
+ * overlay the playground shows in place renders open; on a page people
28
+ * see, once, as some of its variants size the window, not the card.
29
+ * @param {import('./_project.mjs').ThemeProject} project
30
+ * @param {string} name
31
+ * @param {number} cap at most this many
32
+ * @param {{shown?: boolean}} [opts] `shown`: for a page people see
33
+ * @returns {Array<{label: string, props: Record<string, unknown>}>}
15
34
  */
35
+ export function catalogVariants(project: import("./_project.mjs").ThemeProject, name: string, cap: number, opts?: {
36
+ shown?: boolean;
37
+ }): Array<{
38
+ label: string;
39
+ props: Record<string, unknown>;
40
+ }>;
16
41
  /**
17
- * The catalog: each component with playground defaults once per visual
18
- * variant value, the hand-made extras, and the sample page.
42
+ * The catalog: each component with playground defaults, once per visual
43
+ * variant value ({@link catalogVariants}), and the sample page.
19
44
  * @param {import('./_project.mjs').ThemeProject} project
20
45
  * @returns {{items: import('./_catalog.mjs').CatalogItem[], imports: Record<string, string>}}
21
46
  */
@@ -23,6 +48,13 @@ export function catalogItems(project: import("./_project.mjs").ThemeProject): {
23
48
  items: import("./_catalog.mjs").CatalogItem[];
24
49
  imports: Record<string, string>;
25
50
  };
51
+ /**
52
+ * What of React the project cannot resolve (react, react-dom/server), to
53
+ * render the editor's pages; empty when both resolve.
54
+ * @param {string} cwd
55
+ * @returns {string[]}
56
+ */
57
+ export function reactMissing(cwd: string): string[];
26
58
  /**
27
59
  * Render a generated page module and give its HTML and markup (the body,
28
60
  * without the embedded source block).
@@ -17,20 +17,21 @@
17
17
 
18
18
  import * as fs from 'node:fs';
19
19
  import * as path from 'node:path';
20
+ import {createRequire} from 'node:module';
20
21
  import {AstryxError} from '../error.mjs';
21
22
  import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
22
- import {importPathOf} from './_project.mjs';
23
- import {renderPage, RenderError} from '../../foundation/render/render-page.mjs';
23
+ import {importPathOf, installLine, literals} from './_project.mjs';
24
+ import {removeEmptyFolder} from './_palette.mjs';
25
+ import {VOID} from '../../foundation/render/html-reader.mjs';
24
26
  import {
25
- EXTRA,
26
- OPEN_OVERLAYS,
27
- SAMPLE_PAGE,
28
- elementNames,
29
- pageSource,
30
- } from './_catalog.mjs';
27
+ RENDER_TIMEOUT_MS,
28
+ renderPage,
29
+ RenderError,
30
+ } from '../../foundation/render/render-page.mjs';
31
+ import {SAMPLE_PAGE, elementNames, pageSource} from './_catalog.mjs';
31
32
 
32
33
  /** Bump when the shape of the cached part data changes. */
33
- const PART_DATA_VERSION = 3;
34
+ const PART_DATA_VERSION = 4;
34
35
 
35
36
  /**
36
37
  * @typedef {object} PartRecord
@@ -47,33 +48,193 @@ const PART_DATA_VERSION = 3;
47
48
  */
48
49
 
49
50
  /**
50
- * The catalog: each component with playground defaults once per visual
51
- * variant value, the hand-made extras, and the sample page.
51
+ * A sample value for a required prop the doc declares, from its type: the
52
+ * component's name for text, false for a flag, the first value of a union.
53
+ * Undefined for a type with no plain sample, such as a function.
54
+ * @param {string} name
55
+ * @param {string} type
56
+ * @returns {unknown}
57
+ */
58
+ function sampleOf(name, type) {
59
+ if (type.includes('=>')) return undefined;
60
+ // Null and undefined are the absence of a value, never a sample.
61
+ const t = type
62
+ .split('|')
63
+ .map(x => x.trim())
64
+ .filter(x => x !== 'null' && x !== 'undefined')
65
+ .join(' | ');
66
+ if (t === 'string' || /ReactNode$/.test(t)) return name;
67
+ if (t === 'boolean') return false;
68
+ if (t === 'number') return 1;
69
+ const words = literals(t);
70
+ if (words.length) return words[0];
71
+ if (/^\d+(\s*\|\s*\d+)*$/.test(t)) return Number(t.split('|')[0]);
72
+ return undefined;
73
+ }
74
+
75
+ /**
76
+ * Three items made from the element the doc suggests for a group's
77
+ * children, each with its own label and value; the first takes the group's
78
+ * value, so it shows selected.
79
+ * @param {{__element: string, props?: Record<string, unknown>, children?: unknown}} slot
80
+ * @param {unknown} selected
81
+ */
82
+ function slotItems(slot, selected) {
83
+ return [1, 2, 3].map(i => {
84
+ const props = {...(slot.props ?? {})};
85
+ if (typeof props.label === 'string') props.label = `${props.label} ${i}`;
86
+ if ('value' in props || typeof selected === 'string')
87
+ props.value =
88
+ i === 1 && typeof selected === 'string'
89
+ ? selected
90
+ : `${String(props.value ?? 'item')}-${i}`;
91
+ return {...slot, props};
92
+ });
93
+ }
94
+
95
+ /**
96
+ * The props a component renders with: its doc's playground defaults, or,
97
+ * for a doc with none (an older Core), a sample for each required prop.
98
+ * A group with no children gets three of the items its doc suggests. Null
99
+ * for a doc with no defaults that has no theming targets (a hook or a
100
+ * helper), or that is a part its parent lists as an item: it works only
101
+ * inside the parent, whose own entry shows it.
102
+ * @param {import('./_project.mjs').ThemeProject} project
103
+ * @param {string} name
104
+ * @returns {Record<string, unknown> | null}
105
+ */
106
+ export function demoProps(project, name) {
107
+ const info = project.components[name];
108
+ const given =
109
+ info.defaults && typeof info.defaults === 'object'
110
+ ? /** @type {Record<string, unknown>} */ ({...info.defaults})
111
+ : null;
112
+ if (
113
+ !given &&
114
+ // Only a component a theme styles: hooks, providers and helpers have
115
+ // docs too, and render nothing.
116
+ (!/^[A-Z]/.test(name) ||
117
+ !info.targets.length ||
118
+ // A doc that declares no props of its own name is a family page,
119
+ // not a component the Core exports.
120
+ !Object.keys(info.props ?? {}).length ||
121
+ (info.subComponentOf && isItemOf(project, name)))
122
+ )
123
+ return null;
124
+ const props = given ?? {};
125
+ if (!given)
126
+ for (const [p, d] of Object.entries(info.props ?? {})) {
127
+ if (!d.required || /^on[A-Z]/.test(p)) continue;
128
+ if (p === 'children' && d.slots.length) continue;
129
+ const v = sampleOf(name, d.type);
130
+ // A required prop with no plain sample, such as a render function:
131
+ // the component cannot be shown from its doc alone.
132
+ if (v === undefined) return null;
133
+ props[p] = v;
134
+ }
135
+ const kids = info.props?.children;
136
+ const slot = kids?.slots?.[0];
137
+ if (!('children' in props) && kids) {
138
+ if (slot?.__element && (kids.required || slot.__element.startsWith(name))) {
139
+ if (info.props.value && !('value' in props) && slot.props?.value != null)
140
+ props.value = `${String(slot.props.value)}-1`;
141
+ props.children = slotItems(slot, props.value);
142
+ }
143
+ }
144
+ return props;
145
+ }
146
+
147
+ /**
148
+ * Whether a part's parent lists it as an item of its children.
149
+ * @param {import('./_project.mjs').ThemeProject} project
150
+ * @param {string} name
151
+ */
152
+ export function isItemOf(project, name) {
153
+ const parent = project.components[name]?.subComponentOf;
154
+ const slots = parent
155
+ ? (project.components[parent]?.props?.children?.slots ?? [])
156
+ : [];
157
+ return slots.some(s => s?.__element === name);
158
+ }
159
+
160
+ /**
161
+ * A component's catalog entries, all from its doc: its demo props
162
+ * ({@link demoProps}), then once per value of each visual prop with a
163
+ * closed set of values (`variant`, `status`, `color`, and a number union
164
+ * such as a heading's level), and its checked and disabled states. An
165
+ * overlay the playground shows in place renders open; on a page people
166
+ * see, once, as some of its variants size the window, not the card.
167
+ * @param {import('./_project.mjs').ThemeProject} project
168
+ * @param {string} name
169
+ * @param {number} cap at most this many
170
+ * @param {{shown?: boolean}} [opts] `shown`: for a page people see
171
+ * @returns {Array<{label: string, props: Record<string, unknown>}>}
172
+ */
173
+ export function catalogVariants(project, name, cap, opts = {}) {
174
+ const info = project.components[name];
175
+ const demo = demoProps(project, name);
176
+ if (!demo) return [];
177
+ if (info.opens && opts.shown)
178
+ return [
179
+ {label: '', props: 'isOpen' in demo ? demo : {...demo, isOpen: true}},
180
+ ];
181
+ const base =
182
+ info.opens && !('isOpen' in demo) ? {...demo, isOpen: true} : demo;
183
+ const values = info.targets[0]?.values ?? {};
184
+ const variants = [{label: '', props: base}];
185
+ for (const p of ['variant', 'status', 'color'])
186
+ for (const v of values[p] ?? [])
187
+ variants.push({
188
+ label: v,
189
+ // A status like `{type: 'error', message?: string}` takes the
190
+ // value as its `type`; the bare string breaks the component.
191
+ props: {
192
+ ...base,
193
+ [p]: info.props?.[p]?.type.startsWith('{') ? {type: v} : v,
194
+ },
195
+ });
196
+ // The states the doc names that a prop sets: checked (a boolean value)
197
+ // and disabled. The label says the state shown.
198
+ const states = new Set(info.targets.flatMap(t => t.states ?? []));
199
+ if (states.has('checked') && info.props?.value?.type.startsWith('boolean')) {
200
+ const on = base.value !== true;
201
+ variants.push({
202
+ label: on ? 'on' : 'off',
203
+ props: {
204
+ ...base,
205
+ value: on,
206
+ ...(typeof base.label === 'string' ? {label: on ? 'On' : 'Off'} : {}),
207
+ },
208
+ });
209
+ }
210
+ if (states.has('disabled') && info.props?.isDisabled)
211
+ variants.push({label: 'disabled', props: {...base, isDisabled: true}});
212
+ for (const p of Object.keys(values)) {
213
+ const type = info.props?.[p]?.type ?? '';
214
+ if (!/^\s*\d+(\s*\|\s*\d+)*\s*$/.test(type)) continue;
215
+ for (const n of type.split('|').map(Number))
216
+ if (n !== base[p])
217
+ variants.push({label: `${p} ${n}`, props: {...base, [p]: n}});
218
+ }
219
+ return variants.slice(0, cap);
220
+ }
221
+
222
+ /**
223
+ * The catalog: each component with playground defaults, once per visual
224
+ * variant value ({@link catalogVariants}), and the sample page.
52
225
  * @param {import('./_project.mjs').ThemeProject} project
53
226
  * @returns {{items: import('./_catalog.mjs').CatalogItem[], imports: Record<string, string>}}
54
227
  */
55
228
  export function catalogItems(project) {
56
229
  /** @type {import('./_catalog.mjs').CatalogItem[]} */
57
230
  const items = [];
58
- for (const [name, info] of Object.entries(project.components)) {
59
- const defaults = /** @type {any} */ (info.defaults);
60
- if (!defaults || typeof defaults !== 'object') continue;
61
- const values = info.targets[0]?.values ?? {};
62
- /** @type {Array<Record<string, string>>} */
63
- const variants = [{}];
64
- for (const p of ['variant', 'status', 'color'])
65
- for (const v of values[p] ?? []) variants.push({[p]: v});
66
- for (const v of variants.slice(0, 16)) {
67
- const label = Object.values(v).join(' ');
231
+ for (const name of Object.keys(project.components))
232
+ for (const v of catalogVariants(project, name, 16))
68
233
  items.push({
69
234
  name,
70
- label,
71
- tree: {__element: name, props: {...defaults, ...v}},
235
+ label: v.label,
236
+ tree: {__element: name, props: v.props},
72
237
  });
73
- }
74
- }
75
- for (const [name, list] of [...EXTRA, ...OPEN_OVERLAYS])
76
- for (const [label, tree] of list) items.push({name, label, tree});
77
238
  /** @type {Record<string, string>} */
78
239
  const imports = {};
79
240
  for (const n of elementNames([items.map(i => i.tree), SAMPLE_PAGE])) {
@@ -83,6 +244,27 @@ export function catalogItems(project) {
83
244
  return {items, imports};
84
245
  }
85
246
 
247
+ /**
248
+ * What of React the project cannot resolve (react, react-dom/server), to
249
+ * render the editor's pages; empty when both resolve.
250
+ * @param {string} cwd
251
+ * @returns {string[]}
252
+ */
253
+ export function reactMissing(cwd) {
254
+ const req = createRequire(path.join(cwd, 'package.json'));
255
+ return ['react', 'react-dom/server'].filter(m => {
256
+ try {
257
+ req.resolve(m);
258
+ return false;
259
+ } catch {
260
+ return true;
261
+ }
262
+ });
263
+ }
264
+
265
+ /** How long the editor's own pages may take to render. */
266
+ const EDITOR_RENDER_MS = 3 * RENDER_TIMEOUT_MS;
267
+
86
268
  /** One name per process, so two commands at once never share a folder. */
87
269
  const RUN = `${process.pid}-${Math.random().toString(36).slice(2, 8)}`;
88
270
 
@@ -96,6 +278,43 @@ const RUN = `${process.pid}-${Math.random().toString(36).slice(2, 8)}`;
96
278
  export const renderDir = project =>
97
279
  path.join(project.cwd, 'themes', `.astryx-render-${RUN}`);
98
280
 
281
+ /**
282
+ * Remove render folders a stopped command left (killed, out of memory):
283
+ * only `.astryx-render-<pid>-<random>` folders more than ten minutes old.
284
+ * Links are never followed.
285
+ * @param {string} themes
286
+ */
287
+ function sweepRenders(themes) {
288
+ let names;
289
+ try {
290
+ names = fs.readdirSync(themes);
291
+ } catch {
292
+ return;
293
+ }
294
+ for (const n of names) {
295
+ const m = /^\.astryx-render-(\d+)-[a-z0-9]+$/.exec(n);
296
+ if (!m || Number(m[1]) === process.pid) continue;
297
+ const p = path.join(themes, n);
298
+ // A process id from another process space may look gone or alive here,
299
+ // so age decides: a render takes at most three minutes, and a folder
300
+ // older than ten is a leftover.
301
+ /** @type {number} */
302
+ let age;
303
+ try {
304
+ age = Date.now() - fs.lstatSync(p).mtimeMs;
305
+ } catch {
306
+ continue;
307
+ }
308
+ if (age < 10 * 60_000) continue;
309
+ try {
310
+ if (fs.lstatSync(p).isDirectory())
311
+ fs.rmSync(p, {recursive: true, force: true});
312
+ } catch {
313
+ // Gone already.
314
+ }
315
+ }
316
+ }
317
+
99
318
  /**
100
319
  * Render a generated page module and give its HTML and markup (the body,
101
320
  * without the embedded source block).
@@ -111,6 +330,7 @@ export async function renderGenerated(project, file, source, extra = []) {
111
330
  const themes = path.dirname(dir);
112
331
  // A themes/ folder that holds only other renders was made by another
113
332
  // lookup running beside this one, so it is ours to remove too.
333
+ sweepRenders(themes);
114
334
  const madeThemes =
115
335
  !fs.existsSync(themes) ||
116
336
  (() => {
@@ -129,22 +349,47 @@ export async function renderGenerated(project, file, source, extra = []) {
129
349
  process.once('SIGINT', onSignal);
130
350
  process.once('SIGTERM', onSignal);
131
351
  const entry = path.join(dir, file);
132
- fs.writeFileSync(entry, source);
352
+ // Demo images point at the docs site's own folder (`/template-assets/…`),
353
+ // which a page opened from disk cannot reach: the inline placeholder
354
+ // takes their place, as when a template is scaffolded.
355
+ const {stripTemplateAssetRefs} =
356
+ await import('../../foundation/discovery/template-adapter.mjs');
357
+ fs.writeFileSync(entry, stripTemplateAssetRefs(source));
133
358
  for (const x of extra) {
134
359
  fs.mkdirSync(path.dirname(path.join(dir, x.name)), {recursive: true});
135
360
  fs.copyFileSync(x.from, path.join(dir, x.name));
136
361
  }
137
362
  try {
138
- const page = await renderPage(entry, {title: file.replace(/\.[^.]+$/, '')});
139
- const body = page.html.slice(page.html.indexOf('<body>') + 6);
363
+ // The editor's pages render every component, and a busy machine slows
364
+ // them down: they get three times the page limit.
365
+ const page = await renderPage(entry, {
366
+ title: file.replace(/\.[^.]+$/, ''),
367
+ timeoutMs: EDITOR_RENDER_MS,
368
+ });
369
+ // The page is the same whichever command rendered it: the folder's
370
+ // per-process name never reaches the file.
371
+ const html = page.html.split(path.basename(dir)).join('.astryx-render');
372
+ const body = html.slice(html.indexOf('<body>') + 6);
140
373
  const end = body.indexOf('<script type=');
141
- return {html: page.html, markup: end >= 0 ? body.slice(0, end) : body};
374
+ return {html, markup: end >= 0 ? body.slice(0, end) : body};
142
375
  } catch (e) {
143
376
  if (e instanceof RenderError) {
144
377
  const missing = e.code === 'ERR_RENDER_MISSING_DEPENDENCY';
378
+ // A render stopped by its time limit on the editor's own page is the
379
+ // machine being busy, not a loop in the page.
380
+ const slow = e.code === 'ERR_RENDER_TIMEOUT';
145
381
  throw new AstryxError(
146
- `Could not render the components with this project's React: ${e.message}${missing ? ' The theme command renders components on the server, so react and react-dom must be installed.' : ''}`,
147
- [],
382
+ slow
383
+ ? `The components did not finish rendering within ${EDITOR_RENDER_MS / 1000} seconds, so the render was stopped. The theme files are written; only its pages were not rendered. The machine may be busy: run show again when it is less loaded.`
384
+ : `Could not render the components with this project's React: ${e.message}${missing ? ' The theme command renders components on the server, so react and react-dom must be installed.' : ''}`,
385
+ missing
386
+ ? [
387
+ {
388
+ name: installLine(project.cwd, ['react', 'react-dom']),
389
+ reason: 'install them',
390
+ },
391
+ ]
392
+ : [],
148
393
  missing
149
394
  ? ERROR_CODES.ERR_RENDER_MISSING_DEPENDENCY
150
395
  : ERROR_CODES.ERR_RENDER_FAILED,
@@ -156,7 +401,8 @@ export async function renderGenerated(project, file, source, extra = []) {
156
401
  // it, so a render running beside this one keeps its own.
157
402
  for (const f of [entry, ...extra.map(x => path.join(dir, x.name))])
158
403
  fs.rmSync(f, {force: true});
159
- removeEmpty(madeThemes ? themes : dir);
404
+ // Another render may still use it: a folder with a file in it stays.
405
+ removeEmptyFolder(madeThemes ? themes : dir, {deep: true});
160
406
  process.removeListener('SIGINT', onSignal);
161
407
  process.removeListener('SIGTERM', onSignal);
162
408
  }
@@ -165,21 +411,6 @@ export async function renderGenerated(project, file, source, extra = []) {
165
411
  /** @param {unknown} v @returns {v is Record<string, unknown>} */
166
412
  const isRecord = v => typeof v === 'object' && v !== null && !Array.isArray(v);
167
413
 
168
- /**
169
- * Remove `dir` and the folders under it when no file is left in them.
170
- * @param {string} dir
171
- */
172
- function removeEmpty(dir) {
173
- if (!fs.existsSync(dir)) return;
174
- for (const e of fs.readdirSync(dir, {withFileTypes: true}))
175
- if (e.isDirectory()) removeEmpty(path.join(dir, e.name));
176
- try {
177
- fs.rmdirSync(dir);
178
- } catch {
179
- // Not empty: another render is using it.
180
- }
181
- }
182
-
183
414
  /**
184
415
  * The part data for the installed Core, from the cache or built now.
185
416
  * @param {import('./_project.mjs').ThemeProject} project
@@ -280,22 +511,6 @@ export function declarations(css) {
280
511
  return decls;
281
512
  }
282
513
 
283
- const VOID = new Set([
284
- 'area',
285
- 'base',
286
- 'br',
287
- 'col',
288
- 'embed',
289
- 'hr',
290
- 'img',
291
- 'input',
292
- 'link',
293
- 'meta',
294
- 'source',
295
- 'track',
296
- 'wbr',
297
- ]);
298
-
299
514
  /** @param {string} v */
300
515
  const refs = v => [...v.matchAll(/var\((--[a-zA-Z0-9_-]+)/g)].map(m => m[1]);
301
516
 
@@ -337,7 +552,6 @@ export function walkMarkup(html, decls, tokens) {
337
552
  const stack = [{env: {}, target: null, fg: null, bg: null}];
338
553
  /** @type {Map<string, TextPair>} */
339
554
  const pairs = new Map();
340
- let inSample = false;
341
555
  for (const m of html.matchAll(
342
556
  /([^<]+)|<(\/?)([a-zA-Z][a-zA-Z0-9-]*)([^>]*?)(\/?)>/g,
343
557
  )) {
@@ -356,13 +570,10 @@ export function walkMarkup(html, decls, tokens) {
356
570
  }
357
571
  continue;
358
572
  }
359
- if (!close && tag === 'section')
360
- inSample = /data-cat="sample-page"/.test(attrs);
361
573
  if (close) {
362
574
  if (stack.length > 1) stack.pop();
363
575
  continue;
364
576
  }
365
- void inSample;
366
577
  const cls = (/class="([^"]*)"/.exec(attrs)?.[1] ?? '')
367
578
  .split(/\s+/)
368
579
  .filter(Boolean);
@@ -11,18 +11,18 @@
11
11
  export function decodePng(bytes: Buffer): Picture;
12
12
  /**
13
13
  * The exact color at each point, with its alpha (0 is fully see-through,
14
- * 1 fully opaque). Every point must be inside the picture.
14
+ * 1 fully opaque). A point outside the picture has no color.
15
15
  * @param {Picture} pic
16
16
  * @param {Array<{x: number, y: number}>} points
17
- * @returns {Array<{at: string, color: string, alpha: number}>}
17
+ * @returns {Array<{at: string, color: string | null, alpha: number | null}>}
18
18
  */
19
19
  export function colorsAt(pic: Picture, points: Array<{
20
20
  x: number;
21
21
  y: number;
22
22
  }>): Array<{
23
23
  at: string;
24
- color: string;
25
- alpha: number;
24
+ color: string | null;
25
+ alpha: number | null;
26
26
  }>;
27
27
  /**
28
28
  * The main colors of a picture: every pixel (on a grid past 4 million)
@@ -222,13 +222,15 @@ const hx = v => Math.round(v).toString(16).padStart(2, '0');
222
222
 
223
223
  /**
224
224
  * The exact color at each point, with its alpha (0 is fully see-through,
225
- * 1 fully opaque). Every point must be inside the picture.
225
+ * 1 fully opaque). A point outside the picture has no color.
226
226
  * @param {Picture} pic
227
227
  * @param {Array<{x: number, y: number}>} points
228
- * @returns {Array<{at: string, color: string, alpha: number}>}
228
+ * @returns {Array<{at: string, color: string | null, alpha: number | null}>}
229
229
  */
230
230
  export function colorsAt(pic, points) {
231
231
  return points.map(({x, y}) => {
232
+ if (x < 0 || y < 0 || x >= pic.width || y >= pic.height)
233
+ return {at: `${x},${y}`, color: null, alpha: null};
232
234
  const i = (y * pic.width + x) * 4;
233
235
  return {
234
236
  at: `${x},${y}`,
@@ -1,6 +1,12 @@
1
1
  // @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
2
2
  // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
3
 
4
+ /**
5
+ * For tests: stop the next preview changes at the step where `when`
6
+ * returns true, as a kill would, with nothing put back. Null clears it.
7
+ * @param {((step: string) => boolean) | null} when
8
+ */
9
+ export function stopPreviewCommitAt(when: ((step: string) => boolean) | null): void;
4
10
  /**
5
11
  * Rewrite the in-app preview routes of a theme when their imports changed, so
6
12
  * a font package installed after `set` loads on the next change or `show`,
@@ -44,6 +50,14 @@ export function themeEditPreviewAdd(description?: string, options?: import("./th
44
50
  * @returns {Promise<import('./theme-edit.type.mjs').ThemeEditPreviewAppResponse>}
45
51
  */
46
52
  export function themeEditPreviewApp(dir: string, options?: import("./theme-edit.type.mjs").ThemeEditPreviewAppOptions): Promise<import("./theme-edit.type.mjs").ThemeEditPreviewAppResponse>;
53
+ /**
54
+ * Why the in-app preview's record is damaged, with how to fix it, or null
55
+ * (no preview, or a sound one). For answers that would suggest a preview
56
+ * command, which the damage refuses.
57
+ * @param {string} cwd
58
+ * @returns {string | null}
59
+ */
60
+ export function previewRecordProblem(cwd: string): string | null;
47
61
  /**
48
62
  * The pages of the in-app theme preview. With no preview, and with no
49
63
  * routes folder for one, the list is empty; a damaged theme-preview.json is