@astryxdesign/cli 0.7.15-canary.gf890983 → 0.7.15

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 (115) hide show
  1. package/CHANGELOG.md +353 -3
  2. package/api/doctor/_adapter.d.mts +2 -1
  3. package/api/doctor/_adapter.mjs +6 -1
  4. package/api/doctor/doctor.mjs +2 -2
  5. package/api/doctor/theme-checks.mjs +56 -21
  6. package/api/integration/add-helpers.mjs +5 -0
  7. package/api/integration/add-theme.mjs +15 -110
  8. package/api/theme/_adapter.d.mts +37 -5
  9. package/api/theme/_adapter.mjs +363 -50
  10. package/api/theme/_fork.d.mts +90 -0
  11. package/api/theme/_fork.mjs +246 -0
  12. package/api/theme/build/build.d.mts +25 -1
  13. package/api/theme/build/build.mjs +352 -41
  14. package/api/theme/eject/eject.mjs +36 -15
  15. package/api/theme/list/list.mjs +3 -6
  16. package/api/theme/palette/generate/generator.d.mts +13 -0
  17. package/api/theme/palette/generate/generator.mjs +32 -2
  18. package/api/theme/targets/targets.mjs +10 -3
  19. package/api/theme/template/template.mjs +48 -2
  20. package/api/theme/theme.type.d.mts +8 -0
  21. package/api/theme/theme.type.mjs +5 -1
  22. package/api/theme/themeBuild.doc.mjs +2 -2
  23. package/api/theme/themeRemove.doc.mjs +5 -2
  24. package/api/theme/themeTemplate.doc.mjs +2 -2
  25. package/api/theme/themeUse.doc.mjs +8 -2
  26. package/api/theme-edit/_adapter.mjs +157 -36
  27. package/api/theme-edit/_catalog.d.mts +1 -6
  28. package/api/theme-edit/_catalog.mjs +5 -268
  29. package/api/theme-edit/_check.d.mts +3 -1
  30. package/api/theme-edit/_check.mjs +733 -308
  31. package/api/theme-edit/_color.d.mts +19 -0
  32. package/api/theme-edit/_color.mjs +277 -33
  33. package/api/theme-edit/_compiler.d.mts +1 -0
  34. package/api/theme-edit/_compiler.mjs +25 -0
  35. package/api/theme-edit/_context.d.mts +2 -0
  36. package/api/theme-edit/_context.mjs +83 -9
  37. package/api/theme-edit/_css.d.mts +84 -0
  38. package/api/theme-edit/_css.mjs +700 -0
  39. package/api/theme-edit/_edit.mjs +370 -85
  40. package/api/theme-edit/_how-commands.d.mts +5 -0
  41. package/api/theme-edit/_how-commands.mjs +20 -13
  42. package/api/theme-edit/_how-question.d.mts +29 -0
  43. package/api/theme-edit/_how-question.mjs +1573 -182
  44. package/api/theme-edit/_how.mjs +306 -22
  45. package/api/theme-edit/_overview.mjs +2 -7
  46. package/api/theme-edit/_palette.d.mts +27 -5
  47. package/api/theme-edit/_palette.mjs +231 -97
  48. package/api/theme-edit/_parts.d.mts +42 -10
  49. package/api/theme-edit/_parts.mjs +224 -69
  50. package/api/theme-edit/_preview-app.d.mts +8 -0
  51. package/api/theme-edit/_preview-app.mjs +549 -96
  52. package/api/theme-edit/_preview.d.mts +7 -16
  53. package/api/theme-edit/_preview.mjs +77 -84
  54. package/api/theme-edit/_project.d.mts +20 -1
  55. package/api/theme-edit/_project.mjs +43 -13
  56. package/api/theme-edit/_settings.d.mts +96 -20
  57. package/api/theme-edit/_settings.mjs +646 -141
  58. package/api/theme-edit/_tables.d.mts +18 -12
  59. package/api/theme-edit/_tables.mjs +144 -147
  60. package/api/theme-edit/theme-edit.type.d.mts +10 -1
  61. package/api/theme-edit/theme-edit.type.mjs +3 -1
  62. package/api/theme-edit/themeEditNew.doc.mjs +1 -1
  63. package/api/theme-edit/themeEditPreviewAdd.doc.mjs +1 -1
  64. package/api/theme-edit/themeEditPreviewApp.doc.mjs +1 -1
  65. package/api/theme-edit/themeEditPreviewList.doc.mjs +2 -2
  66. package/api/theme-edit/themeEditSet.doc.mjs +1 -1
  67. package/api/theme-edit/themeEditUndo.doc.mjs +1 -1
  68. package/assets/docs/tree/publishing.doc.mjs +1 -0
  69. package/assets/docs.generated.json +2 -2
  70. package/assets/self-audit.generated.json +1 -1
  71. package/clients/cli/build-id.generated.json +1 -1
  72. package/clients/cli/commands/experimental/index.mjs +10 -10
  73. package/clients/cli/commands/experimental/theme.mjs +22 -3
  74. package/clients/cli/commands/experimental-theme-compile.doc.mjs +2 -2
  75. package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +3 -3
  76. package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +1 -1
  77. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
  78. package/clients/cli/commands/integration.mjs +8 -0
  79. package/clients/cli/commands/theme-build.doc.mjs +2 -2
  80. package/clients/cli/commands/theme-list.doc.mjs +1 -1
  81. package/clients/cli/commands/theme-template.doc.mjs +1 -1
  82. package/clients/cli/commands/theme.mjs +325 -48
  83. package/clients/cli/index.mjs +18 -3
  84. package/clients/cli/lib/cli-error.mjs +118 -3
  85. package/clients/cli/manifest.generated.json +1 -1
  86. package/foundation/config/project.mjs +34 -8
  87. package/foundation/config/theme-state.d.mts +16 -0
  88. package/foundation/config/theme-state.mjs +84 -13
  89. package/foundation/discovery/template-needs.d.mts +13 -0
  90. package/foundation/discovery/template-needs.mjs +30 -15
  91. package/foundation/discovery/theme-discovery.mjs +14 -2
  92. package/foundation/discovery/theme-imports.d.mts +16 -0
  93. package/foundation/discovery/theme-imports.mjs +30 -2
  94. package/foundation/doc-compiler/import.mjs +10 -4
  95. package/foundation/fs/gather-cache.mjs +82 -23
  96. package/foundation/fs/jiti-cache.d.mts +21 -0
  97. package/foundation/fs/jiti-cache.mjs +66 -0
  98. package/foundation/fs/module-loader.mjs +7 -2
  99. package/foundation/integrations/autolink.d.mts +1 -24
  100. package/foundation/integrations/autolink.mjs +7 -60
  101. package/foundation/integrations/declared-dependencies.d.mts +27 -0
  102. package/foundation/integrations/declared-dependencies.mjs +73 -0
  103. package/foundation/integrations/may-have-integrations.d.mts +8 -0
  104. package/foundation/integrations/may-have-integrations.mjs +47 -0
  105. package/foundation/release/cache.d.mts +21 -2
  106. package/foundation/release/cache.mjs +48 -8
  107. package/foundation/release/folder-lock.d.mts +7 -0
  108. package/foundation/release/folder-lock.mjs +60 -26
  109. package/foundation/release/lock-lease.d.mts +2 -1
  110. package/foundation/release/lock-lease.mjs +7 -2
  111. package/foundation/response/error-codes.doc.mjs +1 -1
  112. package/foundation/response/response-types.doc.mjs +4 -4
  113. package/package.json +6 -6
  114. package/api/theme-edit/_css-properties.d.mts +0 -10
  115. package/api/theme-edit/_css-properties.mjs +0 -556
