@astryxdesign/cli 0.7.15 → 0.7.16-canary.g9b5ad42

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 (81) hide show
  1. package/README.md +5 -3
  2. package/api/doctor/_adapter.d.mts +17 -0
  3. package/api/doctor/_adapter.mjs +35 -0
  4. package/api/doctor/doctor.mjs +18 -4
  5. package/api/integration/validate-integration.mjs +1 -1
  6. package/api/theme/_adapter.d.mts +5 -2
  7. package/api/theme/_adapter.mjs +43 -12
  8. package/api/theme/build/build.d.mts +1 -21
  9. package/api/theme/build/build.mjs +149 -37
  10. package/api/theme/eject/eject.mjs +22 -1
  11. package/api/theme/template/template.d.mts +2 -0
  12. package/api/theme/template/template.mjs +41 -12
  13. package/api/theme/theme.type.d.mts +4 -0
  14. package/api/theme/theme.type.mjs +2 -1
  15. package/api/theme/themeBuild.doc.mjs +3 -3
  16. package/api/theme/themeEject.doc.mjs +1 -1
  17. package/api/theme/themeTemplate.doc.mjs +9 -3
  18. package/api/theme-edit/_adapter.d.mts +10 -0
  19. package/api/theme-edit/_adapter.mjs +76 -5
  20. package/api/theme-edit/_check.mjs +181 -50
  21. package/api/theme-edit/_color.d.mts +2 -8
  22. package/api/theme-edit/_color.mjs +126 -55
  23. package/api/theme-edit/_compiler.d.mts +1 -0
  24. package/api/theme-edit/_compiler.mjs +3 -0
  25. package/api/theme-edit/_context.d.mts +7 -0
  26. package/api/theme-edit/_context.mjs +34 -1
  27. package/api/theme-edit/_css.d.mts +21 -1
  28. package/api/theme-edit/_css.mjs +172 -13
  29. package/api/theme-edit/_edit.d.mts +9 -0
  30. package/api/theme-edit/_edit.mjs +377 -48
  31. package/api/theme-edit/_how-commands.d.mts +7 -26
  32. package/api/theme-edit/_how-commands.mjs +27 -1
  33. package/api/theme-edit/_how-question.mjs +330 -12
  34. package/api/theme-edit/_how.mjs +68 -26
  35. package/api/theme-edit/_palette.mjs +10 -0
  36. package/api/theme-edit/_parts.d.mts +16 -0
  37. package/api/theme-edit/_parts.mjs +49 -1
  38. package/api/theme-edit/_preview-app.mjs +176 -9
  39. package/api/theme-edit/_preview.mjs +7 -3
  40. package/api/theme-edit/_project.d.mts +11 -0
  41. package/api/theme-edit/_project.mjs +35 -9
  42. package/api/theme-edit/_settings.d.mts +46 -5
  43. package/api/theme-edit/_settings.mjs +217 -25
  44. package/api/theme-edit/theme-edit.type.d.mts +2 -1
  45. package/api/theme-edit/theme-edit.type.mjs +1 -1
  46. package/api/theme-edit/themeEditNew.doc.mjs +1 -1
  47. package/api/theme-edit/themeEditShow.doc.mjs +1 -1
  48. package/assets/docs/tree/tokens-and-setup.doc.mjs +39 -3
  49. package/assets/docs.generated.json +2 -2
  50. package/assets/preview-app/next.config.mjs +7 -1
  51. package/assets/self-audit.generated.json +1 -1
  52. package/assets/templates/blocks/components/Table/ColumnsVirtualizationHookUsage.doc.mjs +14 -0
  53. package/assets/templates/blocks/components/Table/ColumnsVirtualizationHookUsage.tsx +53 -0
  54. package/assets/templates/blocks/components/Table/InfiniteScrollHookUsage.doc.mjs +14 -0
  55. package/assets/templates/blocks/components/Table/InfiniteScrollHookUsage.tsx +33 -0
  56. package/assets/templates/components.generated.json +1 -1
  57. package/clients/cli/build-id.generated.json +1 -1
  58. package/clients/cli/commands/doctor.doc.mjs +2 -2
  59. package/clients/cli/commands/experimental/theme.mjs +113 -34
  60. package/clients/cli/commands/experimental-theme-compile.doc.mjs +1 -1
  61. package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +1 -1
  62. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
  63. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  64. package/clients/cli/commands/experimental-theme-eject.doc.mjs +1 -1
  65. package/clients/cli/commands/experimental-theme-template.doc.mjs +1 -1
  66. package/clients/cli/commands/theme-build.doc.mjs +2 -2
  67. package/clients/cli/commands/theme-eject.doc.mjs +1 -1
  68. package/clients/cli/commands/theme-template.doc.mjs +3 -1
  69. package/clients/cli/commands/theme.mjs +85 -5
  70. package/clients/cli/manifest.generated.json +1 -1
  71. package/foundation/config/project.mjs +50 -14
  72. package/foundation/fs/gather-cache.d.mts +9 -2
  73. package/foundation/fs/gather-cache.mjs +36 -10
  74. package/foundation/fs/paths.d.mts +16 -0
  75. package/foundation/fs/paths.mjs +25 -0
  76. package/foundation/response/error-codes.d.mts +2 -1
  77. package/foundation/response/error-codes.d.ts +1 -0
  78. package/foundation/response/error-codes.doc.mjs +5 -0
  79. package/foundation/response/error-codes.mjs +3 -0
  80. package/foundation/response/response-types.doc.mjs +1 -1
  81. package/package.json +7 -6
@@ -28,7 +28,7 @@ import {
28
28
  familyOf,
29
29
  } from './_tables.mjs';
30
30
  import {colorsAt, decodePng, mainColors} from './_png.mjs';
31
- import {baseThemeFile, installLine} from './_project.mjs';
31
+ import {baseInstalled, installLine} from './_project.mjs';
32
32
  import {loadTheme, nearest, refuse, themeSourceProblem} from './_settings.mjs';
33
33
  import {ASPECTS, checkSetting} from './_check.mjs';
34
34
  import {MODAL} from './_compiler.mjs';
@@ -332,14 +332,8 @@ async function answer(subject, options, cwd) {
332
332
  const d = DIALS[key];
333
333
  // A base is named only when it is installed here; the others are
334
334
  // listed with the line that installs them.
335
- const installed = (/** @type {string} */ b) => {
336
- try {
337
- baseThemeFile(ctx.project, b);
338
- return true;
339
- } catch {
340
- return false;
341
- }
342
- };
335
+ const installed = (/** @type {string} */ b) =>
336
+ baseInstalled(ctx.project, b);
343
337
  const missing = key === 'base' ? LIKE.filter(b => !installed(b)) : [];
344
338
  return {
345
339
  kind: 'setting',
@@ -459,7 +453,7 @@ async function answer(subject, options, cwd) {
459
453
  };
460
454
  }
461
455
  // A question about the tool itself gets the command that answers it.
462
- const tool = toolAnswer(t, env);
456
+ const tool = toolAnswer(t, env, ctx.project.target);
463
457
  if (tool === 'list') {
464
458
  const kind =
465
459
  /\b(dials?|tokens?|parts?|groups?|properties|property)\b/i
@@ -650,21 +644,22 @@ function partAnswer(ctx, env, subject, key, unknown, themeFields) {
650
644
  const group = familyOf(key);
651
645
  // Only the states set takes on this part (it refuses one that never
652
646
  // applies there).
647
+ // Only the states set takes on this part: a state is dropped when the
648
+ // same change without it passes and with it is refused.
649
+ const takes = (/** @type {string | undefined} */ st) => {
650
+ try {
651
+ checkSetting(ctx, env.slug, env.data, 'opacity', '1', {
652
+ on: key,
653
+ ...(st ? {state: st} : {}),
654
+ });
655
+ return true;
656
+ } catch {
657
+ return false;
658
+ }
659
+ };
660
+ const plainOk = takes(undefined);
653
661
  const states = [...new Set([...target.states, ...COMMON_STATES])].filter(
654
- st => {
655
- try {
656
- checkSetting(ctx, env.slug, env.data, 'opacity', '1', {
657
- on: key,
658
- state: st,
659
- });
660
- return true;
661
- } catch (e) {
662
- return !(
663
- /** @type {any} */ (e)?.code === 'ERR_THEME_NO_EFFECT' &&
664
- /--state/.test(String(/** @type {any} */ (e)?.message))
665
- );
666
- }
667
- },
662
+ st => !plainOk || takes(st),
668
663
  );
669
664
  const reads = Object.fromEntries(
670
665
  Object.keys(GROUPS).map(g => [
@@ -937,9 +932,10 @@ function colorsAnswer(env, dialCommands) {
937
932
  * the caller lists the kind the question names.
938
933
  * @param {string} t
939
934
  * @param {import('./_how-commands.mjs').HowTheme} env
935
+ * @param {Map<string, unknown>} targets the parts Core styles
940
936
  * @returns {Array<{text: string, commands: string[]}> | 'list' | null}
941
937
  */
942
- function toolAnswer(t, env) {
938
+ function toolAnswer(t, env, targets) {
943
939
  const q = t
944
940
  .trim()
945
941
  .toLowerCase()
@@ -1011,7 +1007,53 @@ function toolAnswer(t, env) {
1011
1007
  commands: [`${P} new ${env.slug}-2 --like ${env.data.like ?? 'none'}`],
1012
1008
  },
1013
1009
  ];
1014
- if (/\b(version|which cli|what cli)\b/.test(q))
1010
+ // Screen-size, print and device conditions: the editor has none.
1011
+ if (
1012
+ /\bmedia quer|^(on|for) (mobile|phones?|tablets?|desktops?|print)$|^(print|printing|print styles?)$/.test(
1013
+ q,
1014
+ )
1015
+ )
1016
+ return [
1017
+ {
1018
+ text: 'the theme editor has no screen-size, print or device conditions: every setting applies on every screen; a media query needs your own CSS',
1019
+ commands: [`${HOW} --help`],
1020
+ },
1021
+ ];
1022
+ // Questions about what is set now, or the choices: a lookup, never a
1023
+ // write.
1024
+ const asks =
1025
+ /^(how (round|big|small|large|thick|dark|light|bold|wide|tall) (is|are)|what (is|are) the (current|available)|which \w+ (does|do|is|are)|what (font|color|colour|radius|accent) (does|do|is|are)|what (values|options|choices))\b|\b(right now|currently|at the moment)\??$|\bnow\?$/.exec(
1026
+ q,
1027
+ );
1028
+ if (asks || /^which parts (are|does|do)\b/.test(q)) {
1029
+ // The part or setting the question names gets its own lookup.
1030
+ const named = q
1031
+ .split(/[^a-z-]+/)
1032
+ .find(
1033
+ w =>
1034
+ w.length > 2 &&
1035
+ (targets.has(w) ||
1036
+ targets.has(w.replace(/s$/, '')) ||
1037
+ ['inputs', 'checks'].includes(w)),
1038
+ );
1039
+ const key =
1040
+ named && !targets.has(named) && !['inputs', 'checks'].includes(named)
1041
+ ? named.replace(/s$/, '')
1042
+ : named;
1043
+ return [
1044
+ {
1045
+ text: key
1046
+ ? `how ${key} says what ${key} reads and what sets it; show lists what ${env.slug} sets now`
1047
+ : `show lists what ${env.slug} sets now; how <part> or how <setting> says what a part reads and the values a setting takes`,
1048
+ commands: [...(key ? [`${HOW} ${key}`] : []), `${P} show ${env.slug}`],
1049
+ },
1050
+ ];
1051
+ }
1052
+ if (
1053
+ /\b((astryx|cli) version|version of (the )?(cli|astryx)|which cli|what cli)\b/.test(
1054
+ q,
1055
+ )
1056
+ )
1015
1057
  return [{text: 'the CLI says its version', commands: ['astryx --version']}];
1016
1058
  if (/\bwhere (are|is|do) .*\b(stored|saved|kept|live|files?)\b/.test(q))
1017
1059
  return [
@@ -1123,6 +1123,16 @@ function planPalette(cwd, slug, data, base) {
1123
1123
  families.push(family);
1124
1124
  }
1125
1125
  if (!families.length) return null;
1126
+ // The editor's colors stand in the order of their settings, wherever they
1127
+ // sit among a config's own families, so a step back writes the same file.
1128
+ const slots = families
1129
+ .map((f, i) => (PALETTE_DIALS.includes(f?.id) ? i : -1))
1130
+ .filter(i => i >= 0);
1131
+ const inOrder = fams
1132
+ .map(x => families.find(f => f?.id === x.id))
1133
+ .filter(Boolean);
1134
+ if (inOrder.length === slots.length)
1135
+ slots.forEach((i, k) => (families[i] = inOrder[k]));
1126
1136
  config.families = families;
1127
1137
  const dead = deadKeys(config);
1128
1138
  if (dead.length)
@@ -13,6 +13,14 @@
13
13
  * @returns {Record<string, unknown> | null}
14
14
  */
15
15
  export function demoProps(project: import("./_project.mjs").ThemeProject, name: string): Record<string, unknown> | null;
16
+ /**
17
+ * Whether a part works only inside its parent: its parent lists it as an
18
+ * item, or it is a piece of its parent by name (TableBody of Table) and
19
+ * its doc gives no playground of its own.
20
+ * @param {import('./_project.mjs').ThemeProject} project
21
+ * @param {string} name
22
+ */
23
+ export function needsParent(project: import("./_project.mjs").ThemeProject, name: string): boolean;
16
24
  /**
17
25
  * Whether a part's parent lists it as an item of its children.
18
26
  * @param {import('./_project.mjs').ThemeProject} project
@@ -77,6 +85,14 @@ export function renderGenerated(project: import("./_project.mjs").ThemeProject,
77
85
  * @returns {Promise<PartData>}
78
86
  */
79
87
  export function partData(project: import("./_project.mjs").ThemeProject): Promise<PartData>;
88
+ /**
89
+ * The tokens Core's stylesheet multiplies by another value inside calc()
90
+ * (`var(--text-body-size) * var(--text-body-leading)`): a unit there makes
91
+ * the declaration invalid. Empty with no stylesheet.
92
+ * @param {any} project
93
+ * @returns {Set<string>}
94
+ */
95
+ export function multipliedTokens(project: any): Set<string>;
80
96
  /**
81
97
  * Atomic class -> its declarations, with the condition (pseudo-class,
82
98
  * attribute) each one applies under.
@@ -118,7 +118,7 @@ export function demoProps(project, name) {
118
118
  // A doc that declares no props of its own name is a family page,
119
119
  // not a component the Core exports.
120
120
  !Object.keys(info.props ?? {}).length ||
121
- (info.subComponentOf && isItemOf(project, name)))
121
+ needsParent(project, name))
122
122
  )
123
123
  return null;
124
124
  const props = given ?? {};
@@ -144,6 +144,22 @@ export function demoProps(project, name) {
144
144
  return props;
145
145
  }
146
146
 
147
+ /**
148
+ * Whether a part works only inside its parent: its parent lists it as an
149
+ * item, or it is a piece of its parent by name (TableBody of Table) and
150
+ * its doc gives no playground of its own.
151
+ * @param {import('./_project.mjs').ThemeProject} project
152
+ * @param {string} name
153
+ */
154
+ export function needsParent(project, name) {
155
+ const info = project.components[name];
156
+ const parent = info?.subComponentOf;
157
+ if (!parent) return false;
158
+ if (isItemOf(project, name)) return true;
159
+ const own = info.defaults && typeof info.defaults === 'object';
160
+ return !own && name.startsWith(parent) && name !== parent;
161
+ }
162
+
147
163
  /**
148
164
  * Whether a part's parent lists it as an item of its children.
149
165
  * @param {import('./_project.mjs').ThemeProject} project
@@ -479,6 +495,38 @@ async function buildPartData(project) {
479
495
  };
480
496
  }
481
497
 
498
+ /** @type {Map<string, Set<string>>} */
499
+ const multipliedCache = new Map();
500
+
501
+ /**
502
+ * The tokens Core's stylesheet multiplies by another value inside calc()
503
+ * (`var(--text-body-size) * var(--text-body-leading)`): a unit there makes
504
+ * the declaration invalid. Empty with no stylesheet.
505
+ * @param {any} project
506
+ * @returns {Set<string>}
507
+ */
508
+ export function multipliedTokens(project) {
509
+ /** @type {string} */
510
+ let file;
511
+ try {
512
+ file = project.req.resolve('@astryxdesign/core/astryx.css');
513
+ } catch {
514
+ return new Set();
515
+ }
516
+ const cached = multipliedCache.get(file);
517
+ if (cached) return cached;
518
+ const css = fs.readFileSync(file, 'utf8');
519
+ /** @type {Set<string>} */
520
+ const out = new Set();
521
+ // Each side of a `*` on its own, so `a * b * c` names all three.
522
+ for (const m of css.matchAll(/var\((--[a-z0-9-]+)\)(?=\s*\*)/gi))
523
+ out.add(m[1].toLowerCase());
524
+ for (const m of css.matchAll(/\*\s*var\((--[a-z0-9-]+)\)/gi))
525
+ out.add(m[1].toLowerCase());
526
+ multipliedCache.set(file, out);
527
+ return out;
528
+ }
529
+
482
530
  /**
483
531
  * Atomic class -> its declarations, with the condition (pseudo-class,
484
532
  * attribute) each one applies under.
@@ -204,11 +204,12 @@ function readNextConfig(cwd) {
204
204
  basePath: undefined,
205
205
  distDir: undefined,
206
206
  };
207
+ // The order Next loads them in; it refuses next.config.cjs.
207
208
  for (const name of [
208
- 'next.config.mjs',
209
209
  'next.config.js',
210
+ 'next.config.mjs',
210
211
  'next.config.ts',
211
- 'next.config.cjs',
212
+ 'next.config.mts',
212
213
  ]) {
213
214
  /** @type {string} */
214
215
  let text;
@@ -270,6 +271,11 @@ function readNextConfig(cwd) {
270
271
  let exported = null;
271
272
  for (const st of body) {
272
273
  if (st.type === 'ExportDefaultDeclaration') exported = st.declaration;
274
+ // export {config as default}
275
+ if (st.type === 'ExportNamedDeclaration' && !st.source)
276
+ for (const sp of st.specifiers ?? [])
277
+ if ((sp.exported?.name ?? sp.exported?.value) === 'default')
278
+ exported = sp.local;
273
279
  if (
274
280
  st.type === 'ExpressionStatement' &&
275
281
  st.expression.type === 'AssignmentExpression' &&
@@ -281,6 +287,66 @@ function readNextConfig(cwd) {
281
287
  }
282
288
  const config = exported && configOf(exported);
283
289
  if (!config) return exported ? unknown : none;
290
+ const wrapped = resolve(exported)?.type === 'CallExpression';
291
+ // Changes to the config after it is made: `config.key = value` sets a
292
+ // key (the last one counts), and anything else (Object.assign, a
293
+ // computed key) makes every key unknown.
294
+ const names = new Set(
295
+ [...consts]
296
+ .filter(([, init]) => resolve(init) === config)
297
+ .map(([n]) => n),
298
+ );
299
+ /** @type {Map<string, any>} */
300
+ const later = new Map();
301
+ let changed = false;
302
+ // Only a statement at the top of the file surely runs: a key set inside
303
+ // an `if` or a function, or deleted, is known only by running it.
304
+ const UNSURE = Symbol('unsure');
305
+ const topLevel = new Set(
306
+ body
307
+ .filter(st => st.type === 'ExpressionStatement')
308
+ .map(st => st.expression),
309
+ );
310
+ /** @param {any} n */
311
+ const visit = n => {
312
+ if (!n || typeof n !== 'object') return;
313
+ if (Array.isArray(n)) return n.forEach(visit);
314
+ if (
315
+ n.type === 'AssignmentExpression' &&
316
+ n.left?.type === 'MemberExpression' &&
317
+ names.has(n.left.object?.name)
318
+ ) {
319
+ if (n.left.computed) changed = true;
320
+ else
321
+ later.set(
322
+ n.left.property.name,
323
+ topLevel.has(n) && n.operator === '=' ? n.right : UNSURE,
324
+ );
325
+ }
326
+ if (
327
+ n.type === 'UnaryExpression' &&
328
+ n.operator === 'delete' &&
329
+ n.argument?.type === 'MemberExpression' &&
330
+ names.has(n.argument.object?.name)
331
+ ) {
332
+ if (n.argument.computed) changed = true;
333
+ else later.set(n.argument.property.name, UNSURE);
334
+ }
335
+ if (
336
+ n.type === 'CallExpression' &&
337
+ n.arguments?.some(
338
+ (/** @type {any} */ a) =>
339
+ a.type === 'Identifier' && names.has(a.name),
340
+ ) &&
341
+ n.callee?.type === 'MemberExpression' &&
342
+ n.callee.object?.name === 'Object'
343
+ )
344
+ changed = true;
345
+ for (const [k, v] of Object.entries(n))
346
+ if (k !== 'loc' && k !== 'start' && k !== 'end') visit(v);
347
+ };
348
+ visit(body);
349
+ if (changed) return unknown;
284
350
  // The config's keys in order, with a spread of a constant object read
285
351
  // in place; a spread of anything else may set any key (`null`).
286
352
  /** @type {Array<{key: string, value: any} | null>} */
@@ -302,8 +368,10 @@ function readNextConfig(cwd) {
302
368
  }
303
369
  };
304
370
  flatten(config, 0);
305
- /** The entry that sets a key last; null when an unreadable spread may; undefined when nothing does. @param {string} key */
371
+ /** The entry that sets a key last: a later assignment, else the last key; null when an unreadable spread may; undefined when nothing does. @param {string} key */
306
372
  const prop = key => {
373
+ if (later.has(key))
374
+ return later.get(key) === UNSURE ? null : {key, value: later.get(key)};
307
375
  for (let i = entries.length - 1; i >= 0; i--) {
308
376
  const e = entries[i];
309
377
  if (e === null) return null;
@@ -332,10 +400,13 @@ function readNextConfig(cwd) {
332
400
  const out = n.elements.map((/** @type {any} */ e) => str(e));
333
401
  return out.every((/** @type {any} */ e) => e != null) ? out : null;
334
402
  }),
335
- basePath: value('basePath', n => {
336
- const v = str(n);
337
- return v == null ? null : v.replace(/\/+$/, '');
338
- }),
403
+ // A wrapper such as withX(config) may add a basePath of its own.
404
+ basePath: wrapped
405
+ ? null
406
+ : value('basePath', n => {
407
+ const v = str(n);
408
+ return v == null ? null : v.replace(/\/+$/, '');
409
+ }),
339
410
  distDir: value('distDir', n => {
340
411
  const v = str(n);
341
412
  return v == null
@@ -3008,6 +3079,25 @@ async function addPreview(description, options, box) {
3008
3079
  options.theme != null
3009
3080
  ? ` --theme ${quoteCommandArgument(options.theme)}`
3010
3081
  : '';
3082
+ // A default page's id (overview, components, colors), as `remove` takes
3083
+ // it, asks for the default pages back: the same as a bare add.
3084
+ const DEFAULT_IDS = ['overview', 'components', COLORS_PAGE.id];
3085
+ const isDefault = (/** @type {unknown} */ v) =>
3086
+ typeof v === 'string' && DEFAULT_IDS.includes(v.trim().toLowerCase());
3087
+ // Beside another page asked for, it is refused below (drop one).
3088
+ const blank = (/** @type {unknown} */ v) => v == null || !String(v).trim();
3089
+ const askedId = [description, options.template]
3090
+ .find(isDefault)
3091
+ ?.trim()
3092
+ .toLowerCase();
3093
+ const again =
3094
+ askedId != null &&
3095
+ (blank(description) || isDefault(description)) &&
3096
+ (blank(options.template) || isDefault(options.template));
3097
+ if (again) {
3098
+ description = undefined;
3099
+ options = {...options, template: undefined};
3100
+ }
3011
3101
  const said = (description ?? '').trim();
3012
3102
  const named =
3013
3103
  options.template == null ? null : String(options.template).trim();
@@ -3243,7 +3333,13 @@ async function addPreview(description, options, box) {
3243
3333
  writes.set(rel, w);
3244
3334
  if (!data.files.includes(rel)) data.files.push(rel);
3245
3335
  const had = data.pages.find(x => x.id === page.id);
3246
- if (!had) {
3336
+ // A recorded page whose file is gone is added back, not refreshed.
3337
+ if (!had || !fs.existsSync(path.join(at.dir, rel))) {
3338
+ if (had) {
3339
+ had.label = page.label;
3340
+ added.push(page.id);
3341
+ continue;
3342
+ }
3247
3343
  data.pages.push({
3248
3344
  id: page.id,
3249
3345
  label: page.label,
@@ -3328,6 +3424,19 @@ async function addPreview(description, options, box) {
3328
3424
  const worn = data.theme;
3329
3425
  if (worn)
3330
3426
  await themeLocked(options, worn, () => recordPreview(cwd, worn, at.dir));
3427
+ if (again)
3428
+ notes.push(
3429
+ `a default page's id brings back the default pages that are missing, as ${PREVIEW} add does`,
3430
+ );
3431
+ if (
3432
+ askedId === COLORS_PAGE.id &&
3433
+ !data.pages.some(p => p.id === COLORS_PAGE.id)
3434
+ )
3435
+ notes.push(
3436
+ data.theme
3437
+ ? `the Colors page shows a theme's generated colors, and ${data.theme} has none yet; it appears once it does: ${EDITOR} set ${data.theme} accent <color>`
3438
+ : `the Colors page shows a theme's generated colors, and the preview wears the app's own theme; it appears with an editor theme that has colors: ${PREVIEW} add --theme <theme>`,
3439
+ );
3331
3440
  return {
3332
3441
  type: 'themeEdit.previewAdd',
3333
3442
  data: {
@@ -3611,6 +3720,20 @@ export async function themeEditPreviewList(options = {}) {
3611
3720
  const themeMissing = Boolean(
3612
3721
  data?.theme && !fs.existsSync(themeFiles(cwd, data.theme).dir),
3613
3722
  );
3723
+ if (at && fs.existsSync(path.join(at.dir, PENDING)))
3724
+ notes.push(
3725
+ `a preview change was stopped part way, so this list may be out of date; ${PREVIEW} add${pf} finishes it`,
3726
+ );
3727
+ // A page whose file is gone breaks the route, which still imports it.
3728
+ const missing = new Set(
3729
+ (data?.pages ?? [])
3730
+ .filter(p => at && !fs.existsSync(path.join(at.dir, `${p.file}.tsx`)))
3731
+ .map(p => p.id),
3732
+ );
3733
+ if (at && missing.size)
3734
+ notes.push(
3735
+ `${[...missing].join(', ')} ${missing.size > 1 ? 'are' : 'is'} listed, but ${missing.size > 1 ? 'their files are' : 'its file is'} gone, so the route does not build; ${PREVIEW} add${pf} writes ${missing.size > 1 ? 'them' : 'it'} again`,
3736
+ );
3614
3737
  if (data?.theme && themeMissing) {
3615
3738
  const themes = editedThemes(cwd);
3616
3739
  notes.push(
@@ -3623,7 +3746,11 @@ export async function themeEditPreviewList(options = {}) {
3623
3746
  folder: at && data ? shownDir(cwd, at.dir) : null,
3624
3747
  theme: data?.theme ?? null,
3625
3748
  themeMissing,
3626
- pages: (data?.pages ?? []).map(x => ({id: x.id, label: x.label})),
3749
+ pages: (data?.pages ?? []).map(x => ({
3750
+ id: x.id,
3751
+ label: x.label,
3752
+ ...(missing.has(x.id) ? {missing: true} : {}),
3753
+ })),
3627
3754
  notes,
3628
3755
  },
3629
3756
  };
@@ -3658,6 +3785,24 @@ async function removePreview(page, options, box) {
3658
3785
  const retry = (/** @type {string | null} */ p) =>
3659
3786
  `${PREVIEW} remove${page != null ? ` ${quoteCommandArgument(page)}` : ''}${p == null ? '' : ` --path ${quoteCommandArgument(p)}`}`;
3660
3787
  const at = previewDir(cwd, options.path, {retry});
3788
+ // The pages a stopped change was removing, read before it is finished.
3789
+ /** @type {string[]} */
3790
+ let removing = [];
3791
+ try {
3792
+ const pend = JSON.parse(
3793
+ fs.readFileSync(path.join(at.dir, PENDING), 'utf8'),
3794
+ );
3795
+ const ids = (/** @type {any} */ r) =>
3796
+ Array.isArray(r?.pages)
3797
+ ? r.pages.map((/** @type {any} */ x) => String(x?.id))
3798
+ : [];
3799
+ const after = ids(pend?.record);
3800
+ removing = ids(pend?.old).filter(
3801
+ (/** @type {string} */ id) => !after.includes(id),
3802
+ );
3803
+ } catch {
3804
+ // No stopped change, or a damaged one.
3805
+ }
3661
3806
  const recovered = recoverPreview(cwd, at.dir);
3662
3807
  box.note = recovered;
3663
3808
  const data = readPreview(cwd, at.dir, pf);
@@ -3709,6 +3854,28 @@ async function removePreview(page, options, box) {
3709
3854
  one =
3710
3855
  data.pages.find(x => x.id === key) ??
3711
3856
  data.pages.find(x => x.id.toLowerCase() === key.toLowerCase());
3857
+ // The same remove run again after it was stopped: the change just
3858
+ // finished removed the page.
3859
+ if (
3860
+ !one &&
3861
+ recovered &&
3862
+ removing.some(id => id.toLowerCase() === key.toLowerCase())
3863
+ )
3864
+ return {
3865
+ type: 'themeEdit.previewRemove',
3866
+ data: {
3867
+ folder,
3868
+ removed: [],
3869
+ kept: [],
3870
+ missing: [],
3871
+ rewritten: [],
3872
+ notes: [
3873
+ ...notes,
3874
+ `${key} is no longer in the preview: the change that was stopped removed it`,
3875
+ ],
3876
+ pages: data.pages.map(x => x.id),
3877
+ },
3878
+ };
3712
3879
  if (!one)
3713
3880
  refuse(E.ERR_NOT_FOUND, `The preview has no page "${page}".`, {
3714
3881
  didYouMean: nearest(
@@ -24,7 +24,7 @@ import {
24
24
  } from './_catalog.mjs';
25
25
  import {
26
26
  catalogVariants,
27
- isItemOf,
27
+ needsParent,
28
28
  renderDir,
29
29
  renderGenerated,
30
30
  } from './_parts.mjs';
@@ -187,7 +187,7 @@ function inParentOf(project, name) {
187
187
  const wrapper = info.wrapper;
188
188
  // A part its parent does not list as an item, such as a heading of the
189
189
  // text family, renders on its own.
190
- if (!wrapper) return isItemOf(project, name) ? null : tree => tree;
190
+ if (!wrapper) return needsParent(project, name) ? null : tree => tree;
191
191
  if (project.components[wrapper.component]?.wrapper) return null;
192
192
  const props = Object.fromEntries(
193
193
  Object.entries(wrapper.props).filter(([k]) => !OPEN_PROPS.includes(k)),
@@ -984,7 +984,7 @@ export function previewRouteSource({
984
984
  ' ))}',
985
985
  ' </SideNav>',
986
986
  ' }>',
987
- ' <current.Page />',
987
+ ' {ready ? <current.Page /> : null}',
988
988
  ' </AppShell>',
989
989
  ' </div>',
990
990
  ];
@@ -1028,6 +1028,10 @@ export function previewRouteSource({
1028
1028
  '',
1029
1029
  'export default function ThemePreviewPage() {',
1030
1030
  ' const [page, setPage] = React.useState(PAGES[0]?.id);',
1031
+ // Pages render only in the browser: some components crash a server
1032
+ // render, and Next prerenders a client page's first state.
1033
+ ' const [ready, setReady] = React.useState(false);',
1034
+ ' React.useEffect(() => setReady(true), []);',
1031
1035
  ...(theme
1032
1036
  ? [
1033
1037
  oneMode
@@ -32,6 +32,16 @@ export function openProject(cwd: string): Promise<ThemeProject>;
32
32
  * @returns {string | null}
33
33
  */
34
34
  export function importPathOf(project: ThemeProject, name: string): string | null;
35
+ /**
36
+ * The built module of a base's theme package, resolved the way the theme
37
+ * resolves it: from the project (an open project, or its folder, relative
38
+ * or not). Null when it is not installed there. The one base resolution:
39
+ * `new`, `set base`, `how` and every suggestion ask this.
40
+ * @param {ThemeProject | string} from
41
+ * @param {string} like
42
+ * @returns {string | null}
43
+ */
44
+ export function baseModule(from: ThemeProject | string, like: string): string | null;
35
45
  /**
36
46
  * The built-in theme package for a base, or a refusal naming its install line.
37
47
  * @param {ThemeProject} project
@@ -51,6 +61,7 @@ export function baseResolved(project: ThemeProject, like: string | undefined): P
51
61
  components: Record<string, any>;
52
62
  }>;
53
63
  export function literals(type: string): string[];
64
+ export function baseInstalled(from: ThemeProject | string, like: string): boolean;
54
65
  export type ThemeProject = {
55
66
  cwd: string;
56
67
  /**
@@ -321,6 +321,34 @@ export function importPathOf(project, name) {
321
321
  return spec && spec !== '@astryxdesign/core' ? spec : null;
322
322
  }
323
323
 
324
+ /**
325
+ * The built module of a base's theme package, resolved the way the theme
326
+ * resolves it: from the project (an open project, or its folder, relative
327
+ * or not). Null when it is not installed there. The one base resolution:
328
+ * `new`, `set base`, `how` and every suggestion ask this.
329
+ * @param {ThemeProject | string} from
330
+ * @param {string} like
331
+ * @returns {string | null}
332
+ */
333
+ export function baseModule(from, like) {
334
+ const req =
335
+ typeof from === 'string'
336
+ ? createRequire(path.join(path.resolve(from), 'noop.js'))
337
+ : from.req;
338
+ try {
339
+ return req.resolve(`@astryxdesign/theme-${like}/built`);
340
+ } catch {
341
+ return null;
342
+ }
343
+ }
344
+
345
+ /**
346
+ * Whether a base's theme package is installed in the project.
347
+ * @param {ThemeProject | string} from
348
+ * @param {string} like
349
+ */
350
+ export const baseInstalled = (from, like) => baseModule(from, like) != null;
351
+
324
352
  /**
325
353
  * The built-in theme package for a base, or a refusal naming its install line.
326
354
  * @param {ThemeProject} project
@@ -329,15 +357,13 @@ export function importPathOf(project, name) {
329
357
  */
330
358
  export function baseThemeFile(project, like) {
331
359
  const pkg = `@astryxdesign/theme-${like}`;
332
- try {
333
- return project.req.resolve(`${pkg}/built`);
334
- } catch {
335
- throw new AstryxError(
336
- `The ${like} theme is not installed, so a theme cannot build on it. Install ${pkg}, or build on no theme. Nothing was changed.`,
337
- [{name: installLine(project.cwd, [pkg]), reason: 'install it'}],
338
- ERROR_CODES.ERR_UNKNOWN_THEME,
339
- );
340
- }
360
+ const file = baseModule(project, like);
361
+ if (file) return file;
362
+ throw new AstryxError(
363
+ `The ${like} theme is not installed, so a theme cannot build on it. Install ${pkg}, or build on no theme. Nothing was changed.`,
364
+ [{name: installLine(project.cwd, [pkg]), reason: 'install it'}],
365
+ ERROR_CODES.ERR_UNKNOWN_THEME,
366
+ );
341
367
  }
342
368
 
343
369
  /**