@@ -49,7 +49,7 @@ import {
49
49
  list,
50
50
  } from '../formatters/index.mjs';
51
51
  import {logger} from '../../../api/logger.mjs';
52
- import {cliError} from '../lib/cli-error.mjs';
52
+ import {cliError, themeGroupUnknownSubcommand} from '../lib/cli-error.mjs';
53
53
  import {ERROR_CODES} from '../../../foundation/response/error-codes.mjs';
54
54
  import {Project} from '../../../foundation/config/project.mjs';
55
55
  import {warnOnIntegrationIssues} from '../../../foundation/integrations/integration-warnings.mjs';
@@ -76,7 +76,9 @@ import {
76
76
  themeBuild,
77
77
  themeBuildFamily,
78
78
  themeBuildForReport,
79
+ themeSourceFiles,
79
80
  themeSourceGuesses,
81
+ writeDeferredBuildOutputs,
80
82
  } from '../../../api/theme/build/build.mjs';
81
83
  import {defineCommand} from '../lib/define-command.mjs';
82
84
  import {NO_RESULT_SET, resultSet} from '../../../foundation/debug/index.mjs';
@@ -178,15 +180,19 @@ function inGroupNotes(result, words) {
178
180
  * A theme API refusal as the group that ran it words it: under
179
181
  * `experimental theme`, the commands it names are that group's own (`build`
180
182
  * is `compile` there). Under `theme`, the text is unchanged.
183
+ * A refusal that names no command gets `fallback`: a command of the same
184
+ * group that works whatever went wrong.
181
185
  * @param {import('../../../api/error.mjs').AstryxError} err
182
186
  * @param {ThemeCommandWords} [words]
187
+ * @param {Array<{name: string, reason: string}>} [fallback]
183
188
  */
184
- function themeError(err, words = THEME_WORDS) {
189
+ function themeError(err, words = THEME_WORDS, fallback = []) {
185
190
  err = words.explain?.(err, process.cwd()) ?? err;
186
191
  /** @param {string} text */
187
192
  const local = text => inGroupWords(text, words);
193
+ const given = err.suggestions || [];
188
194
  return cliError(local(err.message), {
189
- suggestions: (err.suggestions || []).map(x => ({
195
+ suggestions: (given.length > 0 ? given : fallback).map(x => ({
190
196
  ...x,
191
197
  name: local(x.name),
192
198
  ...(x.reason ? {reason: local(x.reason)} : {}),
@@ -195,6 +201,27 @@ function themeError(err, words = THEME_WORDS) {
195
201
  });
196
202
  }
197
203
 
204
+ /**
205
+ * A command of this CLI as this project runs it, each argument quoted for the
206
+ * shell.
207
+ * @param {ThemeCommandWords} words
208
+ * @param {string} sub the subcommand, such as `list` or the build word
209
+ * @param {string[]} [args]
210
+ * @param {string} reason
211
+ * @returns {{name: string, reason: string}}
212
+ */
213
+ function themeCommand(words, sub, args = [], reason = '') {
214
+ return {
215
+ name: [
216
+ getCliInvocation(process.cwd()),
217
+ words.group,
218
+ sub,
219
+ ...args.map(arg => quoteCommandArgument(arg)),
220
+ ].join(' '),
221
+ reason,
222
+ };
223
+ }
224
+
198
225
  /**
199
226
  * Path to this CLI's real entry (clients/cli/bin/astryx.mjs), resolved from
200
227
  * this module's location (clients/cli/commands/theme.mjs → ../bin/
@@ -304,6 +331,8 @@ async function runThemeBuildWatch(entries, options, words = THEME_WORDS) {
304
331
  building = false;
305
332
  // A stop during this build has already ended the watch.
306
333
  if (stopped) return;
334
+ // The rebuilt source may import other files now.
335
+ watchSourceFiles(entry);
307
336
  emit(text(watchingLine));
308
337
  const next = queued.values().next();
309
338
  if (!next.done) {
@@ -312,21 +341,56 @@ async function runThemeBuildWatch(entries, options, words = THEME_WORDS) {
312
341
  }
313
342
  };
314
343
 
315
- // Some editors replace the file (rename) rather than writing in place, which
316
- // can drop the watch. Watch the containing directory and filter to our file
317
- // so edits survive atomic-save/rename.
318
- const watchers = entries.map(entry => {
319
- const baseName = path.basename(entry.filePath);
320
- return fs.watch(path.dirname(entry.filePath), (_eventType, changed) => {
321
- if (changed && changed !== baseName) return;
322
- clearTimeout(debounces.get(entry.filePath));
323
- // Debounce: editors often emit several events per save.
324
- debounces.set(
325
- entry.filePath,
326
- setTimeout(() => rebuild(entry), 100),
327
- );
328
- });
329
- });
344
+ // A theme is its source and the local files it imports (an ejected fork's
345
+ // palettes, a split theme); a change to any of them rebuilds it. Some
346
+ // editors replace a file (rename) rather than write in place, which can drop
347
+ // a file watch, so each containing folder is watched and filtered by name.
348
+ /** @type {Map<{file: string, filePath: string}, Set<string>>} */
349
+ const sourceFiles = new Map();
350
+ /** @type {Map<string, import('node:fs').FSWatcher>} */
351
+ const folderWatchers = new Map();
352
+ /** @param {{file: string, filePath: string}} entry */
353
+ const changed = entry => {
354
+ clearTimeout(debounces.get(entry.filePath));
355
+ // Debounce: editors often emit several events per save.
356
+ debounces.set(
357
+ entry.filePath,
358
+ setTimeout(() => rebuild(entry), 100),
359
+ );
360
+ };
361
+ /** @param {string} dir */
362
+ const watchFolder = dir => {
363
+ if (folderWatchers.has(dir)) return;
364
+ folderWatchers.set(
365
+ dir,
366
+ fs.watch(dir, (_eventType, name) => {
367
+ const file = name ? path.join(dir, name.toString()) : null;
368
+ for (const [entry, files] of sourceFiles) {
369
+ if (
370
+ file
371
+ ? files.has(file)
372
+ : [...files].some(f => path.dirname(f) === dir)
373
+ ) {
374
+ changed(entry);
375
+ }
376
+ }
377
+ }),
378
+ );
379
+ };
380
+ /** @param {{file: string, filePath: string}} entry */
381
+ function watchSourceFiles(entry) {
382
+ const files = new Set(themeSourceFiles(entry.filePath));
383
+ files.add(path.resolve(entry.filePath));
384
+ sourceFiles.set(entry, files);
385
+ for (const file of files) {
386
+ try {
387
+ watchFolder(path.dirname(file));
388
+ } catch {
389
+ // A folder that went away is watched again after the next build.
390
+ }
391
+ }
392
+ }
393
+ for (const entry of entries) watchSourceFiles(entry);
330
394
 
331
395
  // Announce readiness only AFTER fs.watch is armed — the log is the "safe to
332
396
  // edit" signal (tests and humans rely on it), so printing it before the watch
@@ -344,7 +408,7 @@ async function runThemeBuildWatch(entries, options, words = THEME_WORDS) {
344
408
  stopped = true;
345
409
  queued.clear();
346
410
  for (const d of debounces.values()) clearTimeout(d);
347
- for (const w of watchers) w.close();
411
+ for (const w of folderWatchers.values()) w.close();
348
412
  // A rebuild in progress would otherwise keep the process alive after
349
413
  // "Stopped watching.". In a terminal, Ctrl-C already reached it.
350
414
  rebuildChild?.kill(signal);
@@ -581,7 +645,7 @@ function printThemeEject(result, words = THEME_WORDS) {
581
645
  ]
582
646
  : []),
583
647
  text(
584
- `This is a local fork. Build it with ${getCliInvocation()} ${words.group} ${words.build} ${path.join(data.outputDir, data.entry)}.`,
648
+ `This is a local fork. Build it with ${getCliInvocation()} ${words.group} ${words.build} ${quoteCommandArgument(path.join(data.outputDir, data.entry).split(path.sep).join('/'))}.`,
585
649
  ),
586
650
  );
587
651
  }
@@ -629,13 +693,13 @@ export function registerTheme(program) {
629
693
  // us as a positional in cmd.args — exit 1 with a clear error.
630
694
  const extras = (cmd && cmd.args) || [];
631
695
  if (extras.length > 0) {
632
- const unknown = String(extras[0]);
633
- const known = (theme.commands || []).map(c => c.name());
634
- const suggestions = known.map(name => ({
635
- name,
636
- reason: 'available subcommand',
637
- }));
638
- return cliError(`unknown subcommand 'theme ${unknown}'`, {
696
+ const {message, suggestions} = themeGroupUnknownSubcommand(
697
+ 'theme',
698
+ String(extras[0]),
699
+ (theme.commands || []).map(c => c.name()),
700
+ theme,
701
+ );
702
+ return cliError(message, {
639
703
  suggestions,
640
704
  code: ERROR_CODES.ERR_UNKNOWN_SUBCOMMAND,
641
705
  });
@@ -836,18 +900,79 @@ function registerThemeCommands(theme, program, docs, words) {
836
900
  name: `${getCliInvocation(process.cwd())} ${words.group} ${words.build} ${quoteCommandArgument(guess)}`,
837
901
  reason: 'a theme source that exists',
838
902
  }))
839
- : undefined,
903
+ : [
904
+ themeCommand(
905
+ words,
906
+ 'template',
907
+ ['theme.template.ts'],
908
+ 'start a theme source from the annotated template',
909
+ ),
910
+ ],
840
911
  });
841
912
  }
842
913
 
914
+ /**
915
+ * This build again without some of its options, as the way out of a
916
+ * refusal about how they combine.
917
+ * @param {Array<'out' | 'watch' | 'check' | 'family'>} drop
918
+ * @param {string} reason
919
+ * @param {string[]} [only] the files to build, when not all of them
920
+ */
921
+ const buildWithout = (drop, reason, only = files) =>
922
+ themeCommand(
923
+ words,
924
+ words.build,
925
+ [
926
+ ...(options.family && !drop.includes('family')
927
+ ? ['--family', ...only, '--family-key', String(options.familyKey)]
928
+ : only),
929
+ ...(options.out && !drop.includes('out')
930
+ ? ['--out', options.out]
931
+ : []),
932
+ ...(options.check && !drop.includes('check') ? ['--check'] : []),
933
+ ...(options.watch && !drop.includes('watch') ? ['--watch'] : []),
934
+ ...(options.iconsSpecifier
935
+ ? ['--icons-specifier', options.iconsSpecifier]
936
+ : []),
937
+ ],
938
+ reason,
939
+ );
940
+
941
+ // A refusal from the build that names no command: without --out the
942
+ // outputs go beside the source; otherwise the annotated template shows
943
+ // a theme source that builds.
944
+ const buildFallback = options.out
945
+ ? [buildWithout(['out'], 'write the outputs beside the source')]
946
+ : [
947
+ themeCommand(
948
+ words,
949
+ 'template',
950
+ ['theme.template.ts'],
951
+ 'an annotated theme source that builds, to compare with',
952
+ ),
953
+ ];
954
+
843
955
  // One file passed twice would build twice and report two themes.
844
956
  const seenFiles = new Set();
845
957
  for (const entry of entries) {
846
958
  const key = fs.realpathSync(entry.filePath);
847
959
  if (seenFiles.has(key)) {
960
+ const once = entries
961
+ .filter(
962
+ (other, index) =>
963
+ entries.findIndex(
964
+ first =>
965
+ fs.realpathSync(first.filePath) ===
966
+ fs.realpathSync(other.filePath),
967
+ ) === index,
968
+ )
969
+ .map(other => other.file);
848
970
  return cliError(
849
971
  `${entry.file} is given more than once. Select each theme file once.`,
850
- {code: ERROR_CODES.ERR_THEME_INVALID},
972
+ {
973
+ code: ERROR_CODES.ERR_THEME_INVALID,
974
+ suggestions: [buildWithout([], 'each file once', once)],
975
+ },
851
976
  );
852
977
  }
853
978
  seenFiles.add(key);
@@ -861,17 +986,40 @@ function registerThemeCommands(theme, program, docs, words) {
861
986
  options.family
862
987
  ? '--family requires --family-key <key>'
863
988
  : '--family-key requires --family',
864
- {code: ERROR_CODES.ERR_THEME_INVALID},
989
+ {
990
+ code: ERROR_CODES.ERR_THEME_INVALID,
991
+ suggestions: [
992
+ {
993
+ ...themeCommand(
994
+ words,
995
+ words.build,
996
+ [
997
+ ...files,
998
+ // --out names one output, so it goes with one file only.
999
+ ...(options.out && files.length === 1
1000
+ ? ['--out', options.out]
1001
+ : []),
1002
+ ...(options.check ? ['--check'] : []),
1003
+ ],
1004
+ 'build each theme on its own',
1005
+ ),
1006
+ },
1007
+ ],
1008
+ },
865
1009
  );
866
1010
  }
867
1011
  if (options.family && options.out) {
868
1012
  return cliError('--family cannot be combined with --out', {
869
1013
  code: ERROR_CODES.ERR_THEME_INVALID,
1014
+ suggestions: [
1015
+ buildWithout(['out'], 'family outputs go beside the family root'),
1016
+ ],
870
1017
  });
871
1018
  }
872
1019
  if (options.family && options.watch) {
873
1020
  return cliError('--family cannot be combined with --watch', {
874
1021
  code: ERROR_CODES.ERR_THEME_INVALID,
1022
+ suggestions: [buildWithout(['watch'], 'build the family once')],
875
1023
  });
876
1024
  }
877
1025
 
@@ -880,6 +1028,7 @@ function registerThemeCommands(theme, program, docs, words) {
880
1028
  if (options.check && options.watch) {
881
1029
  return cliError('--check cannot be combined with --watch', {
882
1030
  code: ERROR_CODES.ERR_THEME_INVALID,
1031
+ suggestions: [buildWithout(['watch'], 'check once')],
883
1032
  });
884
1033
  }
885
1034
 
@@ -891,7 +1040,15 @@ function registerThemeCommands(theme, program, docs, words) {
891
1040
  `--out takes a single output path and ${entries.length} theme files were given. ` +
892
1041
  'Build them without --out (each theme writes <name>.css next to its source), ' +
893
1042
  'or run one invocation per theme.',
894
- {code: ERROR_CODES.ERR_THEME_INVALID},
1043
+ {
1044
+ code: ERROR_CODES.ERR_THEME_INVALID,
1045
+ suggestions: [
1046
+ buildWithout(
1047
+ ['out'],
1048
+ 'each theme writes <name>.css next to its source',
1049
+ ),
1050
+ ],
1051
+ },
895
1052
  );
896
1053
  }
897
1054
 
@@ -902,6 +1059,9 @@ function registerThemeCommands(theme, program, docs, words) {
902
1059
  if (json) {
903
1060
  return cliError('--watch is not supported with --json', {
904
1061
  code: ERROR_CODES.ERR_THEME_INVALID,
1062
+ suggestions: [
1063
+ buildWithout(['watch'], 'build once, with a JSON result'),
1064
+ ],
905
1065
  });
906
1066
  }
907
1067
  await runThemeBuildWatch(entries, {...options, detail}, words);
@@ -924,7 +1084,7 @@ function registerThemeCommands(theme, program, docs, words) {
924
1084
  check: options.check,
925
1085
  family: Boolean(options.family),
926
1086
  });
927
- if (refused) return themeError(refused, words);
1087
+ if (refused) return themeError(refused, words, buildFallback);
928
1088
 
929
1089
  if (options.family) {
930
1090
  try {
@@ -950,7 +1110,9 @@ function registerThemeCommands(theme, program, docs, words) {
950
1110
  } catch (e) {
951
1111
  const err =
952
1112
  /** @type {import('../../../api/error.mjs').AstryxError} */ (e);
953
- return themeError(err, words);
1113
+ return themeError(err, words, [
1114
+ buildWithout(['family'], 'build each theme on its own'),
1115
+ ]);
954
1116
  }
955
1117
  }
956
1118
 
@@ -962,9 +1124,50 @@ function registerThemeCommands(theme, program, docs, words) {
962
1124
  /** @type {Array<{file: string, receipt: import('../../../api/theme/theme.type.mjs').ThemeBuildResponse | import('../../../api/theme/theme.type.mjs').ThemeBuildCheckResponse | null}>} */
963
1125
  const results = [];
964
1126
  let stale = false;
1127
+ // A batch builds every theme first and then writes them in order, so
1128
+ // two themes that build the same files are refused before either
1129
+ // writes. Any other failure writes the themes built before it, as a
1130
+ // shell loop under `set -e` would have, and stops.
1131
+ const deferred = !options.check && entries.length > 1;
1132
+ // Each built theme keeps its writes and what it prints together; what
1133
+ // it prints waits until its outputs are written, so no `[ok]` line
1134
+ // names a file that is never written.
1135
+ /** @type {Array<{writes: Array<Array<{dest: string, content: string}>>, lines: Array<() => void>}>} */
1136
+ const built = [];
1137
+ const publish = () => {
1138
+ for (const theme of built.splice(0)) {
1139
+ writeDeferredBuildOutputs(theme.writes);
1140
+ for (const show of theme.lines) show();
1141
+ }
1142
+ };
1143
+ const writeFailed = (/** @type {unknown} */ error) =>
1144
+ themeError(
1145
+ /** @type {import('../../../api/error.mjs').AstryxError} */ (error),
1146
+ words,
1147
+ [
1148
+ buildWithout(
1149
+ [],
1150
+ 'run the same build again once the files can be written',
1151
+ ),
1152
+ ],
1153
+ );
965
1154
  /** @type {Map<string, string>} */
966
1155
  const claimedOutputs = new Map();
967
1156
  for (const entry of entries) {
1157
+ /** @type {Array<() => void>} */
1158
+ const lines = [];
1159
+ /** @type {Array<Array<{dest: string, content: string}>>} */
1160
+ const writes = [];
1161
+ const log = logger.log;
1162
+ const warn = logger.warn;
1163
+ if (deferred) {
1164
+ logger.log = (/** @type {string | undefined} */ message) => {
1165
+ lines.push(() => log.call(logger, message));
1166
+ };
1167
+ logger.warn = (/** @type {string | undefined} */ message) => {
1168
+ lines.push(() => warn.call(logger, message));
1169
+ };
1170
+ }
968
1171
  try {
969
1172
  const buildOptions = {
970
1173
  out: options.out,
@@ -972,6 +1175,7 @@ function registerThemeCommands(theme, program, docs, words) {
972
1175
  iconsSpecifier: options.iconsSpecifier,
973
1176
  __onPrivateVarError: onPrivateVarError,
974
1177
  __claimedOutputs: claimedOutputs,
1178
+ __deferredWrites: deferred ? writes : undefined,
975
1179
  __commandWords: words,
976
1180
  };
977
1181
  let result;
@@ -986,18 +1190,24 @@ function registerThemeCommands(theme, program, docs, words) {
986
1190
  if (built.trailer) trailers.push(built.trailer);
987
1191
  if (compact && result?.type === 'theme.build') {
988
1192
  const {outputs, sizeKB, tokenCount, componentCount} = result.data;
989
- emit(
990
- text(
991
- `[ok] ${outputs.css} (${sizeKB} KB, ${tokenCount} token overrides, ${componentCount} component overrides)`,
992
- ),
993
- );
1193
+ const show = () =>
1194
+ emit(
1195
+ text(
1196
+ `[ok] ${outputs.css} (${sizeKB} KB, ${tokenCount} token overrides, ${componentCount} component overrides)`,
1197
+ ),
1198
+ );
1199
+ if (deferred) lines.push(show);
1200
+ else show();
994
1201
  }
995
1202
  } else {
996
1203
  result = await themeBuild(entry.file, buildOptions, {
997
1204
  cwd: process.cwd(),
998
1205
  });
999
1206
  }
1207
+ logger.log = log;
1208
+ logger.warn = warn;
1000
1209
  results.push({file: entry.file, receipt: result ?? null});
1210
+ if (deferred) built.push({writes, lines});
1001
1211
  if (
1002
1212
  options.check &&
1003
1213
  result &&
@@ -1007,16 +1217,44 @@ function registerThemeCommands(theme, program, docs, words) {
1007
1217
  stale = true;
1008
1218
  }
1009
1219
  } catch (e) {
1220
+ logger.log = log;
1221
+ logger.warn = warn;
1010
1222
  const err =
1011
1223
  /** @type {import('../../../api/error.mjs').AstryxError} */ (e);
1012
1224
  // Stop at the first failure, as a shell loop under `set -e` does.
1013
1225
  // With several themes in flight the message alone rarely says which
1014
1226
  // one broke, so name it. Themes already written keep their install
1015
1227
  // and font guidance.
1228
+ if (/** @type {any} */ (err).outputCollision) {
1229
+ // Nothing is written, so only this theme's warnings are shown.
1230
+ for (const show of lines) show();
1231
+ return themeError(err, words, [
1232
+ buildWithout([], 'build the first theme on its own', [
1233
+ entries[0].file,
1234
+ ]),
1235
+ ]);
1236
+ }
1237
+ if (deferred) {
1238
+ try {
1239
+ publish();
1240
+ } catch (writeError) {
1241
+ return writeFailed(writeError);
1242
+ }
1243
+ // The failing theme's warnings, such as a missing package, say
1244
+ // why it failed.
1245
+ for (const show of lines) show();
1246
+ }
1016
1247
  if (report && compact) printCompactTrailer(trailers);
1017
1248
  else if (report) printBatchTrailer(trailers);
1018
1249
  if (entries.length > 1) err.message = `${entry.file}: ${err.message}`;
1019
- return themeError(err, words);
1250
+ return themeError(err, words, buildFallback);
1251
+ }
1252
+ }
1253
+ if (deferred) {
1254
+ try {
1255
+ publish();
1256
+ } catch (e) {
1257
+ return writeFailed(e);
1020
1258
  }
1021
1259
  }
1022
1260
 
@@ -1068,6 +1306,9 @@ function registerThemeCommands(theme, program, docs, words) {
1068
1306
  /** @type {(() => Promise<void>) | undefined} */
1069
1307
  let release;
1070
1308
  buildCli.hook('preAction', async (_, cmd) => {
1309
+ // Under --watch each rebuild runs on its own and takes the locks
1310
+ // itself; holding them for the whole watch would block every one.
1311
+ if (cmd.opts().watch) return;
1071
1312
  release = await words.hold?.(cmd.args, {cwd: process.cwd()});
1072
1313
  });
1073
1314
  buildCli.hook('postAction', async () => {
@@ -1090,7 +1331,14 @@ function registerThemeCommands(theme, program, docs, words) {
1090
1331
  } catch (e) {
1091
1332
  const err =
1092
1333
  /** @type {import('../../../api/error.mjs').AstryxError} */ (e);
1093
- return themeError(err, words);
1334
+ return themeError(err, words, [
1335
+ options.package
1336
+ ? themeCommand(words, 'list', [], 'every theme, from every package')
1337
+ : {
1338
+ name: `${getCliInvocation(process.cwd())} doctor`,
1339
+ reason: 'names the integration or theme that is broken',
1340
+ },
1341
+ ]);
1094
1342
  }
1095
1343
 
1096
1344
  await warnOnThemeIntegrationIssues(json);
@@ -1137,7 +1385,9 @@ function registerThemeCommands(theme, program, docs, words) {
1137
1385
  } catch (e) {
1138
1386
  const err =
1139
1387
  /** @type {import('../../../api/error.mjs').AstryxError} */ (e);
1140
- return themeError(err, words);
1388
+ return themeError(err, words, [
1389
+ themeCommand(words, 'list', [], 'the themes you can add'),
1390
+ ]);
1141
1391
  }
1142
1392
 
1143
1393
  await warnOnThemeIntegrationIssues(json);
@@ -1176,7 +1426,9 @@ function registerThemeCommands(theme, program, docs, words) {
1176
1426
  } catch (e) {
1177
1427
  const err =
1178
1428
  /** @type {import('../../../api/error.mjs').AstryxError} */ (e);
1179
- return themeError(err, words);
1429
+ return themeError(err, words, [
1430
+ themeCommand(words, 'list', [], 'the themes this app has added'),
1431
+ ]);
1180
1432
  }
1181
1433
  await warnOnThemeIntegrationIssues(json);
1182
1434
  result = inGroupNotes(result, words);
@@ -1197,7 +1449,9 @@ function registerThemeCommands(theme, program, docs, words) {
1197
1449
  } catch (e) {
1198
1450
  const err =
1199
1451
  /** @type {import('../../../api/error.mjs').AstryxError} */ (e);
1200
- return themeError(err, words);
1452
+ return themeError(err, words, [
1453
+ themeCommand(words, 'list', [], 'the themes this app has added'),
1454
+ ]);
1201
1455
  }
1202
1456
  await warnOnThemeIntegrationIssues(json);
1203
1457
  result = inGroupNotes(result, words);
@@ -1227,7 +1481,9 @@ function registerThemeCommands(theme, program, docs, words) {
1227
1481
  } catch (e) {
1228
1482
  const err =
1229
1483
  /** @type {import('../../../api/error.mjs').AstryxError} */ (e);
1230
- return themeError(err, words);
1484
+ return themeError(err, words, [
1485
+ themeCommand(words, 'list', [], 'the themes you can fork'),
1486
+ ]);
1231
1487
  }
1232
1488
  await warnOnThemeIntegrationIssues(json);
1233
1489
  if (json) jsonOut(result);
@@ -1255,7 +1511,14 @@ function registerThemeCommands(theme, program, docs, words) {
1255
1511
  } catch (e) {
1256
1512
  const err =
1257
1513
  /** @type {import('../../../api/error.mjs').AstryxError} */ (e);
1258
- return themeError(err, words);
1514
+ return themeError(err, words, [
1515
+ themeCommand(
1516
+ words,
1517
+ 'template',
1518
+ ['theme.template.ts'],
1519
+ 'write the template at the project root',
1520
+ ),
1521
+ ]);
1259
1522
  }
1260
1523
 
1261
1524
  // Writes a starter theme file, or declines to overwrite one.
@@ -1266,9 +1529,19 @@ function registerThemeCommands(theme, program, docs, words) {
1266
1529
 
1267
1530
  const invocation = getCliInvocation(process.cwd());
1268
1531
  if (!result.data.written) {
1532
+ const {overwritable, alternative} = result.data;
1269
1533
  emit(
1270
1534
  text(`[skip] ${result.data.path} already exists — left as is.`),
1271
- text(`Pass --overwrite to replace it with a fresh copy.`),
1535
+ ...(overwritable === false && alternative
1536
+ ? [
1537
+ text(
1538
+ 'It is not a theme template that --overwrite may replace. Write the template to a new file:',
1539
+ ),
1540
+ code(
1541
+ `${invocation} ${words.group} template ${quoteCommandArgument(alternative)}`,
1542
+ ),
1543
+ ]
1544
+ : [text(`Pass --overwrite to replace it with a fresh copy.`)]),
1272
1545
  );
1273
1546
  return NO_RESULT_SET;
1274
1547
  }
@@ -1278,7 +1551,9 @@ function registerThemeCommands(theme, program, docs, words) {
1278
1551
  'It documents every defineTheme field, the token families, and the component override syntax. ' +
1279
1552
  'Copy what you need into your own theme file, then delete it.',
1280
1553
  ),
1281
- code(`${invocation} ${words.group} ${words.build} ${result.data.path}`),
1554
+ code(
1555
+ `${invocation} ${words.group} ${words.build} ${quoteCommandArgument(result.data.path)}`,
1556
+ ),
1282
1557
  );
1283
1558
  return NO_RESULT_SET;
1284
1559
  },
@@ -1296,7 +1571,9 @@ function registerThemeCommands(theme, program, docs, words) {
1296
1571
  } catch (e) {
1297
1572
  const err =
1298
1573
  /** @type {import('../../../api/error.mjs').AstryxError} */ (e);
1299
- return themeError(err, words);
1574
+ return themeError(err, words, [
1575
+ themeCommand(words, 'targets', [], 'every theming target'),
1576
+ ]);
1300
1577
  }
1301
1578
 
1302
1579
  // A filter is a substring search with an exact-name fast path (see
@@ -25,7 +25,7 @@ import {answeredBy, runChoice} from '../../foundation/release/run-context.mjs';
25
25
  import {cliInstallAdvice, installFolder, projectRelease, SETUP_REMINDER_KEY, takeNotice} from '../../foundation/release/notices.mjs';
26
26
  import {isCi} from '../../foundation/release/update-notice-env.mjs';
27
27
  import {buildManifest, storedManifest} from './lib/manifest.mjs';
28
- import {cliError} from './lib/cli-error.mjs';
28
+ import {cliError, themeGroupUnknownSubcommand} from './lib/cli-error.mjs';
29
29
  import {section, text, records} from './formatters/index.mjs';
30
30
  import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
31
31
  import {levenshteinDistance} from '../../foundation/text/string-utils.mjs';
@@ -732,10 +732,25 @@ export async function createProgram({argv} = {}) {
732
732
  const extras = actionCommand.commands.length > 0 ? actionCommand.args : [];
733
733
  const unknown = extras.find(arg => !String(arg).startsWith('-'));
734
734
  if (unknown != null) {
735
+ const names = actionCommand.commands.map(child => child.name());
736
+ if (fullName === 'theme' || fullName === 'experimental') {
737
+ // The theme groups name whole commands, closest first, and point at
738
+ // `experimental theme <word>` when the word is one of its commands.
739
+ const {message, suggestions} = themeGroupUnknownSubcommand(
740
+ fullName,
741
+ String(unknown),
742
+ names,
743
+ actionCommand,
744
+ );
745
+ cliError(message, {
746
+ code: ERROR_CODES.ERR_UNKNOWN_SUBCOMMAND,
747
+ suggestions,
748
+ });
749
+ }
735
750
  cliError(`unknown subcommand '${fullName} ${unknown}'`, {
736
751
  code: ERROR_CODES.ERR_UNKNOWN_SUBCOMMAND,
737
- suggestions: actionCommand.commands.map(child => ({
738
- name: child.name(),
752
+ suggestions: names.map(name => ({
753
+ name,
739
754
  reason: 'available subcommand',
740
755
  })),
741
756
  });